// tslint:disable:deprecation
var _a;
import { __decorate, __extends } from "tslib";
import Component from 'vue-class-component';
import { Emit, Prop } from 'vue-property-decorator';
import { ElementQueries } from '../../mixins/element-queries/element-queries';
import { InputLabel } from '../../mixins/input-label/input-label';
import { InputManagement } from '../../mixins/input-management/input-management';
import { InputState } from '../../mixins/input-state/input-state';
import { InputWidth } from '../../mixins/input-width/input-width';
import { FormatMode } from '../../utils/i18n/i18n';
import uuid from '../../utils/uuid/uuid';
import { ModulVue } from '../../utils/vue/vue';
import { INPUT_STYLE_NAME, RICH_TEXT_EDITOR_NAME, VALIDATION_MESSAGE_NAME } from '../component-names';
import { MInputStyle } from '../input-style/input-style';
import { MValidationMessage } from '../validation-message/validation-message';
import VueFroala from './adapter/vue-froala';
import { MRichTextEditorDefaultOptions } from './rich-text-editor-options';
import WithRender from './rich-text-editor.html?style=./rich-text-editor.scss';
var RICH_TEXT_LICENSE_KEY = 'm-rich-text-license-key';
var MRichTextEditor = /** @class */ (function (_super) {
    __extends(MRichTextEditor, _super);
    function MRichTextEditor() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.selector = '.fr-element.fr-view';
        _this.customTranslations = {
            'Update': _this.$i18n.translate('m-inplace-edit:modify'),
            'URL': _this.$i18n.translate('m-rich-text-editor:URL')
        };
        _this.i18nHeaderTitle = _this.$i18n.translate('m-rich-text-editor:title');
        _this.i18nHeaderSubtitle = _this.$i18n.translate('m-rich-text-editor:subtitle');
        return _this;
    }
    MRichTextEditor.prototype.mounted = function () {
        this.testSelectorProps();
        if (!this.headerLevelValid) {
            throw new Error(RICH_TEXT_EDITOR_NAME + ": first-header-level must be inferior or equal to last-header-level");
        }
    };
    Object.defineProperty(MRichTextEditor.prototype, "internalOptions", {
        get: function () {
            var propOptions = {
                // Hack to "hide" the default froala placeholder text
                placeholderText: this.as().placeholder || ' ',
                toolbarStickyOffset: this.calculateToolbarStickyOffset(),
                scrollableContainer: this.getScrollableContainer(),
                imageHideFloatLayout: this.showImageFloatLayout,
                charCounterCount: this.showCharacterCount || !!this.characterCountMax,
                charCounterMax: !this.characterCountMax ? -1 : this.characterCountMax
            };
            return Object.assign(this.getOptions(), propOptions);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MRichTextEditor.prototype, "froalaLicenseKey", {
        get: function () {
            return this.$license.getLicense(RICH_TEXT_LICENSE_KEY) || '';
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MRichTextEditor.prototype, "headerLevelValid", {
        get: function () {
            return this.firstHeaderLevel <= this.lastHeaderLevel;
        },
        enumerable: false,
        configurable: true
    });
    MRichTextEditor.prototype.getOptions = function () {
        var richTextEditorOptions = new MRichTextEditorDefaultOptions(this.froalaLicenseKey, this.$i18n.currentLang());
        if (this.showImageButton) {
            richTextEditorOptions.pluginsEnabled.push('image');
            // toolbar for desktop devices
            richTextEditorOptions.toolbarButtons.moreRich.buttons.push('insertImage');
            // for mobile devices
            richTextEditorOptions.toolbarButtonsXS.moreRich.buttons.push('insertImage');
        }
        if (this.titleAvailable) {
            richTextEditorOptions.paragraphStyles = this.manageHeaderLevels();
            // toolbar for desktop devices
            richTextEditorOptions.toolbarButtons.moreText.buttons.splice(0, 0, 'paragraphStyle');
            // for mobile devices
            richTextEditorOptions.toolbarButtonsXS.moreText.buttons.splice(0, 0, 'paragraphStyle');
        }
        if (this.showUnderlineButton) {
            // toolbar for desktop devices
            richTextEditorOptions.toolbarButtons.moreText.buttons.splice(richTextEditorOptions.toolbarButtons.moreText.buttons.length - 2, 0, 'underline');
            // for mobile devices
            richTextEditorOptions.toolbarButtonsXS.moreText.buttons.splice(richTextEditorOptions.toolbarButtons.moreText.buttons.length - 2, 0, 'underline');
        }
        if (this.showStrikeThroughButton) {
            // toolbar for desktop devices
            richTextEditorOptions.toolbarButtons.moreText.buttons.splice(richTextEditorOptions.toolbarButtons.moreText.buttons.length - 2, 0, 'strikeThrough');
            // for mobile devices
            richTextEditorOptions.toolbarButtonsXS.moreText.buttons.splice(richTextEditorOptions.toolbarButtons.moreText.buttons.length - 2, 0, 'strikeThrough');
        }
        if (this.showParagraphAlignmentButtons) {
            // toolbar for desktop devices
            richTextEditorOptions.toolbarButtons.moreParagraph.buttons.splice(0, 0, 'align');
            // for mobile devices
            richTextEditorOptions.toolbarButtonsXS.moreParagraph.buttons.splice(0, 0, 'align');
        }
        if (this.showCharacterCount || this.characterCountMax) {
            richTextEditorOptions.pluginsEnabled.push('charCounter');
        }
        return richTextEditorOptions;
    };
    MRichTextEditor.prototype.manageHeaderLevels = function () {
        var headersLevel = {};
        headersLevel[''] = this.$i18n.translate('m-rich-text-editor:normal-level');
        if (this.firstHeaderLevel === this.lastHeaderLevel) {
            // One level of header
            headersLevel['rte-h' + this.firstHeaderLevel + this.getClassLevel(1)] = this.i18nHeaderTitle;
        }
        else if (this.lastHeaderLevel - this.firstHeaderLevel === 1) {
            // Two levels of header
            headersLevel['rte-h' + this.firstHeaderLevel + this.getClassLevel(1)] = this.i18nHeaderTitle;
            headersLevel['rte-h' + this.lastHeaderLevel + this.getClassLevel(2)] = this.i18nHeaderSubtitle;
        }
        else {
            // Multiple levels of header
            var levelNumber = 1;
            for (var headerLevel = this.firstHeaderLevel; headerLevel <= this.lastHeaderLevel; headerLevel++) {
                headersLevel['rte-h' + headerLevel + this.getClassLevel(levelNumber)] = this.$i18n.translate('m-rich-text-editor:title-level', { headerLevel: levelNumber }, 1, undefined, undefined, FormatMode.Sprintf);
                levelNumber++;
            }
        }
        return headersLevel;
    };
    MRichTextEditor.prototype.getClassLevel = function (level) {
        var classLevel = ' rte_h_level';
        return classLevel + level;
    };
    MRichTextEditor.prototype.getSelectorErrorMsg = function (prop) {
        return RICH_TEXT_EDITOR_NAME + ": No element has been found with the selector given in the " + prop + " prop.";
    };
    MRichTextEditor.prototype.refreshModel = function (newValue) {
        this.$emit('input', newValue);
    };
    MRichTextEditor.prototype.calculateToolbarStickyOffset = function () {
        if (this.toolbarStickyOffset) {
            if (this.isNumber(this.toolbarStickyOffset)) {
                return Number(this.toolbarStickyOffset);
            }
            else {
                var element = document.querySelector(this.toolbarStickyOffset);
                return element.offsetHeight;
            }
        }
    };
    MRichTextEditor.prototype.isNumber = function (value) {
        return /^-*\d+$/.test(this.toolbarStickyOffset);
    };
    MRichTextEditor.prototype.getScrollableContainer = function () {
        // The froala version 3 don't support 'scrollableContainer' with undefined value. By default is 'body'.
        return this.scrollableContainer || 'body';
    };
    MRichTextEditor.prototype.testSelectorProps = function () {
        var propInError;
        if (this.scrollableContainer) {
            if (!document.querySelector(this.scrollableContainer)) {
                propInError = 'scrollable-container';
            }
        }
        if (this.toolbarStickyOffset && !this.isNumber(this.toolbarStickyOffset)) {
            if (!document.querySelector(this.toolbarStickyOffset)) {
                propInError = 'toolbar-sticky-offset';
            }
        }
        if (propInError) {
            throw new Error(this.getSelectorErrorMsg(propInError));
        }
    };
    MRichTextEditor.prototype.onFullscreen = function (fullscreenWasActived) { };
    MRichTextEditor.prototype.imageReady = function (file, storeName) {
    };
    MRichTextEditor.prototype.imageAdded = function (file, insertImage) {
    };
    MRichTextEditor.prototype.imageRemoved = function (id, storeName) {
    };
    __decorate([
        Prop({ default: '' })
    ], MRichTextEditor.prototype, "value", void 0);
    __decorate([
        Prop({ default: 1 })
    ], MRichTextEditor.prototype, "minRows", void 0);
    __decorate([
        Prop({ default: '0' })
    ], MRichTextEditor.prototype, "toolbarStickyOffset", void 0);
    __decorate([
        Prop()
    ], MRichTextEditor.prototype, "scrollableContainer", void 0);
    __decorate([
        Prop({
            default: 5,
            validator: function (level) {
                return level >= 1 && level <= 6;
            }
        })
    ], MRichTextEditor.prototype, "firstHeaderLevel", void 0);
    __decorate([
        Prop({
            default: 6,
            validator: function (level) {
                return level >= 1 && level <= 6;
            }
        })
    ], MRichTextEditor.prototype, "lastHeaderLevel", void 0);
    __decorate([
        Prop({ default: false })
    ], MRichTextEditor.prototype, "showCharacterCount", void 0);
    __decorate([
        Prop()
    ], MRichTextEditor.prototype, "characterCountMax", void 0);
    __decorate([
        Prop({ default: false })
    ], MRichTextEditor.prototype, "showParagraphAlignmentButtons", void 0);
    __decorate([
        Prop({ default: false })
    ], MRichTextEditor.prototype, "showUnderlineButton", void 0);
    __decorate([
        Prop({ default: false })
    ], MRichTextEditor.prototype, "showStrikeThroughButton", void 0);
    __decorate([
        Prop({ default: false })
    ], MRichTextEditor.prototype, "showImageButton", void 0);
    __decorate([
        Prop({ default: false })
    ], MRichTextEditor.prototype, "showImageFloatLayout", void 0);
    __decorate([
        Prop({ default: false })
    ], MRichTextEditor.prototype, "titleAvailable", void 0);
    __decorate([
        Prop()
    ], MRichTextEditor.prototype, "imageUploadCustomValidation", void 0);
    __decorate([
        Prop({ default: function () { return "mRichText-" + uuid.generate(); } })
    ], MRichTextEditor.prototype, "id", void 0);
    __decorate([
        Emit('fullscreen')
    ], MRichTextEditor.prototype, "onFullscreen", null);
    __decorate([
        Emit('image-ready')
    ], MRichTextEditor.prototype, "imageReady", null);
    __decorate([
        Emit('image-added')
    ], MRichTextEditor.prototype, "imageAdded", null);
    __decorate([
        Emit('image-removed')
    ], MRichTextEditor.prototype, "imageRemoved", null);
    MRichTextEditor = __decorate([
        WithRender,
        Component({
            components: (_a = {
                    Froala: VueFroala
                },
                _a[INPUT_STYLE_NAME] = MInputStyle,
                _a[VALIDATION_MESSAGE_NAME] = MValidationMessage,
                _a),
            mixins: [
                InputState,
                InputManagement,
                InputWidth,
                InputLabel,
                ElementQueries
            ]
        })
    ], MRichTextEditor);
    return MRichTextEditor;
}(ModulVue));
export { MRichTextEditor };
//# sourceMappingURL=rich-text-editor.js.map