"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 Searchable_1 = require("./Searchable");
var Package_1 = require("./Package");
// tslint:disable-next-line:no-submodule-imports
var colors_1 = require("vuetify/es5/util/colors");
var parseAuthors = require("parse-authors");
var forbiddenColors = ['shades', 'grey', 'blueGrey'];
var Crafter = /** @class */ (function (_super) {
    __extends(Crafter, _super);
    function Crafter(author) {
        var _this = _super.call(this) || this;
        _this.name = '';
        _this.email = '';
        _this.url = '';
        if (author) {
            if (typeof author === 'string') {
                var authorObject = parseAuthors(author)[0];
                Object.assign(_this, authorObject);
            }
            else {
                Object.assign(_this, author);
            }
        }
        var alreadyCreatedCrafter = Crafter.allCrafters.find(function (crafter) { return crafter.equals(_this); });
        if (alreadyCreatedCrafter) {
            return alreadyCreatedCrafter;
        }
        else {
            Crafter.allCrafters.push(_this);
        }
        return _this;
    }
    Object.defineProperty(Crafter.prototype, "initials", {
        get: function () {
            if (this.name) {
                var nameParts = this.name.split(', ');
                if (nameParts.length === 2) {
                    return "" + nameParts[1].charAt(0) + nameParts[0].charAt(0);
                }
                nameParts = this.name.split(' ');
                if (nameParts.length === 2) {
                    return "" + nameParts[0].charAt(0) + nameParts[1].charAt(0);
                }
                return this.name.charAt(0);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Crafter.prototype, "color", {
        get: function () {
            if (this.backgroundColor) {
                return this.backgroundColor;
            }
            if (Crafter.lastUsedColorNumber >= Crafter.colors.length - 1) {
                Crafter.lastUsedColorNumber = -1;
            }
            Crafter.lastUsedColorNumber++;
            var colorKey = Crafter.colors[Crafter.lastUsedColorNumber]
                .replace(/(?:^|\.?)([A-Z])/g, function (x, y) { return '-' + y.toLowerCase(); })
                .replace(/^-/, '');
            return (this.backgroundColor = colorKey);
        },
        enumerable: true,
        configurable: true
    });
    Crafter.prototype.matches = function (other, packages) {
        var _this = this;
        if (other instanceof Crafter && other.equals(this)) {
            return true;
        }
        if (other instanceof Package_1.default && other.crafters.some(function (crafter) { return crafter.equals(_this); })) {
            return true;
        }
        for (var _i = 0, packages_1 = packages; _i < packages_1.length; _i++) {
            var item = packages_1[_i];
            if (item.matches(this) && item.matches(other)) {
                return true;
            }
        }
        return false;
    };
    Crafter.prototype.getSearchItemText = function () {
        var text = [];
        if (this.name) {
            text.push(this.name, "author:" + this.name, "@" + this.name, "crafter:" + this.name, "contributor:" + this.name, "collaborator:" + this.name);
        }
        text.push(this.name || '', this.email || '', this.url || '', this.initials || '');
        return text;
    };
    Crafter.prototype.toString = function () {
        return "" + this.name;
    };
    Crafter.prototype.equals = function (other) {
        if (this.email && other.email) {
            return this.email === other.email;
        }
        return this.name === other.name && this.email === other.email && this.url === other.url;
    };
    Crafter.lastUsedColorNumber = -1;
    Crafter.colors = Object.keys(colors_1.default).filter(function (color) {
        return !forbiddenColors.some(function (forbidden) { return forbidden === color; }) && !color.startsWith('light');
    });
    Crafter.allCrafters = [];
    return Crafter;
}(Searchable_1.default));
exports.default = Crafter;
