"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPageApi = exports.removeGlboalApi = exports.registerApp = exports.app = exports.subPackageName = void 0;
var tslib_1 = require("tslib");
var app_h5_sdk_1 = (0, tslib_1.__importDefault)(require("@tcwd/weapps-sdk/lib/app-h5-sdk"));
var weda_client_1 = require("@cloudbase/weda-client");
var index_1 = require("../utils/adapter/index");
var initGlobalVar_1 = require("../utils/initGlobalVar");
var EXTRA_API = weda_client_1.WEDA_CLOUD_SDK.EXTRA_API, createDataset = weda_client_1.WEDA_CLOUD_SDK.createDataset;
var EPLATFORM;
(function (EPLATFORM) {
    EPLATFORM["WEB"] = "WEB";
    /**
     * 拼写错误
     * 兼容性无法修改
     */
    EPLATFORM["MINIPROGRAM"] = "MINIPROGRAME";
})(EPLATFORM || (EPLATFORM = {}));
exports.subPackageName = '';
exports.app = createGlboalApi();
function registerApp(_app) {
    if (_app === void 0) { _app = createGlboalApi(); }
    _injectApp2Runtime(_app, true);
    exports.app = _app;
}
exports.registerApp = registerApp;
mountAPIs(app_h5_sdk_1.default);
function createGlboalApi(initData) {
    if (initData === void 0) { initData = {}; }
    var webApp = (0, weda_client_1.createWebApp)(initData);
    var globalAPI = new Proxy((0, tslib_1.__assign)((0, tslib_1.__assign)({ id: initData.id || '', domain: initData.domain || '', state: {}, computed: {}, dataset: {}, common: {}, enumOptions: {} }, webApp), { utils: (0, tslib_1.__assign)((0, tslib_1.__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]
                    : (0, initGlobalVar_1.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 exports.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 (exports.subPackageName) {
        globalAPI.mainApp = index_1.runtime["$app"];
        index_1.runtime["$$subapp_" + exports.subPackageName] = globalAPI;
    }
    else {
        // 预览区调试栏可调试
        // 暴露给复合组件，代码组件来引用
        index_1.runtime['app'] = hard ? globalAPI : index_1.runtime['app'] || globalAPI;
        index_1.runtime["$app"] = index_1.runtime['app'];
    }
}
function removeGlboalApi() {
    if (exports.subPackageName) {
        index_1.runtime["$$subapp_" + exports.subPackageName] = undefined;
    }
    else {
        index_1.runtime['app'] = undefined;
        index_1.runtime["$app"] = undefined;
    }
}
exports.removeGlboalApi = removeGlboalApi;
function createPageApi() {
    var $page = new Proxy({
        state: {},
        computed: {},
        handler: {},
        props: {},
        widgets: {},
        dataset: {},
        setState: function (userSetState) {
            Object.keys(userSetState).forEach(function (keyPath) {
                var _a;
                exports.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;
}
exports.createPageApi = createPageApi;
// 分app 和 wx 挂载app
function mountAPIs(sdks) {
    Object.keys(sdks).forEach(function (item) {
        var action = sdks[item];
        if (!(item in weda_client_1.ACTIONS_KEY) && !(item in weda_client_1.ROUTER_KEY)) {
            exports.app[item] = action;
        }
    });
    return exports.app;
}
//# sourceMappingURL=global-api.js.map