"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RowExpanding = void 0;
const utils_1 = require("../utils");
//
exports.RowExpanding = {
    getInitialState: (state) => {
        return {
            expanded: {},
            ...state,
        };
    },
    getDefaultOptions: (table) => {
        return {
            onExpandedChange: (0, utils_1.makeStateUpdater)('expanded', table),
            paginateExpandedRows: true,
        };
    },
    createTable: (table) => {
        let registered = false;
        let queued = false;
        table._autoResetExpanded = () => {
            var _a, _b;
            if (!registered) {
                table._queue(() => {
                    registered = true;
                });
                return;
            }
            if ((_b = (_a = table.options.autoResetAll) !== null && _a !== void 0 ? _a : table.options.autoResetExpanded) !== null && _b !== void 0 ? _b : !table.options.manualExpanding) {
                if (queued)
                    return;
                queued = true;
                table._queue(() => {
                    table.resetExpanded();
                    queued = false;
                });
            }
        };
        table.setExpanded = (updater) => { var _a, _b; return (_b = (_a = table.options).onExpandedChange) === null || _b === void 0 ? void 0 : _b.call(_a, updater); };
        table.toggleAllRowsExpanded = (expanded) => {
            if (expanded !== null && expanded !== void 0 ? expanded : !table.getIsAllRowsExpanded()) {
                table.setExpanded(true);
            }
            else {
                table.setExpanded({});
            }
        };
        table.resetExpanded = (defaultState) => {
            var _a, _b;
            table.setExpanded(defaultState ? {} : ((_b = (_a = table.initialState) === null || _a === void 0 ? void 0 : _a.expanded) !== null && _b !== void 0 ? _b : {}));
        };
        table.getCanSomeRowsExpand = () => {
            return table.getPrePaginationRowModel().flatRows.some((row) => row.getCanExpand());
        };
        table.getToggleAllRowsExpandedHandler = () => {
            return (e) => {
                var _a, _b;
                (_b = (_a = e).persist) === null || _b === void 0 ? void 0 : _b.call(_a);
                table.toggleAllRowsExpanded();
            };
        };
        table.getIsSomeRowsExpanded = () => {
            const { expanded } = table.getState();
            return expanded === true || Object.values(expanded).some(Boolean);
        };
        table.getIsAllRowsExpanded = () => {
            const { expanded } = table.getState();
            // If expanded is true, save some cycles and return true
            if (typeof expanded === 'boolean') {
                return expanded === true;
            }
            if (!Object.keys(expanded).length) {
                return false;
            }
            // If any row is not expanded, return false
            if (table.getRowModel().flatRows.some((row) => !row.getIsExpanded())) {
                return false;
            }
            // They must all be expanded :shrug:
            return true;
        };
        table.getExpandedDepth = () => {
            let maxDepth = 0;
            const rowIds = table.getState().expanded === true
                ? Object.keys(table.getRowModel().rowsById)
                : Object.keys(table.getState().expanded);
            rowIds.forEach((id) => {
                const splitId = id.split('.');
                maxDepth = Math.max(maxDepth, splitId.length);
            });
            return maxDepth;
        };
        table.getPreExpandedRowModel = () => table.getSortedRowModel();
        table.getExpandedRowModel = () => {
            if (!table._getExpandedRowModel && table.options.getExpandedRowModel) {
                table._getExpandedRowModel = table.options.getExpandedRowModel(table);
            }
            if (table.options.manualExpanding || !table._getExpandedRowModel) {
                return table.getPreExpandedRowModel();
            }
            return table._getExpandedRowModel();
        };
    },
    createRow: (row, table) => {
        row.toggleExpanded = (expanded) => {
            table.setExpanded((old) => {
                const exists = old === true ? true : !!(old === null || old === void 0 ? void 0 : old[row.id]);
                let oldExpanded = {};
                if (old === true) {
                    Object.keys(table.getRowModel().rowsById).forEach((rowId) => {
                        oldExpanded[rowId] = true;
                    });
                }
                else {
                    oldExpanded = old;
                }
                expanded = expanded !== null && expanded !== void 0 ? expanded : !exists;
                if (!exists && expanded) {
                    return {
                        ...oldExpanded,
                        [row.id]: true,
                    };
                }
                if (exists && !expanded) {
                    const { [row.id]: _, ...rest } = oldExpanded;
                    return rest;
                }
                return old;
            });
        };
        row.getIsExpanded = () => {
            var _a, _b, _c;
            const { expanded } = table.getState();
            return !!((_c = (_b = (_a = table.options).getIsRowExpanded) === null || _b === void 0 ? void 0 : _b.call(_a, row)) !== null && _c !== void 0 ? _c : (expanded === true || (expanded === null || expanded === void 0 ? void 0 : expanded[row.id])));
        };
        row.getCanExpand = () => {
            var _a, _b, _c, _d, _e;
            return ((_c = (_b = (_a = table.options).getRowCanExpand) === null || _b === void 0 ? void 0 : _b.call(_a, row)) !== null && _c !== void 0 ? _c : (((_d = table.options.enableExpanding) !== null && _d !== void 0 ? _d : true) && !!((_e = row.subRows) === null || _e === void 0 ? void 0 : _e.length)));
        };
        row.getIsAllParentsExpanded = () => {
            let isFullyExpanded = true;
            let currentRow = row;
            while (isFullyExpanded && currentRow.parentId) {
                currentRow = table.getRow(currentRow.parentId, true);
                isFullyExpanded = currentRow.getIsExpanded();
            }
            return isFullyExpanded;
        };
        row.getToggleExpandedHandler = () => {
            const canExpand = row.getCanExpand();
            return () => {
                if (!canExpand)
                    return;
                row.toggleExpanded();
            };
        };
    },
};
