"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeGlobalVar = exports.initGlobalVar = exports.getEnumOptions = void 0;
const global_api_1 = require("../app/global-api");
const computed_1 = require("./computed");
const mobx_1 = require("mobx");
const weda_client_1 = require("@cloudbase/weda-client");
const adapter_1 = require("./adapter");
const { createDataset } = weda_client_1._WEDA_CLOUD_SDK;
// 防止页面重复引用
let isInit = false;
let options = (0, mobx_1.observable)({});
let loading = {};
function getEnumOptions(optionName) {
    if (options[optionName]) {
        return options[optionName];
    }
    if (!loading[optionName]) {
        loading[optionName] = true;
        (0, weda_client_1._getGeneralOptions)(optionName).then((data) => {
            var _a;
            options[optionName] = (_a = data === null || data === void 0 ? void 0 : data.Items[0]) === null || _a === void 0 ? void 0 : _a.Config;
        });
    }
    return '';
}
exports.getEnumOptions = getEnumOptions;
function initGlobalVar({ common, state, computedFuncs, dataset = undefined }, initAppAPI = (app) => { }) {
    if (!isInit) {
        adapter_1.runtime['$$global'] = adapter_1.runtime['$$global'] || {};
        global_api_1.app.common = common;
        global_api_1.app.state = (0, mobx_1.observable)(state);
        global_api_1.app.computed = (0, computed_1.createComputed)(computedFuncs);
        const _dataset = dataset || createDataset('$global');
        global_api_1.app.dataset = _dataset;
        global_api_1.app.state.dataset = _dataset;
        global_api_1.app.enumOptions = options;
        /**
         * 对 app 上方法进行覆盖
         */
        initAppAPI === null || initAppAPI === void 0 ? void 0 : initAppAPI(global_api_1.app);
        /**
         * 覆盖之后使用新方法更新值
         */
        const { isMobile, $w } = (global_api_1.app === null || global_api_1.app === void 0 ? void 0 : global_api_1.app.__internal__) || {};
        // 应用信息
        if ($w === null || $w === void 0 ? void 0 : $w.wedaContext) {
            const type = (isMobile === null || isMobile === void 0 ? void 0 : isMobile()) ? 'MOBILEWEB' : 'PCWEB';
            $w.wedaContext.platforms = ($w.wedaContext.platforms
                ? $w.wedaContext.platforms.filter((item) => item !== 'MOBILEWEB' && item !== 'PCWEB')
                : ['WEB']).concat([type]);
        }
    }
}
exports.initGlobalVar = initGlobalVar;
function removeGlobalVar() {
    isInit = false;
    adapter_1.runtime['$$global'] = undefined;
}
exports.removeGlobalVar = removeGlobalVar;
//# sourceMappingURL=initGlobalVar.js.map