/**
 * DevExtreme (esm/__internal/grids/pivot_grid/headers_area/m_headers_area.js)
 * Version: 22.1.13
 * Build date: Fri Feb 09 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import $ from "../../../../core/renderer";
import {
    each
} from "../../../../core/utils/iterator";
import {
    setHeight,
    setWidth
} from "../../../../core/utils/size";
import {
    isDefined
} from "../../../../core/utils/type";
import Scrollable from "../../../../ui/scroll_view/ui.scrollable";
import {
    AreaItem
} from "../area_item/m_area_item";
var PIVOTGRID_AREA_CLASS = "dx-pivotgrid-area";
var PIVOTGRID_AREA_COLUMN_CLASS = "dx-pivotgrid-horizontal-headers";
var PIVOTGRID_AREA_ROW_CLASS = "dx-pivotgrid-vertical-headers";
var PIVOTGRID_TOTAL_CLASS = "dx-total";
var PIVOTGRID_GRAND_TOTAL_CLASS = "dx-grandtotal";
var PIVOTGRID_ROW_TOTAL_CLASS = "dx-row-total";
var PIVOTGRID_EXPANDED_CLASS = "dx-pivotgrid-expanded";
var PIVOTGRID_COLLAPSED_CLASS = "dx-pivotgrid-collapsed";
var PIVOTGRID_LAST_CELL_CLASS = "dx-last-cell";
var PIVOTGRID_VERTICAL_SCROLL_CLASS = "dx-vertical-scroll";
var PIVOTGRID_EXPAND_BORDER = "dx-expand-border";
var isRenovatedScrollable = !!Scrollable.IS_RENOVATED_WIDGET;

function getCellPath(tableElement, cell) {
    if (cell) {
        var {
            data: data
        } = tableElement.data();
        var {
            rowIndex: rowIndex
        } = cell.parentNode;
        var {
            cellIndex: cellIndex
        } = cell;
        return data[rowIndex] && data[rowIndex][cellIndex] && data[rowIndex][cellIndex].path
    }
    return
}
var HorizontalHeadersArea = AreaItem.inherit({
    ctor(component) {
        this.callBase(component);
        this._scrollBarWidth = 0
    },
    _getAreaName: () => "column",
    _getAreaClassName: () => PIVOTGRID_AREA_COLUMN_CLASS,
    _createGroupElement() {
        return $("<div>").addClass(this._getAreaClassName()).addClass(PIVOTGRID_AREA_CLASS)
    },
    _applyCustomStyles(options) {
        var {
            cssArray: cssArray
        } = options;
        var {
            cell: cell
        } = options;
        var {
            rowsCount: rowsCount
        } = options;
        var {
            classArray: classArray
        } = options;
        if (options.cellIndex === options.cellsCount - 1) {
            cssArray.push("".concat(options.rtlEnabled ? "border-left:" : "border-right:", "0px"))
        }
        if (cell.rowspan === rowsCount - options.rowIndex || options.rowIndex + 1 === rowsCount) {
            cssArray.push("border-bottom-width:0px")
        }
        if ("T" === cell.type || "GT" === cell.type) {
            classArray.push(PIVOTGRID_ROW_TOTAL_CLASS)
        }
        if ("T" === options.cell.type) {
            classArray.push(PIVOTGRID_TOTAL_CLASS)
        }
        if ("GT" === options.cell.type) {
            classArray.push(PIVOTGRID_GRAND_TOTAL_CLASS)
        }
        if (isDefined(cell.expanded)) {
            classArray.push(cell.expanded ? PIVOTGRID_EXPANDED_CLASS : PIVOTGRID_COLLAPSED_CLASS)
        }
        this.callBase(options)
    },
    _getMainElementMarkup() {
        return "<thead class='".concat(this._getAreaClassName(), "'>")
    },
    _getCloseMainElementMarkup: () => "</thead>",
    setVirtualContentParams(params) {
        this.callBase(params);
        this._setTableCss({
            left: params.left,
            top: 0
        });
        this._virtualContentWidth = params.width
    },
    hasScroll() {
        var tableWidth = this._virtualContent ? this._virtualContentWidth : this._tableWidth;
        var groupWidth = this.getGroupWidth();
        if (groupWidth && tableWidth) {
            return tableWidth - groupWidth >= 1
        }
        return false
    },
    renderScrollable() {
        this._groupElement.dxScrollable({
            useNative: false,
            useSimulatedScrollbar: false,
            showScrollbar: "never",
            bounceEnabled: false,
            direction: "horizontal",
            rtlEnabled: isRenovatedScrollable ? this.component.option("rtlEnabled") : false,
            updateManually: true
        })
    },
    updateScrollableOptions(_ref) {
        var {
            rtlEnabled: rtlEnabled
        } = _ref;
        var scrollable = this._getScrollable();
        isRenovatedScrollable && scrollable.option({
            rtlEnabled: rtlEnabled
        })
    },
    processScrollBarSpacing(scrollBarWidth) {
        var groupAlignment = this.option("rtlEnabled") ? "right" : "left";
        var groupWidth = this.getGroupWidth();
        if (groupWidth) {
            this.setGroupWidth(groupWidth - scrollBarWidth)
        }
        if (this._scrollBarWidth) {
            this._groupElement.next().remove()
        }
        this._groupElement.toggleClass(PIVOTGRID_VERTICAL_SCROLL_CLASS, scrollBarWidth > 0);
        setWidth(this._groupElement.css("float", groupAlignment), this.getGroupHeight());
        this._scrollBarWidth = scrollBarWidth
    },
    getScrollPath(offset) {
        var tableElement = this.tableElement();
        var cell;
        offset -= parseInt(tableElement[0].style.left, 10) || 0;
        each(tableElement.find("td"), (_, td) => {
            if (1 === td.colSpan && td.offsetLeft <= offset && td.offsetWidth + td.offsetLeft > offset) {
                cell = td;
                return false
            }
            return
        });
        return getCellPath(tableElement, cell)
    },
    _moveFakeTable(scrollPos) {
        this._moveFakeTableHorizontally(scrollPos);
        this.callBase()
    }
});
var VerticalHeadersArea = HorizontalHeadersArea.inherit({
    _getAreaClassName: () => PIVOTGRID_AREA_ROW_CLASS,
    _applyCustomStyles(options) {
        this.callBase(options);
        if (options.cellIndex === options.cellsCount - 1) {
            options.classArray.push(PIVOTGRID_LAST_CELL_CLASS)
        }
        if (options.rowIndex === options.rowsCount - 1) {
            options.cssArray.push("border-bottom: 0px")
        }
        if (options.cell.isWhiteSpace) {
            options.classArray.push("dx-white-space-column")
        }
    },
    _getAreaName: () => "row",
    setVirtualContentParams(params) {
        this.callBase(params);
        this._setTableCss({
            top: params.top,
            left: 0
        });
        this._virtualContentHeight = params.height
    },
    hasScroll() {
        var tableHeight = this._virtualContent ? this._virtualContentHeight : this._tableHeight;
        var groupHeight = this.getGroupHeight();
        if (groupHeight && tableHeight) {
            return tableHeight - groupHeight >= 1
        }
        return false
    },
    renderScrollable() {
        this._groupElement.dxScrollable({
            useNative: false,
            useSimulatedScrollbar: false,
            showScrollbar: "never",
            bounceEnabled: false,
            direction: "vertical",
            updateManually: true
        })
    },
    processScrollBarSpacing(scrollBarWidth) {
        var groupHeight = this.getGroupHeight();
        if (groupHeight) {
            this.setGroupHeight(groupHeight - scrollBarWidth)
        }
        if (this._scrollBarWidth) {
            this._groupElement.next().remove()
        }
        if (scrollBarWidth) {
            var $div = $("<div>");
            setWidth($div, "100%");
            setHeight($div, scrollBarWidth - 1);
            this._groupElement.after($div)
        }
        this._scrollBarWidth = scrollBarWidth
    },
    getScrollPath(offset) {
        var tableElement = this.tableElement();
        var cell;
        offset -= parseInt(tableElement[0].style.top, 10) || 0;
        each(tableElement.find("tr"), (_, tr) => {
            var td = tr.childNodes[tr.childNodes.length - 1];
            if (td && 1 === td.rowSpan && td.offsetTop <= offset && td.offsetHeight + td.offsetTop > offset) {
                cell = td;
                return false
            }
            return
        });
        return getCellPath(tableElement, cell)
    },
    _moveFakeTable(scrollPos) {
        this._moveFakeTableTop(scrollPos);
        this.callBase()
    },
    _getRowClassNames(rowIndex, cell, rowClassNames) {
        if (0 !== rowIndex & cell.expanded && !rowClassNames.includes(PIVOTGRID_EXPAND_BORDER)) {
            rowClassNames.push(PIVOTGRID_EXPAND_BORDER)
        }
    },
    _getMainElementMarkup() {
        return "<tbody class='".concat(this._getAreaClassName(), "'>")
    },
    _getCloseMainElementMarkup: () => "</tbody>",
    updateColspans(columnCount) {
        var {
            rows: rows
        } = this.tableElement()[0];
        var columnOffset = 0;
        var columnOffsetResetIndexes = [];
        if (this.getColumnsCount() - columnCount > 0) {
            return
        }
        for (var i = 0; i < rows.length; i += 1) {
            for (var j = 0; j < rows[i].cells.length; j += 1) {
                var cell = rows[i].cells[j];
                var {
                    rowSpan: rowSpan
                } = cell;
                if (columnOffsetResetIndexes[i]) {
                    columnOffset -= columnOffsetResetIndexes[i];
                    columnOffsetResetIndexes[i] = 0
                }
                var diff = columnCount - (columnOffset + cell.colSpan);
                if (j === rows[i].cells.length - 1 && diff > 0) {
                    cell.colSpan += diff
                }
                columnOffsetResetIndexes[i + rowSpan] = (columnOffsetResetIndexes[i + rowSpan] || 0) + cell.colSpan;
                columnOffset += cell.colSpan
            }
        }
    }
});
export default {
    HorizontalHeadersArea: HorizontalHeadersArea,
    VerticalHeadersArea: VerticalHeadersArea
};
export {
    HorizontalHeadersArea,
    VerticalHeadersArea
};
