"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFilenameFromURL = void 0;
exports.castRegistryItemData = castRegistryItemData;
exports.castRegistryAlias = castRegistryAlias;
exports.getRegistryIdentifier = getRegistryIdentifier;
exports.generateVersion = generateVersion;
exports.pushIfNotExistsUrl = pushIfNotExistsUrl;
exports.removeByCallback = removeByCallback;
var url_1 = require("url");
var lodash_1 = require("../shared/lodash");
var config_1 = require("../config");
function castRegistryItemData(param) {
    if ((0, lodash_1.isString)(param)) {
        return { url: param };
    }
    return param;
}
var getFilenameFromURL = function (url) {
    if ((0, lodash_1.isNil)(url))
        return;
    var pathname = (0, url_1.parse)(url).pathname;
    if ((0, lodash_1.isNil)(pathname)) {
        console.warn('[UrlParser]', "".concat(url, " failed parse basename"));
        return;
    }
    return (0, lodash_1.last)(pathname.split('/'));
};
exports.getFilenameFromURL = getFilenameFromURL;
function castRegistryAlias(data) {
    var idf = getRegistryIdentifier(data);
    if ((0, lodash_1.isNil)(idf))
        return;
    return idf.alias;
}
function getRegistryIdentifier(data) {
    var name = (0, lodash_1.get)(data, 'name');
    var url = (0, lodash_1.get)(data, 'url');
    if ((0, lodash_1.isNil)(name) && !(0, lodash_1.isNil)(url)) {
        name = (0, exports.getFilenameFromURL)(url);
    }
    if ((0, lodash_1.isNil)(name) || !name.length)
        return;
    name = (0, lodash_1.trim)(name);
    var res = {
        name: name,
        alias: name,
    };
    var version = (0, lodash_1.get)(data, 'version');
    if (!(0, lodash_1.isNil)(version)) {
        version = (0, lodash_1.trim)(version);
        (0, lodash_1.set)(res, 'version', version);
        (0, lodash_1.set)(res, 'alias', [name, version].join('@'));
    }
    return res;
}
function generateVersion(item) {
    var ver = item.version;
    if ((0, lodash_1.isNil)(ver)) {
        return '' + config_1.VERSION_GENERATE_START_NUM;
    }
    if (ver.indexOf('-') < 0) {
        return "".concat(ver, "-").concat(config_1.VERSION_GENERATE_START_NUM);
    }
    var arr = ver.split('-');
    var num = arr.pop();
    if (!(0, lodash_1.isNumber)(num)) {
        return "".concat(ver, "-").concat(config_1.VERSION_GENERATE_START_NUM);
    }
    return "".concat(arr.join('-'), "-").concat((0, lodash_1.toNumber)(num) + 1);
}
function pushIfNotExistsUrl(urls, value) {
    var exists = (0, lodash_1.find)(urls, function (o) {
        var url = o.url;
        return url.indexOf(value) > -1;
    });
    if ((0, lodash_1.isNil)(exists)) {
        urls.push({ url: value });
    }
}
function removeByCallback(urls, callback) {
    var res = false;
    if (!urls.length)
        return false;
    urls = urls
        .map(function (url) {
        var bool = callback(url);
        if (bool) {
            if (!res)
                res = true;
            return;
        }
        return url;
    })
        .filter(Boolean);
    return res;
}
//# sourceMappingURL=utils.js.map