// import { _decorator, Component } from 'cc';
// const { ccclass } = _decorator;
import {_decorator ,sys} from 'cc';
const { ccclass, property } = _decorator;


@ccclass('GlobalManager')
export default class GlobalManager {
    private static instance: GlobalManager;
    private methodMap: Map<string, Function>;

    // 禁止外部 new 实例化
    private constructor() {
        this.methodMap = new Map();
    }

    // 获取单例实例
    public static getInstance(): GlobalManager {
        if (!GlobalManager.instance) {
            GlobalManager.instance = new GlobalManager();
        }
        return GlobalManager.instance;
    }

    // 注册全局方法
    public registerMethod(name: string, method: Function): void {
        if (typeof method !== 'function') {
            console.error(`zh:尝试注册的 ${name} 不是一个函数`);
            return;
        }
        if (this.methodMap.has(name)) {
            console.warn(`zh:方法 ${name} 已经存在，正在覆盖`);
        }
        this.methodMap.set(name, method);
        console.log(`zh:成功注册全局方法 ${name}`);
    }

    // 调用全局方法
    public callMethod(name: string, ...args: any[]): any {
        const method = this.methodMap.get(name);
        if (method) {
            console.log(`zh:调用全局方法 ${name}`);
            return method(...args);
        } else {
            console.error(`zh:全局方法 Method ${name} not found`);
        }
    }
}


//GlobalManager_1.default.instance.callMethod('env_playAdEnd');


//GlobalManager_1.default.instance.registerMethod('env_playAdEnd', this.utilForAd);


// // 注册方法
// GlobalManager.getInstance().registerMethod('sayHello', (name: string) => {
//     console.log(`Hello, ${name}`);
// });

// // 调用方法
// GlobalManager.getInstance().callMethod('sayHello', 'World');
