"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Crafter_1 = require("./Crafter");
var Searchable_1 = require("./Searchable");
var Tag_1 = require("./Tag");
var Package = /** @class */ (function (_super) {
    __extends(Package, _super);
    function Package(packageMetaData) {
        var _this = _super.call(this) || this;
        Object.assign(_this, packageMetaData);
        if (!_this.distTags) {
            _this.distTags = packageMetaData['dist-tags'];
        }
        // set repositoryUrl:
        if (packageMetaData.repository) {
            _this.repositoryUrl =
                typeof packageMetaData.repository === 'string'
                    ? packageMetaData.repository
                    : packageMetaData.repository.url;
        }
        // set bugsUrl:
        if (packageMetaData.bugs) {
            _this.bugTrackerUrl =
                typeof packageMetaData.bugs === 'string' ? packageMetaData.bugs : packageMetaData.bugs.url;
        }
        // Count depenedencies:
        var dependencies = packageMetaData.dependencies;
        var devDependencies = packageMetaData.devDependencies;
        var dependenciesCount = dependencies ? Object.keys(dependencies).length : 0;
        var devDependenciesCount = devDependencies ? Object.keys(devDependencies).length : 0;
        _this.dependenciesCount = dependenciesCount + devDependenciesCount;
        // set scope:
        var packageNameParts = packageMetaData.name.split('/');
        if (packageNameParts.length > 1) {
            _this.scope = packageNameParts[0];
        }
        _this.craftersList = [];
        _this.tagList = [];
        return _this;
    }
    Package.prototype.matches = function (other) {
        if (other instanceof Tag_1.Tag) {
            return this.tags.some(function (tag) { return tag.value === other.value; });
        }
        if (other instanceof Crafter_1.default) {
            return this.crafters.some(function (crafter) { return crafter.equals(other); });
        }
        if (other instanceof Package) {
            return other.name === this.name && other.version === this.version;
        }
        return false;
    };
    Package.prototype.getSearchItemText = function () {
        var _a, _b;
        return (_a = (_b = [
            this.name,
            "package:" + this.name,
            this.description || '',
            this.author ? this.author.toString() : '',
        ]).concat.apply(_b, this.tags.map(function (tag) { return tag.getSearchItemText(); }))).concat.apply(_a, this.crafters.map(function (crafter) { return crafter.getSearchItemText(); }));
    };
    Object.defineProperty(Package.prototype, "crafters", {
        get: function () {
            if (this.craftersList.length) {
                return this.craftersList;
            }
            if (this.author) {
                this.craftersList.push(new Crafter_1.default(this.author));
            }
            if (this.contributors) {
                for (var _i = 0, _a = this.contributors; _i < _a.length; _i++) {
                    var contributor = _a[_i];
                    this.craftersList.push(new Crafter_1.default(contributor));
                }
            }
            return this.craftersList;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Package.prototype, "tags", {
        get: function () {
            var _a;
            if (this.tagList.length) {
                return this.tagList;
            }
            if (this.keywords && this.keywords.length) {
                (_a = this.tagList).push.apply(_a, this.keywords.map(function (keyword) { return new Tag_1.Tag(keyword); }));
            }
            return this.tagList;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Package.prototype, "repositoryName", {
        get: function () {
            if (this.repositoryUrl) {
                return this.url2Name(this.repositoryUrl);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Package.prototype, "bugTrackerName", {
        get: function () {
            if (this.bugTrackerUrl) {
                return this.url2Name(this.bugTrackerUrl);
            }
        },
        enumerable: true,
        configurable: true
    });
    Package.prototype.toString = function () {
        return this.name;
    };
    Package.prototype.url2Name = function (url) {
        if (url.includes('github')) {
            return 'github';
        }
        if (url.includes('gitlab')) {
            return 'gitlab';
        }
        return url.split('/')[2];
    };
    return Package;
}(Searchable_1.default));
exports.default = Package;
