"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.asyncImportURL = asyncImportURL;
exports.asyncImportURLs = asyncImportURLs;
exports.asyncImportItemUrls = asyncImportItemUrls;
var lodash_1 = require("../../shared/lodash");
var dom_utils_1 = require("../../shared/dom-utils");
var interface_1 = require("../../interface");
var async_test_1 = require("./async-test");
var lazyload_man_1 = require("./lazyload-man");
var ASYNC_IMPORT_URL = '[AsnycImportURL]';
function asyncImportURL(options) {
    var _this = this;
    var debug = (0, lodash_1.get)(options, 'debug', false);
    return new Promise(function (resolve, reject) {
        var _a = _this, url = _a.value, id = _a.id, target = _a.target, type = _a.type, alias = _a.alias;
        var elemData = { id: id, url: url, target: target };
        var isSuccess = false;
        var errMsg = null;
        switch (type) {
            case 'css':
                isSuccess = (0, dom_utils_1.appendCss2)(elemData, resolve, _this);
                break;
            case 'js':
                isSuccess = (0, dom_utils_1.appendJs2)(elemData, resolve, _this);
                break;
            default:
                errMsg = "[".concat(alias, "] not supported import type: ").concat(type);
        }
        if ((0, lodash_1.isNil)(errMsg) && !isSuccess) {
            errMsg = "[".concat(alias, "] failed to create element in document");
        }
        if (!(0, lodash_1.isNil)(errMsg)) {
            if (debug) {
                console.warn(errMsg);
            }
            reject({ message: errMsg });
        }
    });
}
function asyncImportURLs(aItems, lazyMan, options) {
    var debug = (0, lodash_1.get)(options, 'debug', false);
    return aItems.map(function (uItem) {
        uItem.once(interface_1.LoaderEventName.LOADED, function (evt) {
            var item = evt.target;
            if (debug) {
                console.log("%c".concat(ASYNC_IMPORT_URL, " loaded: ").concat(item.alias), 'color: black; background-color: white;');
            }
            var lazyLoadUrls = lazyMan.checkLoadableItems(item);
            if (!lazyLoadUrls.length)
                return;
            asyncImportURLs(lazyLoadUrls, lazyMan, options);
        });
        return new Promise(function (resolve, reject) {
            asyncImportURL
                .call(uItem, options)
                .then(function (tItem) {
                async_test_1.asyncItemTest
                    .call(tItem, options)
                    .then(function (item) {
                    item.emit(interface_1.LoaderEventName.LOADED, { target: item });
                    resolve(item);
                })
                    .catch(function (message) {
                    reject({ message: message });
                });
            })
                .catch(function (err) {
                reject(err.message);
            });
        });
    });
}
function asyncImportItemUrls(options) {
    var urls = !this.changed ? this.urls : this.updateUrls;
    var debug = (0, lodash_1.get)(options, 'debug', false);
    var lazyMan = new lazyload_man_1.LazyLoadURLManager();
    var aImportJsNames = urls
        .filter(function (item) { return item.type === 'js'; })
        .map(function (item) { return item.basename; });
    var preloadUrls = urls.filter(function (item) {
        if (item.type !== 'js')
            return true;
        var dependencies = item.dependencies;
        var nDependLen = dependencies.length;
        if (!nDependLen)
            return true;
        for (var i = 0; i < nDependLen; i += 1) {
            var bname = dependencies[i];
            if (!aImportJsNames.includes(bname)) {
                if (debug) {
                    console.warn("".concat(ASYNC_IMPORT_URL, " Invalid Dependencies: Not in import list!"));
                }
                return true;
            }
        }
        lazyMan.add(item);
        return false;
    });
    var asyncTasks = asyncImportURLs(preloadUrls, lazyMan, options);
    return Promise.all(asyncTasks);
}
//# sourceMappingURL=async-load.js.map