"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
    return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IBSheetLoaderStatic = void 0;
var custom_1 = require("./custom");
var lodash_1 = require("./shared/lodash");
var dom_utils_1 = require("./shared/dom-utils");
var interval_manager_1 = require("./shared/interval-manager");
var async_unload_1 = require("./registry/item/async-unload");
var task_man_1 = require("./task-man");
var modules_1 = require("./modules");
var ibsheet_1 = require("./ibsheet");
var ibchart_1 = require("./ibchart");
var ibmap_1 = require("./ibmap");
var constant_1 = require("./constant");
var config_1 = require("./config");
var registry_1 = require("./registry");
var interface_1 = require("./interface");
var IBSheetLoaderStatic = (function (_super) {
    __extends(IBSheetLoaderStatic, _super);
    function IBSheetLoaderStatic() {
        var _this = _super.call(this) || this;
        _this._status = interface_1.LoaderStatus.PENDING;
        _this._ready = false;
        _this._ibsheet = ibsheet_1.IBSheet8GlobalInstance;
        _this._ibchart = ibchart_1.IBChartGlobalInstance;
        _this._ibmap = ibmap_1.IBMapGlobalInstance;
        _this._options = (0, lodash_1.clone)(config_1.DefaultLoaderConfig);
        _this.registry = new registry_1.LoaderRegistry(_this);
        _this._initTasksManagers();
        (0, dom_utils_1.documentReady)(function () {
            _this._ready = true;
            _this._status = interface_1.LoaderStatus.IDLE;
        });
        return _this;
    }
    Object.defineProperty(IBSheetLoaderStatic.prototype, "debug", {
        get: function () {
            return this.getOption('debug', false);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IBSheetLoaderStatic.prototype, "version", {
        get: function () {
            return constant_1.APP_VERSION;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IBSheetLoaderStatic.prototype, "ready", {
        get: function () {
            return this._ready;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IBSheetLoaderStatic.prototype, "status", {
        get: function () {
            return this._status;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IBSheetLoaderStatic.prototype, "options", {
        get: function () {
            return (0, lodash_1.clone)(this._options);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IBSheetLoaderStatic.prototype, "loadedDefaultLib", {
        get: function () {
            var item = this._getDefaultRegItem(false);
            if ((0, lodash_1.isNil)(item))
                return false;
            return item.loaded;
        },
        enumerable: false,
        configurable: true
    });
    IBSheetLoaderStatic.prototype._getDefaultRegItem = function (throwError) {
        if (throwError === void 0) { throwError = true; }
        var item = this.registry.findOne(constant_1.IBSHEET);
        var item2 = this.registry.findOne(constant_1.IBCHART);
        var item3 = this.registry.findOne(constant_1.IBMAP);
        if (throwError && (0, lodash_1.isNil)(item) && (0, lodash_1.isNil)(item2) && (0, lodash_1.isNil)(item3)) {
            throw new Error("not found registration data for ".concat(constant_1.IBSHEET, " library or ").concat(constant_1.IBCHART, " library or ").concat(constant_1.IBMAP, " library"));
        }
        return (item ||
            item2 ||
            item3);
    };
    IBSheetLoaderStatic.prototype._initTasksManagers = function () {
        var createTaskMan = (0, lodash_1.bind)(task_man_1.createTaskManager, this);
        this._loadTaskMan = createTaskMan(task_man_1.LoaderTaskType.LOAD, this);
        this._unloadTaskMan = createTaskMan(task_man_1.LoaderTaskType.UNLOAD, this);
    };
    IBSheetLoaderStatic.prototype.config = function (options) {
        var _this = this;
        var loaderOpts;
        if (!(0, lodash_1.isNil)(options)) {
            loaderOpts = (0, lodash_1.pick)(options, [
                'autoload',
                'debug',
                'retry',
                'globals',
                'preset',
            ]);
            this._options = (0, lodash_1.defaultsDeep)(loaderOpts, this._options);
            var sheetGlobal = (0, lodash_1.get)(loaderOpts, 'globals.ibsheet');
            if (sheetGlobal) {
                this._ibsheet.setGlobalName(sheetGlobal);
            }
            var chartGlobal = (0, lodash_1.get)(loaderOpts, 'globals.ibchart');
            if (chartGlobal) {
                this._ibchart.setGlobalName(chartGlobal);
            }
            var regOpts = (0, lodash_1.get)(options, 'registry');
            if (!(0, lodash_1.isNil)(regOpts)) {
                this.registry.addAll(regOpts, true);
            }
            var readyCallback_1 = (0, lodash_1.get)(options, 'ready');
            if (!(0, lodash_1.isNil)(readyCallback_1)) {
                (0, dom_utils_1.documentReady)(function () { return readyCallback_1.call(_this); });
            }
        }
        if (this.debug) {
            this.intervalMan = new interval_manager_1.IntervalManager(window, loaderOpts);
        }
        return this;
    };
    IBSheetLoaderStatic.prototype.getOption = function (sPath, def) {
        return (0, lodash_1.get)(this.options, sPath, def);
    };
    IBSheetLoaderStatic.prototype.info = function (alias) {
        return this.registry.info(alias);
    };
    IBSheetLoaderStatic.prototype.list = function () {
        var _this = this;
        return this.registry.list().map(function (alias) {
            var item = _this.registry.get(alias);
            return {
                alias: alias,
                loaded: item.loaded,
            };
        });
    };
    IBSheetLoaderStatic.prototype.getIBSheetStatic = function () {
        return this._ibsheet.global;
    };
    IBSheetLoaderStatic.prototype.getIBChartStatic = function () {
        return this._ibchart.global;
    };
    IBSheetLoaderStatic.prototype.load = function (arg, alsoDefaultLib) {
        var _this = this;
        var taskMan = this._loadTaskMan;
        if ((0, lodash_1.isNil)(alsoDefaultLib)) {
            alsoDefaultLib = this.options.autoload;
        }
        var aLoadItems = modules_1.getLoadItems.apply(this, [arg, alsoDefaultLib]);
        var tasks = aLoadItems
            .map(function (item) {
            if (item.changed) {
                var alias_1 = item.alias;
                if (item.loaded) {
                    _this.reload(alias_1);
                    return;
                }
                else if (taskMan.exists(item)) {
                    item.resolveUpdateUrls(function () { return _this.reload(alias_1); });
                    return;
                }
            }
            return taskMan.add(item);
        })
            .filter(Boolean);
        if (!tasks.length) {
            return this;
        }
        taskMan.start();
        return this;
    };
    IBSheetLoaderStatic.prototype.createChart = function (options) {
        var _this = this;
        var opts = {};
        var chartOpts = {};
        var ibchart = this._ibchart;
        [
            { key: 'id' },
            { key: 'cont', alias: ['el'] },
            { key: 'options', alias: ['config'] },
        ].forEach(function (o) {
            var key = o.key;
            (0, lodash_1.concat)(key, (0, lodash_1.get)(o, 'alias'))
                .filter(Boolean)
                .forEach(function (prop) {
                if ((0, lodash_1.has)(options, prop)) {
                    chartOpts[key] = (0, lodash_1.get)(options, prop);
                }
            });
        });
        if (!(0, lodash_1.has)(chartOpts, 'id')) {
            throw new Error('IBChart create error: need "id" property.');
        }
        else if (!(0, lodash_1.has)(chartOpts, 'cont') && !(0, lodash_1.has)(chartOpts, 'el')) {
            throw new Error('IBChart create error: need "el" or "cont" property.');
        }
        if ((0, lodash_1.has)(chartOpts, 'options')) {
            if (typeof chartOpts.options === 'object') {
                opts = (0, lodash_1.defaultsDeep)(opts, chartOpts.options);
            }
        }
        opts.id = chartOpts.id;
        opts.cont = chartOpts.cont;
        var createFn = (0, lodash_1.bind)(ibchart.create, ibchart);
        var createEvtData = { target: ibchart.global, data: opts };
        return new Promise(function (resolve, reject) {
            ;
            (function () { return __awaiter(_this, void 0, void 0, function () {
                var chart, defItem, err_1;
                var _this = this;
                return __generator(this, function (_a) {
                    switch (_a.label) {
                        case 0:
                            _a.trys.push([0, 3, , 4]);
                            if (!this.loadedDefaultLib) return [3, 2];
                            this.emit(interface_1.LoaderEventName.CREATE_CHART, createEvtData);
                            return [4, createFn(opts)];
                        case 1:
                            chart = _a.sent();
                            this.emit(interface_1.LoaderEventName.CREATED_CHART, { target: chart });
                            return [2, resolve(chart)];
                        case 2:
                            defItem = this._getDefaultRegItem();
                            defItem.once(interface_1.LoaderEventName.LOADED, function () { return __awaiter(_this, void 0, void 0, function () {
                                var chart, err_2;
                                return __generator(this, function (_a) {
                                    switch (_a.label) {
                                        case 0:
                                            _a.trys.push([0, 2, , 3]);
                                            this.emit(interface_1.LoaderEventName.CREATE_CHART, createEvtData);
                                            return [4, createFn(opts)];
                                        case 1:
                                            chart = _a.sent();
                                            this.emit(interface_1.LoaderEventName.CREATED_CHART, { target: chart });
                                            resolve(chart);
                                            return [3, 3];
                                        case 2:
                                            err_2 = _a.sent();
                                            this.emit(interface_1.LoaderEventName.CREATE_CHART_FAILED, (0, lodash_1.assignIn)(createEvtData, { error: err_2 }));
                                            reject(err_2);
                                            return [3, 3];
                                        case 3: return [2];
                                    }
                                });
                            }); });
                            this.load('ibchart', true);
                            return [3, 4];
                        case 3:
                            err_1 = _a.sent();
                            this.emit(interface_1.LoaderEventName.CREATE_CHART_FAILED, (0, lodash_1.assignIn)(createEvtData, { error: err_1 }));
                            reject(err_1);
                            return [3, 4];
                        case 4: return [2];
                    }
                });
            }); })();
        });
    };
    IBSheetLoaderStatic.prototype.removeChart = function (sid) {
        if (!this.loadedDefaultLib)
            return;
        var ibchartstatic = window[constant_1.IBCHART_GLOBAL_OBJECT];
        var target = ibchartstatic[sid];
        if ((0, lodash_1.isNil)(target)) {
            if (this.debug) {
                console.warn('not found target chart:', sid);
            }
            return;
        }
        this.emit(interface_1.LoaderEventName.REMOVE_CHART, { target: target });
        try {
            target.dispose();
            delete ibchartstatic[sid];
            ibchartstatic.forEach(function (chart, index) {
                if (chart.getId() === sid) {
                    ibchartstatic.splice(index, 1);
                }
            });
            this.emit(interface_1.LoaderEventName.REMOVED_CHART, {
                target: ibchartstatic,
                data: { id: sid },
            });
        }
        catch (err) {
            console.error(err);
            this.emit(interface_1.LoaderEventName.REMOVE_CHART_FAILED, {
                target: ibchartstatic,
                error: err,
            });
        }
    };
    IBSheetLoaderStatic.prototype.createMap = function (options) {
        var _this = this;
        var opts = {};
        var mapOpts = {};
        var ibmap = this._ibmap;
        [
            { key: 'elem', alias: ['el'] },
            { key: 'cfg', alias: ['options'] },
        ].forEach(function (o) {
            var key = o.key;
            (0, lodash_1.concat)(key, (0, lodash_1.get)(o, 'alias'))
                .filter(Boolean)
                .forEach(function (prop) {
                if ((0, lodash_1.has)(options, prop)) {
                    mapOpts[key] = (0, lodash_1.get)(options, prop);
                }
            });
        });
        if (!(0, lodash_1.has)(mapOpts, 'elem') && !(0, lodash_1.has)(mapOpts, 'el')) {
            throw new Error('IBMap create error: need "el" or "elem" property.');
        }
        else if (!(0, lodash_1.has)(mapOpts, 'cfg') && !(0, lodash_1.has)(mapOpts, 'options')) {
            throw new Error('IBMap create error: need "options" or "cfg" property.');
        }
        if ((0, lodash_1.has)(mapOpts, 'cfg')) {
            if (typeof mapOpts.cfg === 'object') {
                opts['cfg'] = (0, lodash_1.defaultsDeep)(opts['cfg'], mapOpts.cfg);
            }
        }
        opts.elem = mapOpts.elem;
        var createFn = (0, lodash_1.bind)(ibmap.createMap, ibmap);
        var createEvtData = { target: ibmap.global, data: opts };
        return new Promise(function (resolve, reject) {
            ;
            (function () { return __awaiter(_this, void 0, void 0, function () {
                var map, defItem, err_3;
                var _this = this;
                return __generator(this, function (_a) {
                    switch (_a.label) {
                        case 0:
                            _a.trys.push([0, 3, , 4]);
                            if (!this.loadedDefaultLib) return [3, 2];
                            this.emit(interface_1.LoaderEventName.CREATE_MAP, createEvtData);
                            return [4, createFn(opts)];
                        case 1:
                            map = _a.sent();
                            this.emit(interface_1.LoaderEventName.CREATED_MAP, { target: map });
                            return [2, resolve(map)];
                        case 2:
                            defItem = this._getDefaultRegItem();
                            defItem.once(interface_1.LoaderEventName.LOADED, function () { return __awaiter(_this, void 0, void 0, function () {
                                var map, err_4;
                                return __generator(this, function (_a) {
                                    switch (_a.label) {
                                        case 0:
                                            _a.trys.push([0, 2, , 3]);
                                            this.emit(interface_1.LoaderEventName.CREATE_MAP, createEvtData);
                                            return [4, createFn(opts)];
                                        case 1:
                                            map = _a.sent();
                                            this.emit(interface_1.LoaderEventName.CREATED_MAP, { target: map });
                                            resolve(map);
                                            return [3, 3];
                                        case 2:
                                            err_4 = _a.sent();
                                            this.emit(interface_1.LoaderEventName.CREATE_MAP_FAILED, (0, lodash_1.assignIn)(createEvtData, { error: err_4 }));
                                            reject(err_4);
                                            return [3, 3];
                                        case 3: return [2];
                                    }
                                });
                            }); });
                            this.load('ibmap', true);
                            return [3, 4];
                        case 3:
                            err_3 = _a.sent();
                            this.emit(interface_1.LoaderEventName.CREATE_MAP_FAILED, (0, lodash_1.assignIn)(createEvtData, { error: err_3 }));
                            reject(err_3);
                            return [3, 4];
                        case 4: return [2];
                    }
                });
            }); })();
        });
    };
    IBSheetLoaderStatic.prototype.removeMap = function (oMap) {
        if (!this.loadedDefaultLib)
            return;
        var target = oMap;
        if ((0, lodash_1.isEmpty)(target) && (0, lodash_1.isNil)(target)) {
            if (this.debug) {
                console.warn('not found target map:', target);
            }
            return;
        }
        this.emit(interface_1.LoaderEventName.REMOVE_MAP, { target: target });
        try {
            target.dispose();
            this.emit(interface_1.LoaderEventName.REMOVED_MAP, {
                target: target,
            });
        }
        catch (err) {
            console.error(err);
            this.emit(interface_1.LoaderEventName.REMOVE_MAP_FAILED, {
                target: target,
                error: err,
            });
        }
    };
    IBSheetLoaderStatic.prototype.createSheet = function (options) {
        var _this = this;
        var sheetOpts = {};
        var ibsheet = this._ibsheet;
        [
            { key: 'id' },
            { key: 'el', alias: ['elementId'] },
            { key: 'options', alias: ['config'] },
            { key: 'data' },
            { key: 'sync' },
        ].forEach(function (o) {
            var key = o.key;
            (0, lodash_1.concat)(key, (0, lodash_1.get)(o, 'alias'))
                .filter(Boolean)
                .forEach(function (prop) {
                if ((0, lodash_1.has)(options, prop)) {
                    sheetOpts[key] = (0, lodash_1.get)(options, prop);
                }
            });
        });
        if (!(0, lodash_1.has)(sheetOpts, 'id')) {
            sheetOpts.id = (0, ibsheet_1.generateSheetID)();
        }
        if (!(0, lodash_1.has)(sheetOpts, 'el') && (0, lodash_1.has)(options, 'element')) {
            sheetOpts.el = (function (el) {
                var sid = el.getAttribute('id');
                if ((0, lodash_1.isNil)(sid)) {
                    sid = (0, ibsheet_1.generateElementID)();
                    if (_this.debug) {
                        console.log('generated element id:', sid);
                    }
                    el.setAttribute('id', sid);
                }
                return sid;
            })((0, lodash_1.get)(options, 'element'));
        }
        var sheetRenderFirstFinish = function (sheet, mainResolve) {
            return new Promise(function (resolve) {
                sheet.OnAfterRenderFirstFinish = function (callback) {
                    mainResolve(this);
                    resolve(this);
                    if (callback)
                        callback();
                };
            });
        };
        var setloaderPreset = function () {
            return new Promise(function (resolve) {
                var obj = {};
                for (var ws in window) {
                    if (ws.startsWith('IB_')) {
                        obj[ws] = window[ws];
                    }
                }
                resolve(obj);
            });
        };
        var createFn = (0, lodash_1.bind)(ibsheet.create, ibsheet);
        var createEvtData = { target: ibsheet.global, data: sheetOpts };
        return new Promise(function (resolve, reject) {
            ;
            (function () { return __awaiter(_this, void 0, void 0, function () {
                var _a, sheet, _b, defItem, err_5;
                var _this = this;
                return __generator(this, function (_c) {
                    switch (_c.label) {
                        case 0:
                            _c.trys.push([0, 6, , 7]);
                            if (!this.loadedDefaultLib) return [3, 5];
                            _a = this;
                            return [4, setloaderPreset()];
                        case 1:
                            _a.loaderPreset = _c.sent();
                            this.emit(interface_1.LoaderEventName.CREATE_SHEET, createEvtData);
                            return [4, createFn(sheetOpts)];
                        case 2:
                            sheet = _c.sent();
                            this.emit(interface_1.LoaderEventName.CREATED_SHEET, { target: sheet });
                            if (!sheet.LoaderCreateDelay) return [3, 4];
                            _b = resolve;
                            return [4, sheetRenderFirstFinish(sheet, resolve)];
                        case 3: return [2, _b.apply(void 0, [_c.sent()])];
                        case 4: return [2, resolve(sheet)];
                        case 5:
                            defItem = this._getDefaultRegItem();
                            defItem.once(interface_1.LoaderEventName.LOADED, function () { return __awaiter(_this, void 0, void 0, function () {
                                var _a, sheet, err_6;
                                return __generator(this, function (_b) {
                                    switch (_b.label) {
                                        case 0:
                                            _b.trys.push([0, 3, , 4]);
                                            _a = this;
                                            return [4, setloaderPreset()];
                                        case 1:
                                            _a.loaderPreset = _b.sent();
                                            this.emit(interface_1.LoaderEventName.CREATE_SHEET, createEvtData);
                                            return [4, createFn(sheetOpts)];
                                        case 2:
                                            sheet = _b.sent();
                                            this.emit(interface_1.LoaderEventName.CREATED_SHEET, { target: sheet });
                                            resolve(sheet);
                                            return [3, 4];
                                        case 3:
                                            err_6 = _b.sent();
                                            this.emit(interface_1.LoaderEventName.CREATE_SHEET_FAILED, (0, lodash_1.assignIn)(createEvtData, { error: err_6 }));
                                            reject(err_6);
                                            return [3, 4];
                                        case 4: return [2];
                                    }
                                });
                            }); });
                            this.load('ibsheet', true);
                            return [3, 7];
                        case 6:
                            err_5 = _c.sent();
                            this.emit(interface_1.LoaderEventName.CREATE_SHEET_FAILED, (0, lodash_1.assignIn)(createEvtData, { error: err_5 }));
                            reject(err_5);
                            return [3, 7];
                        case 7: return [2];
                    }
                });
            }); })();
        });
    };
    IBSheetLoaderStatic.prototype.removeSheet = function (sid) {
        var _this = this;
        if (!this.loadedDefaultLib)
            return;
        var ibsheetStatic = this.getIBSheetStatic();
        var target = ibsheetStatic[sid];
        if ((0, lodash_1.isNil)(target)) {
            if (this.debug) {
                console.warn('not found target sheet:', sid);
            }
            return;
        }
        this.emit(interface_1.LoaderEventName.REMOVE_SHEET, { target: target });
        try {
            target.dispose();
            (0, async_unload_1.asyncRemoveIBSheetElements)(this.options, true);
            setTimeout(function () {
                _this.emit(interface_1.LoaderEventName.REMOVED_SHEET, {
                    target: ibsheetStatic,
                    data: { id: sid },
                });
            }, 10);
        }
        catch (err) {
            console.error(err);
            this.emit(interface_1.LoaderEventName.REMOVE_SHEET_FAILED, {
                target: ibsheetStatic,
                error: err,
            });
        }
    };
    IBSheetLoaderStatic.prototype.sheetReady = function (callback) {
        var _this = this;
        if (this.loadedDefaultLib) {
            return Promise.resolve(this.getIBSheetStatic());
        }
        return new Promise(function (resolve, reject) {
            try {
                var defItem = _this._getDefaultRegItem();
                defItem.once(interface_1.LoaderEventName.LOADED, function () {
                    var ibsheetStatic = _this.getIBSheetStatic();
                    try {
                        if (!(0, lodash_1.isNil)(callback)) {
                            callback.call(ibsheetStatic, ibsheetStatic);
                        }
                        resolve(ibsheetStatic);
                    }
                    catch (err) {
                        reject(err);
                    }
                });
                _this.load();
            }
            catch (err) {
                reject(err);
            }
        }).catch(function (err) {
            throw new Error(err);
        });
    };
    IBSheetLoaderStatic.prototype.reload = function (arg) {
        var _this = this;
        if ((0, lodash_1.isNil)(arg)) {
            var item = this._getDefaultRegItem(false);
            if ((0, lodash_1.isNil)(item))
                return this;
            arg = item.alias;
        }
        (0, lodash_1.castArray)(arg).forEach(function (alias) {
            var item = _this.registry.findOne(alias);
            if ((0, lodash_1.isNil)(item)) {
                if (_this.debug) {
                    console.warn("not found item: ".concat(alias));
                }
                return;
            }
            if (item.loaded) {
                item.once(interface_1.LoaderEventName.UNLOADED, function (evt) {
                    var target = evt.target;
                    var tAlias = target.alias;
                    if (_this.debug) {
                        console.log("%c[IBSheetLoader] reload start - ".concat(tAlias), 'background-color:green;color:white');
                    }
                    _this.load(tAlias, false);
                });
                _this.unload(alias);
                return;
            }
            _this.load(alias, false);
        });
        return this;
    };
    IBSheetLoaderStatic.prototype.unload = function (params) {
        var _this = this;
        var registry = this.registry;
        var taskMan = this._unloadTaskMan;
        var sheetLib = this._getDefaultRegItem();
        var defaultAlias = sheetLib.raw.alias;
        if (sheetLib.loaded && !taskMan.exists(sheetLib) && (0, lodash_1.isNil)(params)) {
            params = [defaultAlias];
        }
        if ((0, lodash_1.isNil)(params)) {
            return this;
        }
        var tasks = (0, lodash_1.castArray)(params)
            .map(function (data) {
            var item;
            if ((0, lodash_1.isString)(data)) {
                item = registry.get(data);
            }
            if ((0, lodash_1.isNil)(item)) {
                console.warn("invalid paramater: ".concat(data));
                return;
            }
            if (!item.loaded) {
                if (_this.debug) {
                    console.warn("already unloaded library: ".concat(item.alias));
                }
                return;
            }
            return taskMan.add(item);
        })
            .filter(Boolean);
        if (!tasks.length) {
            return this;
        }
        taskMan.start();
        return this;
    };
    IBSheetLoaderStatic.prototype.reset = function () {
        return this;
    };
    return IBSheetLoaderStatic;
}(custom_1.CustomEventEmitter));
exports.IBSheetLoaderStatic = IBSheetLoaderStatic;
//# sourceMappingURL=main.js.map