"use strict";
var __values = (this && this.__values) || function (o) {
    var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
    if (m) return m.call(o);
    return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spread = (this && this.__spread) || function () {
    for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("lodash");
/**
 * 说明 VAP 微服前端模块注册，仅限3.4.2后的vap版本使用，统一升级，所有模块必须更新
 */
var getUrlMap = function () {
    if (_.has(window, '__storeModuleUrl')) {
        return window['__storeModuleUrl'];
    }
    else {
        window['__storeModuleUrl'] = new Map();
        return getUrlMap();
    }
};
var getServiceMap = function () {
    if (_.has(window, '__storeModule')) {
        return window['__storeModule'];
    }
    else {
        window['__storeModule'] = new Map();
        return getServiceMap();
    }
};
var Manager = /** @class */ (function () {
    function Manager(all_groups) {
        this.ALL_GROUPS = null;
        this.ALL_GROUPS = all_groups;
    }
    Manager.prototype.getServiceIdByUrl = function (pathname) {
        var e_1, _a;
        var ALL_URLS = getUrlMap();
        var service_id = ALL_URLS.get(pathname);
        if (service_id) {
            return service_id;
        }
        for (var i = 0; i < this.ALL_GROUPS.length; i++) {
            var group = this.ALL_GROUPS[i];
            if (group.indexOf(pathname) >= 0) {
                var service_id = '';
                try {
                    for (var group_1 = (e_1 = void 0, __values(group)), group_1_1 = group_1.next(); !group_1_1.done; group_1_1 = group_1.next()) {
                        var path = group_1_1.value;
                        if (ALL_URLS.has(path)) {
                            service_id = ALL_URLS.get(path);
                            break;
                        }
                    }
                }
                catch (e_1_1) { e_1 = { error: e_1_1 }; }
                finally {
                    try {
                        if (group_1_1 && !group_1_1.done && (_a = group_1.return)) _a.call(group_1);
                    }
                    finally { if (e_1) throw e_1.error; }
                }
                if (service_id) {
                    group.map(function (path) {
                        ALL_URLS.set(path, service_id);
                    });
                    break;
                }
            }
        }
        return ALL_URLS.get(pathname);
    };
    Manager.prototype.getModelsByServiceId = function (service_id) {
        var modelsMap = getServiceMap();
        return modelsMap.get(service_id) || [];
    };
    return Manager;
}());
var default_1 = /** @class */ (function () {
    function default_1(service_id) {
        this.service_id = '';
        this.pathname = '';
        var ALL_URLS = getUrlMap();
        this.service_id = service_id;
        if (service_id == '_GLOBAL') {
            this.pathname = '_';
        }
        else {
            this.pathname = window.location.pathname;
        }
        ALL_URLS.set(this.pathname, service_id);
        // if (ALL_URLS.has(this.pathname)) {
        //     // console.error(`${this.service_id} 已经被注册了，不需要重复注册`);
        // } else {
        //     // console.log(param)
        // }
    }
    default_1.getManager = function (all_groups) {
        return new Manager(all_groups);
    };
    default_1.prototype.models = function () {
        var models = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            models[_i] = arguments[_i];
        }
        var moudlesMap = getServiceMap();
        var moudles = moudlesMap.get(this.service_id) || [];
        // @ts-ignore
        var ALL_MODELS = window['app']._models;
        var modSet = new Set();
        ALL_MODELS.map(function (item) { return modSet.add(item.namespace); });
        models.map(function (model) {
            // console.log(model.namespace)
            // 同一个 modelname / service_id 只能注册一次
            if (_.findIndex(moudles, { namespace: model.namespace }) < 0) {
                // 第一次加载前，卸载掉冲突
                if (modSet.has(model.namespace)) {
                    // console.log('卸载冲突', model.namespace)
                    window.app.unmodel(model.namespace);
                }
                moudles.push(model);
                window.app.model(model);
            }
        });
        moudlesMap.set(this.service_id, moudles);
    };
    default_1.prototype.views = function () {
        var components = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            components[_i] = arguments[_i];
        }
        window.registerComponents.apply(window, __spread(components));
    };
    default_1.prototype.nodes = function () {
        var nodes = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            nodes[_i] = arguments[_i];
        }
        window.registerNodes.apply(window, __spread(nodes));
    };
    return default_1;
}());
exports.default = default_1;
