/**
 * DevExtreme (esm/__internal/grids/grid_core/header_panel/m_header_panel.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/
 */
import _extends from "@babel/runtime/helpers/esm/extends";
import $ from "../../../../core/renderer";
import {
    getPathParts
} from "../../../../core/utils/data";
import {
    extend
} from "../../../../core/utils/extend";
import {
    isDefined,
    isString
} from "../../../../core/utils/type";
import messageLocalization from "../../../../localization/message";
import Toolbar from "../../../../ui/toolbar";
import {
    ColumnsView
} from "../views/m_columns_view";
var HEADER_PANEL_CLASS = "header-panel";
var TOOLBAR_BUTTON_CLASS = "toolbar-button";
var TOOLBAR_ARIA_LABEL = "-ariaToolbar";
var DEFAULT_TOOLBAR_ITEM_NAMES = ["addRowButton", "applyFilterButton", "columnChooserButton", "exportButton", "groupPanel", "revertButton", "saveButton", "searchPanel"];
export class HeaderPanel extends ColumnsView {
    init() {
        super.init();
        this._editingController = this.getController("editing");
        this._headerFilterController = this.getController("headerFilter");
        this.createAction("onToolbarPreparing", {
            excludeValidators: ["disabled", "readOnly"]
        })
    }
    _getToolbarItems() {
        return []
    }
    _getButtonContainer() {
        return $("<div>").addClass(this.addWidgetPrefix(TOOLBAR_BUTTON_CLASS))
    }
    _getToolbarButtonClass(specificClass) {
        var secondClass = specificClass ? " ".concat(specificClass) : "";
        return this.addWidgetPrefix(TOOLBAR_BUTTON_CLASS) + secondClass
    }
    _getToolbarOptions() {
        var userToolbarOptions = this.option("toolbar");
        var options = {
            toolbarOptions: {
                items: this._getToolbarItems(),
                visible: null === userToolbarOptions || void 0 === userToolbarOptions ? void 0 : userToolbarOptions.visible,
                disabled: null === userToolbarOptions || void 0 === userToolbarOptions ? void 0 : userToolbarOptions.disabled,
                onItemRendered(e) {
                    var itemRenderedCallback = e.itemData.onItemRendered;
                    if (itemRenderedCallback) {
                        itemRenderedCallback(e)
                    }
                }
            }
        };
        var userItems = null === userToolbarOptions || void 0 === userToolbarOptions ? void 0 : userToolbarOptions.items;
        options.toolbarOptions.items = this._normalizeToolbarItems(options.toolbarOptions.items, userItems);
        this.executeAction("onToolbarPreparing", options);
        if (options.toolbarOptions && !isDefined(options.toolbarOptions.visible)) {
            var toolbarItems = options.toolbarOptions.items;
            options.toolbarOptions.visible = !!(null === toolbarItems || void 0 === toolbarItems ? void 0 : toolbarItems.length)
        }
        return options.toolbarOptions
    }
    _normalizeToolbarItems(defaultItems, userItems) {
        defaultItems.forEach(button => {
            if (!DEFAULT_TOOLBAR_ITEM_NAMES.includes(button.name)) {
                throw new Error("Default toolbar item '".concat(button.name, "' is not added to DEFAULT_TOOLBAR_ITEM_NAMES"))
            }
        });
        var defaultProps = {
            location: "after"
        };
        var isArray = Array.isArray(userItems);
        if (!isDefined(userItems)) {
            return defaultItems
        }
        if (!isArray) {
            userItems = [userItems]
        }
        var defaultButtonsByNames = {};
        defaultItems.forEach(button => {
            defaultButtonsByNames[button.name] = button
        });
        var normalizedItems = userItems.map(button => {
            if (isString(button)) {
                button = {
                    name: button
                }
            }
            if (isDefined(button.name)) {
                if (isDefined(defaultButtonsByNames[button.name])) {
                    button = extend(true, {}, defaultButtonsByNames[button.name], button)
                } else if (DEFAULT_TOOLBAR_ITEM_NAMES.includes(button.name)) {
                    button = _extends(_extends({}, button), {
                        visible: false
                    })
                }
            }
            return extend(true, {}, defaultProps, button)
        });
        return isArray ? normalizedItems : normalizedItems[0]
    }
    _renderCore() {
        if (!this._toolbar) {
            var $headerPanel = this.element();
            $headerPanel.addClass(this.addWidgetPrefix(HEADER_PANEL_CLASS));
            var label = messageLocalization.format(this.component.NAME + TOOLBAR_ARIA_LABEL);
            var $toolbar = $("<div>").attr("aria-label", label).appendTo($headerPanel);
            this._toolbar = this._createComponent($toolbar, Toolbar, this._toolbarOptions)
        } else {
            this._toolbar.option(this._toolbarOptions)
        }
    }
    _columnOptionChanged() {}
    _handleDataChanged() {
        if (this._requireReady) {
            this.render()
        }
    }
    _isDisabledDefinedByUser(name) {
        var _a;
        var userItems = null === (_a = this.option("toolbar")) || void 0 === _a ? void 0 : _a.items;
        var userItem = null === userItems || void 0 === userItems ? void 0 : userItems.find(item => (null === item || void 0 === item ? void 0 : item.name) === name);
        return isDefined(null === userItem || void 0 === userItem ? void 0 : userItem.disabled)
    }
    render() {
        this._toolbarOptions = this._getToolbarOptions();
        super.render.apply(this, arguments)
    }
    setToolbarItemDisabled(name, disabled) {
        var _a;
        var toolbar = this._toolbar;
        var isDefinedByUser = this._isDisabledDefinedByUser(name);
        if (!toolbar || isDefinedByUser) {
            return
        }
        var items = null !== (_a = toolbar.option("items")) && void 0 !== _a ? _a : [];
        var itemIndex = items.findIndex(item => item.name === name);
        if (itemIndex < 0) {
            return
        }
        var item = toolbar.option("items[".concat(itemIndex, "]"));
        toolbar.option("items[".concat(itemIndex, "].disabled"), disabled);
        if (item.options) {
            toolbar.option("items[".concat(itemIndex, "].options.disabled"), disabled)
        }
    }
    updateToolbarDimensions() {
        var _a;
        null === (_a = this._toolbar) || void 0 === _a ? void 0 : _a.updateDimensions()
    }
    getHeaderPanel() {
        return this.element()
    }
    getHeight() {
        return this.getElementHeight()
    }
    optionChanged(args) {
        var _a, _b, _c;
        if ("onToolbarPreparing" === args.name) {
            this._invalidate();
            args.handled = true
        }
        if ("toolbar" === args.name) {
            var parts = getPathParts(args.fullName);
            var optionName = args.fullName.replace(/^toolbar\./, "");
            if (1 === parts.length) {
                this._invalidate()
            } else if ("items" === parts[1]) {
                if (2 === parts.length) {
                    var toolbarOptions = this._getToolbarOptions();
                    null === (_a = this._toolbar) || void 0 === _a ? void 0 : _a.option("items", toolbarOptions.items)
                } else if (3 === parts.length) {
                    var normalizedItem = this._normalizeToolbarItems(this._getToolbarItems(), args.value);
                    null === (_b = this._toolbar) || void 0 === _b ? void 0 : _b.option(optionName, normalizedItem)
                } else if (parts.length >= 4) {
                    null === (_c = this._toolbar) || void 0 === _c ? void 0 : _c.option(optionName, args.value)
                }
            } else {
                this._invalidate()
            }
            args.handled = true
        }
        super.optionChanged(args)
    }
    isVisible() {
        return !!(this._toolbarOptions && this._toolbarOptions.visible)
    }
    allowDragging() {}
    hasGroupedColumns() {}
}
var resizing = Base => class extends Base {
    _updateDimensionsCore() {
        super._updateDimensionsCore.apply(this, arguments);
        this.getView("headerPanel").updateToolbarDimensions()
    }
};
export var headerPanelModule = {
    defaultOptions: () => ({}),
    views: {
        headerPanel: HeaderPanel
    },
    extenders: {
        controllers: {
            resizing: resizing
        }
    }
};
