"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultsIBChartUrls = defaultsIBChartUrls;
exports.updateIBChartUrls = updateIBChartUrls;
var lodash_1 = require("../shared/lodash");
var str_utils_1 = require("../shared/str-utils");
var ibchart_1 = require("../ibchart");
var utils_1 = require("./utils");
function defaultsIBChartUrls(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 chartUrlData = { url: "ibchart.js" };
        if (lastDependentBasename_1) {
            chartUrlData.dependencies = [lastDependentBasename_1];
        }
        urls_1.push(chartUrlData);
        var chartinfoUrlData = {
            url: "ibchartinfo.js",
            dependencies: ['ibchart'],
        };
        urls_1.push(chartinfoUrlData);
        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, ibchart_1.setIBChartLicense)(license);
            }
        }
        return urls_1;
    }
    catch (error) {
        console.error('Error in defaultsIBChartUrls:', error);
        throw error;
    }
}
function updateIBChartUrls(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 chartUrlData = { url: "ibchart.js" };
    if (lastDependentBasename) {
        chartUrlData.dependencies = [lastDependentBasename];
    }
    urls.push(chartUrlData);
    var chartinfoUrlData = {
        url: "ibchartinfo.js",
        dependencies: ['ibchart'],
    };
    urls.push(chartinfoUrlData);
    return urls.concat(origins);
}
//# sourceMappingURL=for-ibchart.js.map