"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColumnOrdering = void 0;
/* eslint-disable import/no-cycle */
const utils_1 = require("../utils");
const ColumnGrouping_1 = require("./ColumnGrouping");
const __1 = require("..");
//
exports.ColumnOrdering = {
    getInitialState: (state) => {
        return {
            columnOrder: [],
            ...state,
        };
    },
    getDefaultOptions: (table) => {
        return {
            onColumnOrderChange: (0, utils_1.makeStateUpdater)('columnOrder', table),
        };
    },
    createColumn: (column, table) => {
        column.getIndex = (0, utils_1.memo)((position) => [(0, __1._getVisibleLeafColumns)(table, position)], (columns) => columns.findIndex((d) => d.id === column.id), (0, utils_1.getMemoOptions)(table.options, 'debugColumns', 'getIndex'));
        column.getIsFirstColumn = (position) => {
            var _a;
            const columns = (0, __1._getVisibleLeafColumns)(table, position);
            return ((_a = columns[0]) === null || _a === void 0 ? void 0 : _a.id) === column.id;
        };
        column.getIsLastColumn = (position) => {
            var _a;
            const columns = (0, __1._getVisibleLeafColumns)(table, position);
            return ((_a = columns[columns.length - 1]) === null || _a === void 0 ? void 0 : _a.id) === column.id;
        };
    },
    createTable: (table) => {
        table.setColumnOrder = (updater) => { var _a, _b; return (_b = (_a = table.options).onColumnOrderChange) === null || _b === void 0 ? void 0 : _b.call(_a, updater); };
        table.resetColumnOrder = (defaultState) => {
            var _a;
            table.setColumnOrder(defaultState ? [] : ((_a = table.initialState.columnOrder) !== null && _a !== void 0 ? _a : []));
        };
        table._getOrderColumnsFn = (0, utils_1.memo)(() => [
            table.getState().columnOrder,
            table.getState().grouping,
            table.options.groupedColumnMode,
        ], (columnOrder, grouping, groupedColumnMode) => (columns) => {
            // Sort grouped columns to the start of the column list
            // before the headers are built
            let orderedColumns = [];
            // If there is no order, return the normal columns
            if (!(columnOrder === null || columnOrder === void 0 ? void 0 : columnOrder.length)) {
                orderedColumns = columns;
            }
            else {
                const columnOrderCopy = [...columnOrder];
                // If there is an order, make a copy of the columns
                const columnsCopy = [...columns];
                // And make a new ordered array of the columns
                // Loop over the columns and place them in order into the new array
                while (columnsCopy.length && columnOrderCopy.length) {
                    const targetColumnId = columnOrderCopy.shift();
                    const foundIndex = columnsCopy.findIndex((d) => d.id === targetColumnId);
                    if (foundIndex > -1) {
                        orderedColumns.push(columnsCopy.splice(foundIndex, 1)[0]);
                    }
                }
                // If there are any columns left, add them to the end
                orderedColumns = [...orderedColumns, ...columnsCopy];
            }
            return (0, ColumnGrouping_1.orderColumns)(orderedColumns, grouping, groupedColumnMode);
        }, (0, utils_1.getMemoOptions)(table.options, 'debugTable', '_getOrderColumnsFn'));
    },
};
