System.register(["@stratusjs/runtime/stratus", "lodash"], function (exports_1, context_1) {
    "use strict";
    var stratus_1, lodash_1, InputButtonPlugin;
    var __moduleName = context_1 && context_1.id;
    return {
        setters: [
            function (stratus_1_1) {
                stratus_1 = stratus_1_1;
            },
            function (lodash_1_1) {
                lodash_1 = lodash_1_1;
            }
        ],
        execute: function () {
            InputButtonPlugin = class InputButtonPlugin {
                constructor(options) {
                    this.debug = false;
                    this.name = 'inputButton';
                    this.eventName = 'input-button';
                    this.autoRestoreSelection = false;
                    this.autoSaveSelection = false;
                    this.maintainSelection = false;
                    this.maintainSnapshot = false;
                    this.uid = lodash_1.default.uniqueId(`input_button_`);
                    stratus_1.Stratus.Instances[this.uid] = this;
                    this.debug = options && options.debug;
                    if (!options || typeof options !== 'object') {
                        options = {};
                    }
                    this.name = options.name || this.name;
                    this.eventName = options.eventName || this.eventName;
                    this.editor = options.editor || null;
                    this.insert = options.insert || null;
                    this.autoRestoreSelection = options.autoRestoreSelection || null;
                    this.autoSaveSelection = options.autoSaveSelection || null;
                    this.maintainSelection = options.maintainSelection || null;
                    this.maintainSnapshot = options.maintainSnapshot || null;
                }
                findUID(el) {
                    if ('uid' in el.dataset) {
                        return el.dataset.uid;
                    }
                    return this.findUID(el.parentElement);
                }
                getInstance(uid) {
                    if (!(uid in stratus_1.Stratus.Instances)) {
                        return undefined;
                    }
                    return stratus_1.Stratus.Instances[uid];
                }
                onClick(el, data) {
                    const uid = this.findUID(el);
                    const instance = this.getInstance(uid);
                    if (!instance) {
                        return;
                    }
                    if (this.autoSaveSelection) {
                        this.saveSelection();
                    }
                    this.updateSnapshot();
                    instance.trigger(this.eventName, data, this);
                }
                trigger(name, data, callee) {
                    if (this.debug) {
                        console.log(`${this.name}.trigger():`, name, data, callee);
                    }
                    if (lodash_1.default.isEmpty(data)) {
                        console.warn(`${this.name}.trigger():`, data, 'is not valid data.');
                        return;
                    }
                    if (!this.editor) {
                        console.warn(`${this.name}.trigger(): unable to inject html without a set editor.`);
                        return;
                    }
                    if (name !== 'insert') {
                        console.warn(`${this.name}.trigger():`, name, 'event is not supported.');
                        return;
                    }
                    if (!this.restoreSnapshot()) {
                        return;
                    }
                    if (this.autoRestoreSelection) {
                        this.restoreSelection();
                    }
                    this.insert ? this.insert(data) : this.editor.html.insert(data);
                    this.updateSnapshot();
                    if (this.maintainSelection) {
                        this.saveSelection();
                    }
                }
                restoreSnapshot() {
                    if (!this.maintainSnapshot) {
                        return true;
                    }
                    if (!this.snapshot) {
                        console.warn(`${this.name}.restoreSnapshot(): unable to restore editor session without a snapshot.`);
                        return false;
                    }
                    this.editor.events.focus();
                    this.editor.snapshot.restore(this.snapshot);
                    return true;
                }
                updateSnapshot() {
                    if (!this.maintainSnapshot) {
                        return true;
                    }
                    this.snapshot = this.editor.snapshot.get();
                }
                restoreSelection() {
                    if (!this.editor.selection) {
                        console.warn(`${this.name}.restoreSelection(): unable to restore editor selection.`);
                        return false;
                    }
                    this.editor.events.focus();
                    this.editor.selection.restore();
                    return true;
                }
                saveSelection() {
                    if (!this.editor.selection) {
                        console.warn(`${this.name}.saveSelection(): unable to save editor selection.`);
                        return false;
                    }
                    this.editor.selection.save();
                    return true;
                }
                clearSelection() {
                    if (!this.editor.selection) {
                        console.warn(`${this.name}.clearSelection(): unable to clear editor selection.`);
                        return false;
                    }
                    this.editor.selection.clear();
                    return true;
                }
            };
            exports_1("InputButtonPlugin", InputButtonPlugin);
        }
    };
});

//# sourceMappingURL=inputButton.js.map
