System.register(["@stratusjs/core/environment", "@stratusjs/runtime/stratus", "lodash", "./inputButton", "froala-editor"], function (exports_1, context_1) {
    "use strict";
    var environment_1, stratus_1, lodash_1, inputButton_1, froala_editor_1, systemDir, moduleName, parentModuleName, min, localDir;
    var __moduleName = context_1 && context_1.id;
    return {
        setters: [
            function (environment_1_1) {
                environment_1 = environment_1_1;
            },
            function (stratus_1_1) {
                stratus_1 = stratus_1_1;
            },
            function (lodash_1_1) {
                lodash_1 = lodash_1_1;
            },
            function (inputButton_1_1) {
                inputButton_1 = inputButton_1_1;
            },
            function (froala_editor_1_1) {
                froala_editor_1 = froala_editor_1_1;
            }
        ],
        execute: function () {
            systemDir = '@stratusjs/angular';
            moduleName = 'citationManager';
            parentModuleName = 'froala/plugins';
            min = !environment_1.cookie('env') ? '.min' : '';
            localDir = `${stratus_1.Stratus.BaseUrl}${boot.configuration.paths[`${systemDir}/*`].replace(/[^/]*$/, '').replace(/\/dist\/$/, '/src/')}`;
            lodash_1.extend(froala_editor_1.default.POPUP_TEMPLATES, {
                'citationManager.toolbar': '[_BUTTONS_]'
            });
            lodash_1.extend(froala_editor_1.default.DEFAULTS, {
                citationToolbarButtons: ['citationEdit', 'citationDelete']
            });
            froala_editor_1.default.PLUGINS.citationManager = function citationManager(editor) {
                let inputButton;
                const debug = false;
                function _init() {
                    stratus_1.Stratus.Internals.CssLoader(`${localDir}${parentModuleName}/${moduleName}${min}.css`);
                    inputButton = new inputButton_1.InputButtonPlugin({
                        name: 'Citation Manager',
                        eventName: 'citation-input',
                        editor,
                        autoSaveSelection: true,
                        autoRestoreSelection: true,
                        debug
                    });
                    if (debug) {
                        console.log('initialized:', {
                            instance: this
                        });
                    }
                    editor.events.on('click', (clickEvent) => {
                        if (clickEvent.hasOwnProperty('target') &&
                            (clickEvent.target.nodeName === 'STRATUS-CITATION' ||
                                clickEvent.target.nodeName === 'STRATUS-CITATION-TITLE')) {
                            const citation = getSelectedCitation();
                            if (citation) {
                                editor.citationManager.showToolbar(clickEvent, citation);
                            }
                        }
                    });
                }
                function onClick() {
                    if (!editor.el) {
                        console.warn('citationManager.onClick(): unable to find element');
                        return;
                    }
                    inputButton.onClick(editor.el);
                }
                function getSelectedCitation() {
                    let containedElement = editor.selection.endElement();
                    if (containedElement.tagName === 'STRATUS-CITATION') {
                        return containedElement;
                    }
                    containedElement = editor.selection.element();
                    if (containedElement.tagName === 'STRATUS-CITATION') {
                        return containedElement;
                    }
                    containedElement = containedElement.parentElement;
                    if (containedElement.tagName === 'STRATUS-CITATION') {
                        return containedElement;
                    }
                    return null;
                }
                function getLastKnownCitation() {
                    return editor.citationManager.lastCitation || null;
                }
                function setLastKnownCitation(el) {
                    editor.citationManager.lastCitation = el;
                }
                function initToolbar() {
                    let citationToolbarButtons = '';
                    if (editor.opts.citationToolbarButtons.length > 1) {
                        citationToolbarButtons += '<div class="fr-buttons">';
                        citationToolbarButtons += editor.button.buildList(editor.opts.citationToolbarButtons);
                        citationToolbarButtons += '</div>';
                    }
                    const template = {
                        buttons: citationToolbarButtons
                    };
                    return editor.popups.create('citationManager.toolbar', template);
                }
                function showToolbar(clickEvent, citation) {
                    const popup = editor.popups.get('citationManager.toolbar');
                    if (!popup)
                        initToolbar();
                    editor.popups.setContainer('citationManager.toolbar', editor.$tb);
                    const btn = editor.$tb.find('.fr-command[data-cmd="myButton"]');
                    const left = clickEvent.pageX;
                    const top = clickEvent.pageY;
                    editor.popups.show('citationManager.toolbar', left, top, btn.outerHeight());
                    editor.citationManager.setLastKnownCitation(citation);
                }
                function hideToolbar() {
                    editor.citationManager.setLastKnownCitation(null);
                    editor.popups.hide('citationManager.toolbar');
                }
                return {
                    _init,
                    onClick,
                    showToolbar,
                    hideToolbar,
                    getSelectedCitation,
                    getLastKnownCitation,
                    setLastKnownCitation
                };
            };
            froala_editor_1.default.DefineIcon('citationDelete', { NAME: 'citationDelete', SVG_KEY: 'remove' });
            froala_editor_1.default.RegisterCommand('citationDelete', {
                title: 'Delete Citation',
                undo: false,
                focus: false,
                callback() {
                    const containedElement = this.citationManager.getSelectedCitation();
                    if (containedElement) {
                        containedElement.remove();
                    }
                    this.citationManager.hideToolbar();
                }
            });
            froala_editor_1.default.DefineIcon('citationManager', { NAME: 'citationManager', SVG_KEY: 'imageCaption' });
            froala_editor_1.default.RegisterCommand('citationInsert', {
                icon: 'citationManager',
                title: 'Insert Citation',
                undo: false,
                focus: false,
                modal: true,
                callback(cmd, _val, _params) {
                    const debug = false;
                    if (debug) {
                        console.log('clicked:', this.citationManager);
                    }
                    this.citationManager.onClick();
                },
                plugin: 'citationManager',
            });
            froala_editor_1.default.RegisterCommand('citationEdit', {
                icon: 'citationManager',
                title: 'Edit Citation',
                undo: false,
                focus: false,
                modal: true,
                callback(cmd, _val, _params) {
                    const debug = false;
                    if (debug) {
                        console.log('clicked:', this.citationManager);
                    }
                    this.citationManager.onClick();
                },
                plugin: 'citationManager',
            });
            froala_editor_1.default.RegisterQuickInsertButton('citation', {
                icon: 'citationManager',
                title: 'Insert Citation',
                callback: function citationManagerCallback() {
                    const debug = false;
                    const inputButton = new inputButton_1.InputButtonPlugin({
                        name: 'Citation Manager',
                        eventName: 'citation-input',
                        editor: this,
                        autoSaveSelection: true,
                        autoRestoreSelection: true,
                        debug
                    });
                    if (!this.el) {
                        console.warn('citationManager.onClick(): unable to find element');
                        return;
                    }
                    inputButton.onClick(this.el);
                },
                undo: true
            });
        }
    };
});

//# sourceMappingURL=citationManager.js.map
