/**
 * Utility to register editors and common namespace for keeping reference to all editor classes.
 */ "use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get RegisteredEditor () {
        return RegisteredEditor;
    },
    get _getEditorInstance () {
        return _getEditorInstance;
    },
    get getEditor () {
        return _getItem;
    },
    get getEditorInstance () {
        return _getEditorInstance;
    },
    get getRegisteredEditorNames () {
        return getNames;
    },
    get getRegisteredEditors () {
        return getValues;
    },
    get hasEditor () {
        return hasItem;
    },
    get registerEditor () {
        return _register;
    }
});
const _hooks = require("../core/hooks");
const _staticRegister = require("../utils/staticRegister");
const _errors = require("../helpers/errors");
const registeredEditorClasses = new WeakMap();
const { register, getItem, hasItem, getNames, getValues } = (0, _staticRegister.staticRegister)('editors');
function RegisteredEditor(editorClass) {
    const instances = {};
    const Clazz = editorClass;
    this.getConstructor = function() {
        return editorClass;
    };
    this.getInstance = function(hotInstance) {
        if (!(hotInstance.guid in instances)) {
            instances[hotInstance.guid] = new Clazz(hotInstance);
        }
        return instances[hotInstance.guid];
    };
    _hooks.Hooks.getSingleton().add('afterDestroy', function() {
        instances[this.guid] = null;
    });
}
function _getEditorInstance(name, hotInstance) {
    let editor;
    if (typeof name === 'function') {
        if (!registeredEditorClasses.get(name)) {
            _register(null, name);
        }
        editor = registeredEditorClasses.get(name);
    } else if (typeof name === 'string') {
        editor = getItem(name);
    } else {
        (0, _errors.throwWithCause)('Only strings and functions can be passed as "editor" parameter');
    }
    if (!editor) {
        (0, _errors.throwWithCause)(`No editor registered under name "${name}"`);
    }
    return editor.getInstance(hotInstance);
}
/**
 * Retrieve editor class.
 *
 * @param {string} name Editor identification.
 * @returns {Function} Returns editor class.
 */ function _getItem(name) {
    if (typeof name === 'function') {
        return name;
    }
    if (!hasItem(name)) {
        (0, _errors.throwWithCause)(`No registered editor found under "${name}" name`);
    }
    return getItem(name).getConstructor();
}
/**
 * Register editor class under specified name.
 *
 * @param {string} name Editor identification.
 * @param {Function} editorClass Editor class.
 */ function _register(name, editorClass) {
    if (name && typeof name !== 'string') {
        editorClass = name;
        name = editorClass.EDITOR_TYPE;
    }
    const editorWrapper = new RegisteredEditor(editorClass);
    if (typeof name === 'string') {
        register(name, editorWrapper);
    }
    registeredEditorClasses.set(editorClass, editorWrapper);
}
