/**
 * DevExtreme (esm/__internal/grids/pivot_grid/field_chooser/m_field_chooser.js)
 * Version: 22.2.7 (build 23185-0311)
 * Build date: Tue Jul 04 2023
 *
 * Copyright (c) 2012 - 2023 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import "../data_source/m_data_source";
import registerComponent from "../../../../core/component_registrator";
import $ from "../../../../core/renderer";
import {
    extend
} from "../../../../core/utils/extend";
import {
    getImageContainer
} from "../../../../core/utils/icon";
import {
    each
} from "../../../../core/utils/iterator";
import {
    isDefined
} from "../../../../core/utils/type";
import {
    hasWindow as hasWindowFn
} from "../../../../core/utils/window";
import localizationMessage from "../../../../localization/message";
import ContextMenu from "../../../../ui/context_menu";
import TreeView from "../../../../ui/tree_view";
import {
    foreachDataLevel,
    getCompareFunction
} from "../m_widget_utils";
import {
    ATTRIBUTES,
    CLASSES,
    ICONS
} from "./const";
import {
    FieldChooserBase
} from "./m_field_chooser_base";
var DIV = "<div>";
var hasWindow = hasWindowFn();

function getDimensionFields(item, fields) {
    var result = [];
    if (item.items) {
        for (var i = 0; i < item.items.length; i += 1) {
            result.push.apply(result, getDimensionFields(item.items[i], fields))
        }
    } else if (isDefined(item.index)) {
        result.push(fields[item.index])
    }
    return result
}

function getFirstItem(item, condition) {
    if (item.items) {
        for (var i = 0; i < item.items.length; i += 1) {
            var childrenItem = getFirstItem(item.items[i], condition);
            if (childrenItem) {
                return childrenItem
            }
        }
    }
    if (condition(item)) {
        return item
    }
    return
}
var compareOrder = [function(a, b) {
    var aValue = -!!a.isMeasure;
    var bValue = +!!b.isMeasure;
    return aValue + bValue
}, function(a, b) {
    var aValue = -!!(a.items && a.items.length);
    var bValue = +!!(b.items && b.items.length);
    return aValue + bValue
}, function(a, b) {
    var aValue = +!!(false === a.isMeasure && a.field && a.field.levels && a.field.levels.length);
    var bValue = -!!(false === b.isMeasure && b.field && b.field.levels && b.field.levels.length);
    return aValue + bValue
}, getCompareFunction(item => item.text)];

function compareItems(a, b) {
    var result = 0;
    var i = 0;
    while (!result && compareOrder[i]) {
        result = compareOrder[i++](a, b)
    }
    return result
}

function getScrollable(container) {
    return container.find(".".concat(CLASSES.scrollable.self)).dxScrollable("instance")
}
var FieldChooser = FieldChooserBase.inherit({
    _getDefaultOptions() {
        return extend(this.callBase(), {
            height: 400,
            layout: 0,
            dataSource: null,
            encodeHtml: true,
            onContextMenuPreparing: null,
            allowSearch: false,
            searchTimeout: 500,
            texts: {
                columnFields: localizationMessage.format("dxPivotGrid-columnFields"),
                rowFields: localizationMessage.format("dxPivotGrid-rowFields"),
                dataFields: localizationMessage.format("dxPivotGrid-dataFields"),
                filterFields: localizationMessage.format("dxPivotGrid-filterFields"),
                allFields: localizationMessage.format("dxPivotGrid-allFields")
            }
        })
    },
    _refreshDataSource() {
        var that = this;
        that._expandedPaths = [];
        that._changedHandler = that._changedHandler || function() {
            each(that._dataChangedHandlers, (_, func) => {
                func()
            });
            that._fireContentReadyAction();
            that._skipStateChange = true;
            that.option("state", that._dataSource.state());
            that._skipStateChange = false
        };
        that._disposeDataSource();
        that.callBase();
        that._dataSource && that._dataSource.on("changed", that._changedHandler)
    },
    _disposeDataSource() {
        var dataSource = this._dataSource;
        if (dataSource) {
            dataSource.off("changed", this._changedHandler);
            this._dataSource = void 0
        }
    },
    _dispose() {
        this._disposeDataSource();
        this.callBase.apply(this, arguments)
    },
    _init() {
        this.callBase();
        this._refreshDataSource();
        this._dataChangedHandlers = [];
        this._initActions()
    },
    _initActions() {
        this._actions = {
            onContextMenuPreparing: this._createActionByOption("onContextMenuPreparing")
        }
    },
    _trigger(eventName, eventArg) {
        this._actions[eventName](eventArg)
    },
    _setOptionsByReference() {
        this.callBase();
        extend(this._optionsByReference, {
            dataSource: true
        })
    },
    _optionChanged(args) {
        switch (args.name) {
            case "dataSource":
                this._refreshDataSource();
                this._invalidate();
                break;
            case "layout":
            case "texts":
            case "allowSearch":
            case "searchTimeout":
            case "encodeHtml":
                this._invalidate();
                break;
            case "onContextMenuPreparing":
                this._actions[args.name] = this._createActionByOption(args.name);
                break;
            default:
                this.callBase(args)
        }
    },
    _clean(skipStateSetting) {
        !skipStateSetting && this._dataSource && this.option("state", this._dataSource.state());
        this.$element().children(".".concat(CLASSES.fieldChooser.container)).remove()
    },
    _renderLayout0($container) {
        $container.addClass(CLASSES.layout.zero);
        var $row1 = $(DIV).addClass(CLASSES.row).appendTo($container);
        var $row2 = $(DIV).addClass(CLASSES.row).appendTo($container);
        var $col1 = $(DIV).addClass(CLASSES.col).appendTo($row1);
        var $col2 = $(DIV).addClass(CLASSES.col).appendTo($row1);
        var $col3 = $(DIV).addClass(CLASSES.col).appendTo($row2);
        var $col4 = $(DIV).addClass(CLASSES.col).appendTo($row2);
        this._renderArea($col1, "all");
        this._renderArea($col2, "row");
        this._renderArea($col2, "column");
        this._renderArea($col3, "filter");
        this._renderArea($col4, "data")
    },
    _renderLayout1($container) {
        var $col1 = $(DIV).addClass(CLASSES.col).appendTo($container);
        var $col2 = $(DIV).addClass(CLASSES.col).appendTo($container);
        this._renderArea($col1, "all");
        this._renderArea($col2, "filter");
        this._renderArea($col2, "row");
        this._renderArea($col2, "column");
        this._renderArea($col2, "data")
    },
    _renderLayout2($container) {
        $container.addClass(CLASSES.layout.second);
        var $row1 = $(DIV).addClass(CLASSES.row).appendTo($container);
        this._renderArea($row1, "all");
        var $row2 = $(DIV).addClass(CLASSES.row).appendTo($container);
        var $col1 = $(DIV).addClass(CLASSES.col).appendTo($row2);
        var $col2 = $(DIV).addClass(CLASSES.col).appendTo($row2);
        this._renderArea($col1, "filter");
        this._renderArea($col1, "row");
        this._renderArea($col2, "column");
        this._renderArea($col2, "data")
    },
    _initMarkup() {
        var $element = this.$element();
        var $container = $(DIV).addClass(CLASSES.fieldChooser.container).appendTo($element);
        var layout = this.option("layout");
        this.callBase();
        $element.addClass(CLASSES.fieldChooser.self).addClass(CLASSES.pivotGrid.fieldsContainer);
        this._dataChangedHandlers = [];
        var dataSource = this._dataSource;
        var currentState = "instantly" !== this.option("applyChangesMode") && dataSource && dataSource.state();
        currentState && this.option("state") && dataSource.state(this.option("state"), true);
        if (0 === layout) {
            this._renderLayout0($container)
        } else if (1 === layout) {
            this._renderLayout1($container)
        } else {
            this._renderLayout2($container)
        }
        currentState && dataSource.state(currentState, true)
    },
    _renderContentImpl() {
        this.callBase();
        this.renderSortable();
        this._renderContextMenu();
        this.updateDimensions()
    },
    _fireContentReadyAction() {
        if (!this._dataSource || !this._dataSource.isLoading()) {
            this.callBase()
        }
    },
    _getContextMenuArgs(dxEvent) {
        var targetFieldElement = $(dxEvent.target).closest(".".concat(CLASSES.area.field));
        var targetGroupElement = $(dxEvent.target).closest(".".concat(CLASSES.area.fieldList));
        var field;
        var area;
        if (targetFieldElement.length) {
            var fieldCopy = targetFieldElement.data("field");
            if (fieldCopy) {
                field = this.getDataSource().field(fieldCopy.index) || fieldCopy
            }
        }
        if (targetGroupElement.length) {
            area = targetGroupElement.attr("group")
        }
        return {
            event: dxEvent,
            field: field,
            area: area,
            items: []
        }
    },
    _renderContextMenu() {
        var that = this;
        var $container = that.$element();
        if (that._contextMenu) {
            that._contextMenu.$element().remove()
        }
        that._contextMenu = that._createComponent($(DIV).appendTo($container), ContextMenu, {
            onPositioning(actionArgs) {
                var {
                    event: event
                } = actionArgs;
                if (!event) {
                    return
                }
                var args = that._getContextMenuArgs(event);
                that._trigger("onContextMenuPreparing", args);
                if (args.items && args.items.length) {
                    actionArgs.component.option("items", args.items)
                } else {
                    actionArgs.cancel = true
                }
            },
            target: $container,
            onItemClick(params) {
                params.itemData.onItemClick && params.itemData.onItemClick(params)
            },
            cssClass: CLASSES.fieldChooser.contextMenu
        })
    },
    _createTreeItems(fields, groupFieldNames, path) {
        var that = this;
        var isMeasure;
        var resultItems = [];
        var groupedItems = [];
        var groupFieldName = groupFieldNames[0];
        var fieldsByGroup = {};
        if (!groupFieldName) {
            each(fields, (_, field) => {
                var icon;
                if (true === field.isMeasure) {
                    icon = ICONS.measure
                }
                if (false === field.isMeasure) {
                    icon = field.groupName ? ICONS.hierarchy : ICONS.dimension
                }
                resultItems.push({
                    index: field.index,
                    field: field,
                    key: field.dataField,
                    selected: isDefined(field.area),
                    text: field.caption || field.dataField,
                    icon: icon,
                    isMeasure: field.isMeasure,
                    isDefault: field.isDefault
                })
            })
        } else {
            each(fields, (_, field) => {
                var groupName = field[groupFieldName] || "";
                fieldsByGroup[groupName] = fieldsByGroup[groupName] || [];
                fieldsByGroup[groupName].push(field);
                if (void 0 === isMeasure) {
                    isMeasure = true
                }
                isMeasure = isMeasure && true === field.isMeasure
            });
            each(fieldsByGroup, (groupName, fields) => {
                var currentPath = path ? "".concat(path, ".").concat(groupName) : groupName;
                var items = that._createTreeItems(fields, groupFieldNames.slice(1), currentPath);
                if (groupName) {
                    groupedItems.push({
                        key: groupName,
                        text: groupName,
                        path: currentPath,
                        isMeasure: items.isMeasure,
                        expanded: that._expandedPaths.includes(currentPath),
                        items: items
                    })
                } else {
                    resultItems = items
                }
            });
            resultItems = groupedItems.concat(resultItems);
            resultItems.isMeasure = isMeasure
        }
        return resultItems
    },
    _createFieldsDataSource(dataSource) {
        var fields = dataSource && dataSource.fields() || [];
        fields = fields.filter(field => false !== field.visible && !isDefined(field.groupIndex));
        var treeItems = this._createTreeItems(fields, ["dimension", "displayFolder"]);
        foreachDataLevel(treeItems, items => {
            items.sort(compareItems)
        }, 0, "items");
        return treeItems
    },
    _renderFieldsTreeView(container) {
        var that = this;
        var dataSource = that._dataSource;
        var treeView = that._createComponent(container, TreeView, {
            dataSource: that._createFieldsDataSource(dataSource),
            showCheckBoxesMode: "normal",
            expandNodesRecursive: false,
            searchEnabled: that.option("allowSearch"),
            searchTimeout: that.option("searchTimeout"),
            useNativeScrolling: false,
            itemTemplate(itemData, itemIndex, itemElement) {
                var _a;
                var $item = $("<div>").toggleClass(CLASSES.area.field, !itemData.items).attr(ATTRIBUTES.treeViewItem, true).data("field", itemData.field).appendTo(itemElement);
                if (itemData.icon) {
                    null === (_a = getImageContainer(itemData.icon)) || void 0 === _a ? void 0 : _a.appendTo($item)
                }
                $("<span>").text(itemData.text).appendTo($item)
            },
            onItemCollapsed(e) {
                var index = that._expandedPaths.indexOf(e.itemData.path);
                if (index >= 0) {
                    that._expandedPaths.splice(index, 1)
                }
            },
            onItemExpanded(e) {
                var index = that._expandedPaths.indexOf(e.itemData.path);
                if (index < 0) {
                    that._expandedPaths.push(e.itemData.path)
                }
            },
            onItemSelectionChanged(e) {
                var data = e.itemData;
                var field;
                var fields;
                var needSelectDefaultItem = true;
                var area;
                if (data.items) {
                    if (data.selected) {
                        treeView.unselectItem(data);
                        return
                    }
                    that._processDemandState(() => {
                        fields = getDimensionFields(data, dataSource.fields());
                        for (var i = 0; i < fields.length; i += 1) {
                            if (fields[i].area) {
                                needSelectDefaultItem = false;
                                break
                            }
                        }
                    });
                    if (needSelectDefaultItem) {
                        var item = getFirstItem(data, item => item.isDefault) || getFirstItem(data, item => isDefined(item.index));
                        item && treeView.selectItem(item);
                        return
                    }
                } else {
                    field = dataSource.fields()[data.index];
                    if (data.selected) {
                        area = field.isMeasure ? "data" : "column"
                    }
                    if (field) {
                        fields = [field]
                    }
                }
                that._applyChanges(fields, {
                    area: area,
                    areaIndex: void 0
                })
            }
        });
        that._dataChangedHandlers.push((function() {
            var scrollable = getScrollable(container);
            var scrollTop = scrollable ? scrollable.scrollTop() : 0;
            treeView.option({
                dataSource: that._createFieldsDataSource(dataSource)
            });
            scrollable = getScrollable(container);
            if (scrollable) {
                scrollable.scrollTo({
                    y: scrollTop
                });
                scrollable.update()
            }
        }))
    },
    _renderAreaFields($container, area) {
        var that = this;
        var dataSource = that._dataSource;
        var fields = dataSource ? extend(true, [], dataSource.getAreaFields(area, true)) : [];
        $container.empty();
        each(fields, (_, field) => {
            if (false !== field.visible) {
                that.renderField(field, true).appendTo($container)
            }
        })
    },
    _renderArea(container, area) {
        var that = this;
        var $areaContainer = $(DIV).addClass(CLASSES.area.self).appendTo(container);
        var $fieldsHeaderContainer = $(DIV).addClass(CLASSES.area.fieldListHeader).appendTo($areaContainer);
        var caption = that.option("texts.".concat(area, "Fields"));
        var $fieldsContent;
        var render;
        $("<span>").addClass(CLASSES.area.icon).addClass("dx-area-icon-".concat(area)).appendTo($fieldsHeaderContainer);
        $("<span>").html("&nbsp;").appendTo($fieldsHeaderContainer);
        $("<span>").addClass(CLASSES.area.caption).text(caption).appendTo($fieldsHeaderContainer);
        var $fieldsContainer = $(DIV).addClass(CLASSES.area.fieldList).addClass(CLASSES.pivotGrid.dragAction).appendTo($areaContainer);
        if ("all" !== area) {
            $fieldsContainer.attr("group", area).attr(ATTRIBUTES.allowScrolling, true);
            $fieldsContent = $(DIV).addClass(CLASSES.area.fieldContainer).appendTo($fieldsContainer);
            render = function() {
                that._renderAreaFields($fieldsContent, area)
            };
            that._dataChangedHandlers.push(render);
            render();
            $fieldsContainer.dxScrollable({
                useNative: false
            })
        } else {
            $areaContainer.addClass(CLASSES.allFields);
            $fieldsContainer.addClass(CLASSES.treeView.borderVisible);
            that._renderFieldsTreeView($fieldsContainer)
        }
    },
    _getSortableOptions: () => ({}),
    _adjustSortableOnChangedArgs() {},
    resetTreeView() {
        var treeView = this.$element().find(".".concat(CLASSES.treeView.self)).dxTreeView("instance");
        if (treeView) {
            treeView.option("searchValue", "");
            treeView.collapseAll()
        }
    },
    applyChanges() {
        var state = this.option("state");
        if (isDefined(state)) {
            this._dataSource.state(state)
        }
    },
    cancelChanges() {
        var dataSource = this._dataSource;
        if (!dataSource.isLoading()) {
            this.option("state", dataSource.state());
            return true
        }
        return false
    },
    getDataSource() {
        return this._dataSource
    },
    updateDimensions() {
        var $scrollableElements = this.$element().find(".".concat(CLASSES.area.self, " .").concat(CLASSES.scrollable.self));
        $scrollableElements.dxScrollable("update")
    },
    _visibilityChanged(visible) {
        if (visible && hasWindow) {
            this.updateDimensions()
        }
    }
});
registerComponent("dxPivotGridFieldChooser", FieldChooser);
export default {
    FieldChooser: FieldChooser
};
export {
    FieldChooser
};
