import { __assign } from "tslib";
import sdk from '@tcwd/weapps-sdk/lib/app-h5-sdk';
import { createWebApp, _ACTIONS_KEY as ACTIONS_KEY, _ROUTER_KEY as ROUTER_KEY, _WEDA_CLOUD_SDK as WEDA_CLOUD_SDK, } from '@cloudbase/weda-client';
import { runtime } from '../utils/adapter/index';
import { getEnumOptions } from '../utils/initGlobalVar';
var EXTRA_API = WEDA_CLOUD_SDK.EXTRA_API, createDataset = WEDA_CLOUD_SDK.createDataset;
var EPLATFORM;
(function (EPLATFORM) {
    EPLATFORM["WEB"] = "WEB";
    /**
     * 拼写错误
     * 兼容性无法修改
     */
    EPLATFORM["MINIPROGRAM"] = "MINIPROGRAME";
})(EPLATFORM || (EPLATFORM = {}));
export var subPackageName = '';
export var app = createGlboalApi();
export function registerApp(_app) {
    if (_app === void 0) { _app = createGlboalApi(); }
    _injectApp2Runtime(_app, true);
    app = _app;
}
mountAPIs(sdk);
function createGlboalApi(initData) {
    if (initData === void 0) { initData = {}; }
    var webApp = createWebApp(initData);
    var globalAPI = new Proxy(__assign(__assign({ id: initData.id || '', domain: initData.domain || '', state: {}, computed: {}, dataset: {}, common: {}, enumOptions: {} }, webApp), { utils: __assign(__assign({}, webApp.utils), { formatEnum: function (path, optionname, app) {
                var _a, _b, _c;
                var isSingle = Array.isArray(path);
                // 这个app是后来添加进去的
                var options = app === null || app === void 0 ? void 0 : app.enumOptions;
                var parseOptions = options[optionname]
                    ? options[optionname]
                    : getEnumOptions(optionname);
                if (parseOptions === '') {
                    return !isSingle ? path : path.join(',');
                }
                var multiTmp = [];
                var value = !isSingle
                    ? (_b = (_a = JSON.parse(parseOptions)) === null || _a === void 0 ? void 0 : _a.find(function (item) { return item.key === path; })) === null || _b === void 0 ? void 0 : _b.value
                    : (_c = JSON.parse(parseOptions)) === null || _c === void 0 ? void 0 : _c.filter(function (item) {
                        return path.some(function (pathValue) { return item.key === pathValue; });
                    }).map(function (item) { return multiTmp.push(item.value); });
                return !isSingle ? value : multiTmp.join(',');
            } }), 
        /**
         * setState
         */
        setState: function (userSetState) {
            Object.keys(userSetState).forEach(function (keyPath) {
                globalAPI.utils.set(globalAPI.dataset.state, keyPath, userSetState[keyPath]);
            });
        } }), {
        get: function (obj, prop) {
            if (prop === 'app' || prop === '$app') {
                return app;
            }
            else {
                return obj[prop];
            }
        },
        // set: (obj, prop, value) => {
        //   if (prop === 'app' || prop === '$app') {
        //     console.warn('$app 对象中 app/$app 为只读');
        //     return false;
        //   } else {
        //     // 修改 obj
        //     return true;
        //   }
        // },
    });
    _injectApp2Runtime(globalAPI);
    return globalAPI;
}
function _injectApp2Runtime(globalAPI, hard) {
    if (hard === void 0) { hard = false; }
    // 给全局挂上 mainApp/subApp
    // The global api exposed to lowcode
    if (subPackageName) {
        globalAPI.mainApp = runtime["$app"];
        runtime["$$subapp_" + subPackageName] = globalAPI;
    }
    else {
        // 预览区调试栏可调试
        // 暴露给复合组件，代码组件来引用
        runtime['app'] = hard ? globalAPI : runtime['app'] || globalAPI;
        runtime["$app"] = runtime['app'];
    }
}
export function removeGlboalApi() {
    if (subPackageName) {
        runtime["$$subapp_" + subPackageName] = undefined;
    }
    else {
        runtime['app'] = undefined;
        runtime["$app"] = undefined;
    }
}
export function createPageApi() {
    var $page = new Proxy({
        state: {},
        computed: {},
        handler: {},
        props: {},
        widgets: {},
        dataset: {},
        setState: function (userSetState) {
            Object.keys(userSetState).forEach(function (keyPath) {
                var _a;
                app.utils.set((_a = $page.dataset) === null || _a === void 0 ? void 0 : _a.state, keyPath, userSetState[keyPath]);
            });
        },
        _dataBinds: {},
    }, {
        get: function (obj, prop) {
            return prop === '$page' ? obj : obj[prop];
        },
    });
    return $page;
}
// 分app 和 wx 挂载app
function mountAPIs(sdks) {
    Object.keys(sdks).forEach(function (item) {
        var action = sdks[item];
        if (!(item in ACTIONS_KEY) && !(item in ROUTER_KEY)) {
            app[item] = action;
        }
    });
    return app;
}
//# sourceMappingURL=global-api.js.map