"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultsIBMapUrls = defaultsIBMapUrls;
exports.updateIBMapUrls = updateIBMapUrls;
var lodash_1 = require("../shared/lodash");
var str_utils_1 = require("../shared/str-utils");
var ibmap_1 = require("../ibmap");
var utils_1 = require("./utils");
function defaultsIBMapUrls(data) {
    try {
        var urls_1 = (0, lodash_1.get)(data, 'urls') || [];
        if (urls_1.length) {
            urls_1 = urls_1.map(function (o) { return (0, utils_1.castRegistryItemData)(o); });
        }
        var url = (0, lodash_1.get)(data, 'url');
        if (!(0, lodash_1.isNil)(url) && urls_1.length) {
            var fname = (0, str_utils_1.basename)(url);
            (0, utils_1.pushIfNotExistsUrl)(urls_1, fname);
        }
        var dependentUrls = (0, lodash_1.get)(data, 'dependentUrls');
        var lastDependentBasename_1 = null;
        if (!(0, lodash_1.isNil)(dependentUrls) && Array.isArray(dependentUrls)) {
            var prevBasename_1 = null;
            dependentUrls.forEach(function (depUrl) {
                var urlData = { url: depUrl };
                if (prevBasename_1) {
                    urlData.dependencies = [prevBasename_1];
                }
                var filename = depUrl.split('/').pop() || depUrl;
                var parts = filename.split('.');
                if (parts.length > 1) {
                    parts.pop();
                }
                prevBasename_1 = parts.join('.');
                lastDependentBasename_1 = prevBasename_1;
                urls_1.push(urlData);
            });
        }
        var mapCss = "css/ibmap.css";
        (0, utils_1.pushIfNotExistsUrl)(urls_1, mapCss);
        var mapUrlData = { url: "ibmap.js" };
        if (lastDependentBasename_1) {
            mapUrlData.dependencies = [lastDependentBasename_1];
        }
        urls_1.push(mapUrlData);
        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_1.push(license);
            }
            else {
                (0, ibmap_1.setIBMapLicense)(license);
            }
        }
        return urls_1;
    }
    catch (error) {
        console.error('Error in defaultsIBMapUrls:', error);
        throw error;
    }
}
function updateIBMapUrls(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);
    }
    var dependentUrls = (0, lodash_1.get)(data, 'dependentUrls');
    var lastDependentBasename = null;
    if (!(0, lodash_1.isNil)(dependentUrls) && Array.isArray(dependentUrls)) {
        var prevBasename_2 = null;
        dependentUrls.forEach(function (depUrl) {
            var urlData = { url: depUrl };
            if (prevBasename_2) {
                urlData.dependencies = [prevBasename_2];
            }
            var filename = depUrl.split('/').pop() || depUrl;
            var parts = filename.split('.');
            if (parts.length > 1) {
                parts.pop();
            }
            prevBasename_2 = parts.join('.');
            lastDependentBasename = prevBasename_2;
            urls.push(urlData);
        });
    }
    var mapCss = "css/ibmap.css";
    (0, utils_1.pushIfNotExistsUrl)(urls, mapCss);
    var mapUrlData = { url: "ibmap.js" };
    if (lastDependentBasename) {
        mapUrlData.dependencies = [lastDependentBasename];
    }
    urls.push(mapUrlData);
    return urls.concat(origins);
}
//# sourceMappingURL=for-ibmap.js.map