"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultsIBSheetUrls = defaultsIBSheetUrls;
exports.updateIBSheetUrls = updateIBSheetUrls;
exports.defaultsIBSheetEvents = defaultsIBSheetEvents;
var lodash_1 = require("../shared/lodash");
var str_utils_1 = require("../shared/str-utils");
var constant_1 = require("../constant");
var ibsheet_1 = require("../ibsheet");
var item_1 = require("./item");
var utils_1 = require("./utils");
function defaultsIBSheetUrls(data) {
    var urls = (0, lodash_1.get)(data, 'urls') || [];
    if (urls.length) {
        urls = urls.map(function (o) { return (0, utils_1.castRegistryItemData)(o); });
    }
    var url = (0, lodash_1.get)(data, 'url');
    if (!(0, lodash_1.isNil)(url) && urls.length) {
        var fname = (0, str_utils_1.basename)(url);
        (0, utils_1.pushIfNotExistsUrl)(urls, fname);
    }
    ;
    [
        { name: 'theme', def: 'default' },
        { name: 'locales', def: ['ko'] },
        { name: 'corefile', def: 'ibsheet.js' },
    ].forEach(function (o) {
        var name = o.name, def = o.def;
        var value = (0, lodash_1.get)(data, name, def);
        switch (name) {
            case 'theme': {
                if (typeof value === 'string' && !(0, str_utils_1.isFilePath)(value, 'css')) {
                    value = "css/".concat(value, "/main.css");
                }
                break;
            }
            case 'locales': {
                var values = value || [];
                if (Array.isArray(value)) {
                    values = value;
                }
                else if (typeof value === 'string') {
                    values = [value];
                }
                var locale = (0, lodash_1.get)(data, 'locale', 'ko');
                if (values.length === 0) {
                    values = [locale];
                }
                if (Array.isArray(values)) {
                    values.forEach(function (val) {
                        if (!(0, str_utils_1.isFilePath)(val, 'js')) {
                            val = "locale/".concat(val, ".js");
                        }
                        (0, utils_1.pushIfNotExistsUrl)(urls, val);
                    });
                }
                return;
            }
        }
        if (Array.isArray(value)) {
            value.forEach(function (v) { return (0, utils_1.pushIfNotExistsUrl)(urls, v); });
        }
        else {
            (0, utils_1.pushIfNotExistsUrl)(urls, value);
        }
    });
    var plugins = (0, lodash_1.get)(data, 'plugins');
    if (!(0, lodash_1.isEmpty)(plugins) && Array.isArray(plugins)) {
        (0, lodash_1.castArray)(plugins).forEach(function (val) {
            var value = val;
            if (!(0, str_utils_1.isUrlStr)(value)) {
                switch (value) {
                    default:
                        value = "plugins/ibsheet-".concat(value, ".js");
                }
            }
            (0, utils_1.pushIfNotExistsUrl)(urls, value);
        });
    }
    var license = (0, lodash_1.get)(data, 'license');
    if (!(0, lodash_1.isEmpty)(license) && typeof license === 'string') {
        if (/^https?:/.test(license) ||
            /^[./]/.test(license) ||
            /.*\.js$/.test(license)) {
            urls.push(license);
        }
        else {
            (0, ibsheet_1.setIBSheetLicense)(license);
        }
    }
    return urls;
}
function updateIBSheetUrls(originUrls, data) {
    var urls = (0, lodash_1.get)(data, 'urls') || [];
    var origins = originUrls.slice().map(function (o) { return o.value; });
    if (urls.length) {
        urls = urls.map(function (o) { return (0, utils_1.castRegistryItemData)(o); });
    }
    var url = (0, lodash_1.get)(data, 'url');
    if (!(0, lodash_1.isNil)(url) && urls.length) {
        var fname = (0, str_utils_1.basename)(url);
        (0, utils_1.pushIfNotExistsUrl)(urls, fname);
    }
    ;
    [
        { name: 'theme', def: null },
        { name: 'locales', def: null },
    ].forEach(function (o) {
        var name = o.name;
        var value = (0, lodash_1.get)(data, name);
        switch (name) {
            case 'theme': {
                if ((0, lodash_1.isNil)(value))
                    return;
                if (!(0, str_utils_1.isFilePath)(value, 'css')) {
                    value = "css/".concat(value, "/main.css");
                }
                var exists = (0, utils_1.removeByCallback)(origins, function (str) {
                    return /.*css\/.*\/main\.css/.test(str);
                });
                if (exists)
                    return;
                urls.push(value);
                return;
            }
            case 'locales': {
                var values = value || [];
                var locale = (0, lodash_1.get)(data, 'locale');
                if (!values.length) {
                    values = [locale];
                }
                if (!values.length)
                    return;
                var updateLocales = values
                    .map(function (val) {
                    if (!(0, str_utils_1.isFilePath)(val, 'js')) {
                        val = "locale/".concat(val, ".js");
                    }
                    var exists = (0, utils_1.removeByCallback)(origins, function (str) {
                        return str.indexOf(val) >= 0;
                    });
                    if (exists)
                        return;
                    urls.push(val);
                    return val;
                })
                    .filter(Boolean);
                if (updateLocales.length) {
                    (0, lodash_1.remove)(origins, function (str) { return /locale\/[^/]+\.js$/i.test(str); });
                }
                return;
            }
            default:
                return;
        }
    });
    var plugins = (0, lodash_1.get)(data, 'plugins');
    if (!(0, lodash_1.isEmpty)(plugins) && Array.isArray(plugins)) {
        (0, lodash_1.castArray)(plugins).forEach(function (plg) {
            var plugin = plg;
            switch (plugin) {
                default:
                    plugin = "plugins/ibsheet-".concat(plugin, ".js");
            }
            var exists = (0, utils_1.removeByCallback)(origins, function (val) {
                return val.indexOf(plugin) >= 0;
            });
            if (exists)
                return;
            urls.push(plugin);
        });
    }
    return urls.concat(origins);
}
function defaultsIBSheetEvents(item) {
    var CustomGlobalName = this.getUberOption('globals.ibsheet', constant_1.IBSHEET_GLOBAL);
    [
        {
            name: item_1.RegItemEventName.VALIDATE,
            callback: function () {
                return (0, ibsheet_1.existsIBSheetStatic)(CustomGlobalName);
            },
        },
        {
            name: item_1.RegItemEventName.UNLOAD,
            callback: function () {
                if (this.debug) {
                    console.log("%c[".concat(this.name, ".unload / custom] ").concat(this.alias), 'color:royalblue');
                }
                (0, ibsheet_1.destroyIBSheetStatic)(CustomGlobalName);
            },
        },
    ].forEach(function (data) {
        var name = data.name, callback = data.callback;
        if (!item.hasEventOption(name)) {
            item.setEventOption(name, callback);
        }
    });
}
//# sourceMappingURL=for-ibsheet.js.map