"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GlobalRegistry = exports.CardObjectRegistry = void 0;
var serialization_1 = require("./serialization");
var CardObjectRegistry = /** @class */ (function () {
    function CardObjectRegistry() {
        this._items = {};
    }
    CardObjectRegistry.prototype.findByName = function (typeName) {
        return this._items.hasOwnProperty(typeName) ? this._items[typeName] : undefined;
    };
    CardObjectRegistry.prototype.clear = function () {
        this._items = {};
    };
    CardObjectRegistry.prototype.copyTo = function (target) {
        var keys = Object.keys(this._items);
        for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
            var key = keys_1[_i];
            var typeRegistration = this._items[key];
            target.register(typeRegistration.typeName, typeRegistration.objectType, typeRegistration.schemaVersion);
        }
    };
    CardObjectRegistry.prototype.register = function (typeName, objectType, schemaVersion) {
        if (schemaVersion === void 0) { schemaVersion = serialization_1.Versions.v1_0; }
        var registrationInfo = this.findByName(typeName);
        if (registrationInfo !== undefined) {
            registrationInfo.objectType = objectType;
        }
        else {
            registrationInfo = {
                typeName: typeName,
                objectType: objectType,
                schemaVersion: schemaVersion
            };
        }
        this._items[typeName] = registrationInfo;
    };
    CardObjectRegistry.prototype.unregister = function (typeName) {
        delete this._items[typeName];
    };
    CardObjectRegistry.prototype.createInstance = function (typeName, targetVersion) {
        var registrationInfo = this.findByName(typeName);
        return registrationInfo && registrationInfo.schemaVersion.compareTo(targetVersion) <= 0
            ? new registrationInfo.objectType()
            : undefined;
    };
    CardObjectRegistry.prototype.getItemCount = function () {
        return Object.keys(this._items).length;
    };
    CardObjectRegistry.prototype.getItemAt = function (index) {
        var _this = this;
        return Object.keys(this._items).map(function (e) { return _this._items[e]; })[index];
    };
    return CardObjectRegistry;
}());
exports.CardObjectRegistry = CardObjectRegistry;
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
var GlobalRegistry = /** @class */ (function () {
    function GlobalRegistry() {
    }
    GlobalRegistry.populateWithDefaultElements = function (registry) {
        registry.clear();
        GlobalRegistry.defaultElements.copyTo(registry);
    };
    GlobalRegistry.populateWithDefaultActions = function (registry) {
        registry.clear();
        GlobalRegistry.defaultActions.copyTo(registry);
    };
    Object.defineProperty(GlobalRegistry, "elements", {
        get: function () {
            if (!GlobalRegistry._elements) {
                GlobalRegistry._elements = new CardObjectRegistry();
                GlobalRegistry.populateWithDefaultElements(GlobalRegistry._elements);
            }
            return GlobalRegistry._elements;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(GlobalRegistry, "actions", {
        get: function () {
            if (!GlobalRegistry._actions) {
                GlobalRegistry._actions = new CardObjectRegistry();
                GlobalRegistry.populateWithDefaultActions(GlobalRegistry._actions);
            }
            return GlobalRegistry._actions;
        },
        enumerable: false,
        configurable: true
    });
    GlobalRegistry.reset = function () {
        GlobalRegistry._elements = undefined;
        GlobalRegistry._actions = undefined;
    };
    GlobalRegistry.defaultElements = new CardObjectRegistry();
    GlobalRegistry.defaultActions = new CardObjectRegistry();
    return GlobalRegistry;
}());
exports.GlobalRegistry = GlobalRegistry;
//# sourceMappingURL=registry.js.map