import { app } from '../app/global-api';
import { createComputed } from './computed';
import { observable } from 'mobx';
import { _WEDA_CLOUD_SDK as WEDA_CLOUD_SDK, _getGeneralOptions as getGeneralOptions } from '@cloudbase/weda-client';
import { runtime } from './adapter';
var createDataset = WEDA_CLOUD_SDK.createDataset;
// 防止页面重复引用
var isInit = false;
var options = observable({});
var loading = {};
export function getEnumOptions(optionName) {
    if (options[optionName]) {
        return options[optionName];
    }
    if (!loading[optionName]) {
        loading[optionName] = true;
        getGeneralOptions(optionName).then(function (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 '';
}
export function initGlobalVar(_a) {
    var common = _a.common, state = _a.state, computedFuncs = _a.computedFuncs, _b = _a.dataset, dataset = _b === void 0 ? undefined : _b;
    if (!isInit) {
        runtime['$$global'] = runtime['$$global'] || {};
        app.common = common;
        app.state = observable(state);
        app.computed = createComputed(computedFuncs);
        var _dataset = dataset || createDataset('$global');
        app.dataset = _dataset;
        app.state.dataset = _dataset;
        app.enumOptions = options;
    }
}
export function removeGlobalVar() {
    isInit = false;
    runtime['$$global'] = undefined;
}
//# sourceMappingURL=initGlobalVar.js.map