"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.asyncRemoveDepndentUrls = asyncRemoveDepndentUrls;
exports.asyncRemoveIBSheetElements = asyncRemoveIBSheetElements;
exports.asyncRemoveItemUrls = asyncRemoveItemUrls;
var lodash_1 = require("../../shared/lodash");
var dom_utils_1 = require("../../shared/dom-utils");
var constant_1 = require("../../constant");
function asyncRemoveDepndentUrls(options) {
    var dependentUrls = this.getEventOption('dependentUrls');
    var isDebugMode = (0, lodash_1.get)(options, 'debug', false);
    if ((0, lodash_1.isNil)(dependentUrls))
        return;
    var allScriptEls = (0, dom_utils_1.getElementsByTagName)('script');
    var allLinkEls = (0, dom_utils_1.getElementsByTagName)('link');
    return (0, lodash_1.castArray)(dependentUrls).map(function (sUrl) {
        return new Promise(function (resolve, reject) {
            var type = sUrl.indexOf('.css') > 0 ? 'css' : 'js';
            var bScript = type === 'js';
            var attrName = bScript ? 'src' : 'href';
            var targetList = bScript ? allScriptEls : allLinkEls;
            var ndx = (0, lodash_1.findIndex)(targetList, function (elem) {
                return (0, lodash_1.includes)(elem[attrName], sUrl);
            });
            var errMsg;
            if (ndx >= 0) {
                try {
                    var el = targetList.splice(ndx, 1)[0];
                    el.parentElement.removeChild(el);
                }
                catch (err) {
                    errMsg = (0, lodash_1.get)(err, 'message', "remove dependent url element error: ".concat(sUrl));
                }
            }
            else {
                errMsg = "not found dependent url element: ".concat(sUrl);
            }
            if (!(0, lodash_1.isNil)(errMsg)) {
                if (isDebugMode) {
                    console.warn(errMsg);
                }
                reject({ message: errMsg });
            }
            resolve(sUrl);
        });
    });
}
function asyncRemoveIBSheetElements(options, onlySheet) {
    if (onlySheet === void 0) { onlySheet = false; }
    var isDebugMode = (0, lodash_1.get)(options, 'debug', false);
    var xPathList = [
        'HEAD>DIV[id^=IBFastColumns]',
        'HEAD>DIV[id^=IBOverflowColumns]',
    ];
    if (!onlySheet) {
        xPathList = xPathList.concat([
            'BODY>.SheetMain.IBMain',
            'BODY>#IBSheetControlsSheetMain',
        ]);
    }
    return xPathList.map(function (xpath) {
        return new Promise(function (resolve, reject) {
            var success = true;
            var elems;
            try {
                elems = document.querySelectorAll(xpath) || [];
                if (elems.length) {
                    elems.forEach(function (el) {
                        var parent = el.parentElement;
                        parent.removeChild(el);
                    });
                }
                else {
                    success = false;
                }
            }
            catch (err) {
                success = false;
                reject(err);
            }
            if (isDebugMode && success) {
                console.log('# remove element:', xpath, '--', success);
            }
            resolve(elems);
        });
    });
}
function asyncRemoveItemUrls(options) {
    var urls = this.urls;
    var isDebugMode = (0, lodash_1.get)(options, 'debug', false);
    var removeOptionElements = [];
    if (this.name === constant_1.IBSHEET) {
        removeOptionElements = asyncRemoveIBSheetElements(options);
    }
    var removeUrlTasks1 = urls.map(function (uItem) {
        return new Promise(function (resolve, reject) {
            var url = uItem.value, id = uItem.id;
            var errMsg;
            var el = (0, dom_utils_1.removeElemById)(id);
            var isSuccess = !(0, lodash_1.isNil)(el);
            if (!isSuccess) {
                errMsg = "[".concat(url, "] failed to remove element in document");
            }
            if (!(0, lodash_1.isNil)(errMsg)) {
                if (isDebugMode) {
                    console.warn(errMsg);
                }
                reject({ message: errMsg });
            }
            resolve(uItem);
        });
    });
    var removeUrlTasks2 = asyncRemoveDepndentUrls.call(this, options) || [];
    var tasks = (0, lodash_1.concat)(removeOptionElements, removeUrlTasks1, removeUrlTasks2);
    return Promise.all(tasks);
}
//# sourceMappingURL=async-unload.js.map