/**
 * DevExtreme (cjs/ui/text_box/texteditor_button_collection/index.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.default = void 0;
var _renderer = _interopRequireDefault(require("../../../core/renderer"));
var _custom = _interopRequireDefault(require("./custom"));
var _extend = require("../../../core/utils/extend");
var _ui = _interopRequireDefault(require("../../widget/ui.errors"));

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}
const TEXTEDITOR_BUTTONS_CONTAINER_CLASS = "dx-texteditor-buttons-container";

function checkButtonInfo(buttonInfo) {
    (() => {
        if (!buttonInfo || "object" !== typeof buttonInfo || Array.isArray(buttonInfo)) {
            throw _ui.default.Error("E1053")
        }
    })();
    (() => {
        if (!("name" in buttonInfo)) {
            throw _ui.default.Error("E1054")
        }
    })();
    (() => {
        const {
            name: name
        } = buttonInfo;
        if ("string" !== typeof name) {
            throw _ui.default.Error("E1055")
        }
    })();
    (() => {
        const {
            location: location
        } = buttonInfo;
        if ("location" in buttonInfo && "after" !== location && "before" !== location) {
            buttonInfo.location = "after"
        }
    })()
}

function checkNamesUniqueness(existingNames, newName) {
    if (-1 !== existingNames.indexOf(newName)) {
        throw _ui.default.Error("E1055", newName)
    }
    existingNames.push(newName)
}

function isPredefinedButtonName(name, predefinedButtonsInfo) {
    return !!predefinedButtonsInfo.find(info => info.name === name)
}
let TextEditorButtonCollection = function() {
    function TextEditorButtonCollection(editor, defaultButtonsInfo) {
        this.buttons = [];
        this.defaultButtonsInfo = defaultButtonsInfo;
        this.editor = editor
    }
    var _proto = TextEditorButtonCollection.prototype;
    _proto._compileButtonInfo = function(buttons) {
        const names = [];
        return buttons.map(button => {
            const isStringButton = "string" === typeof button;
            if (!isStringButton) {
                checkButtonInfo(button)
            }
            const isDefaultButton = isStringButton || isPredefinedButtonName(button.name, this.defaultButtonsInfo);
            if (isDefaultButton) {
                const defaultButtonInfo = this.defaultButtonsInfo.find(_ref => {
                    let {
                        name: name
                    } = _ref;
                    return name === button || name === button.name
                });
                if (!defaultButtonInfo) {
                    throw _ui.default.Error("E1056", this.editor.NAME, button)
                }
                checkNamesUniqueness(names, button);
                return defaultButtonInfo
            } else {
                const {
                    name: name
                } = button;
                checkNamesUniqueness(names, name);
                return (0, _extend.extend)(button, {
                    Ctor: _custom.default
                })
            }
        })
    };
    _proto._createButton = function(buttonsInfo) {
        const {
            Ctor: Ctor,
            options: options,
            name: name
        } = buttonsInfo;
        const button = new Ctor(name, this.editor, options);
        this.buttons.push(button);
        return button
    };
    _proto._renderButtons = function(buttons, $container, targetLocation) {
        let $buttonsContainer = null;
        const buttonsInfo = buttons ? this._compileButtonInfo(buttons) : this.defaultButtonsInfo;
        buttonsInfo.forEach(buttonsInfo => {
            const {
                location: location = "after"
            } = buttonsInfo;
            if (location === targetLocation) {
                this._createButton(buttonsInfo).render((() => {
                    $buttonsContainer = $buttonsContainer || (0, _renderer.default)("<div>").addClass("dx-texteditor-buttons-container");
                    "before" === targetLocation ? $container.prepend($buttonsContainer) : $container.append($buttonsContainer);
                    return $buttonsContainer
                })())
            }
        });
        return $buttonsContainer
    };
    _proto.clean = function() {
        this.buttons.forEach(button => button.dispose());
        this.buttons = []
    };
    _proto.getButton = function(buttonName) {
        const button = this.buttons.find(_ref2 => {
            let {
                name: name
            } = _ref2;
            return name === buttonName
        });
        return button && button.instance
    };
    _proto.renderAfterButtons = function(buttons, $container) {
        return this._renderButtons(buttons, $container, "after")
    };
    _proto.renderBeforeButtons = function(buttons, $container) {
        return this._renderButtons(buttons, $container, "before")
    };
    _proto.updateButtons = function(names) {
        this.buttons.forEach(button => {
            if (!names || -1 !== names.indexOf(button.name)) {
                button.update()
            }
        })
    };
    return TextEditorButtonCollection
}();
exports.default = TextEditorButtonCollection;
module.exports = exports.default;
module.exports.default = exports.default;
