import { getMemoOptions, makeStateUpdater, memo } from '../utils';
//
export const ColumnVisibility = {
    getInitialState: (state) => {
        return {
            columnVisibility: {},
            ...state,
        };
    },
    getDefaultOptions: (table) => {
        return {
            onColumnVisibilityChange: makeStateUpdater('columnVisibility', table),
        };
    },
    createColumn: (column, table) => {
        column.toggleVisibility = (value) => {
            if (column.getCanHide()) {
                table.setColumnVisibility((old) => ({
                    ...old,
                    [column.id]: value !== null && value !== void 0 ? value : !column.getIsVisible(),
                }));
            }
        };
        column.getIsVisible = () => {
            var _a, _b;
            const childColumns = column.columns;
            return ((_b = (childColumns.length
                ? childColumns.some((c) => c.getIsVisible())
                : (_a = table.getState().columnVisibility) === null || _a === void 0 ? void 0 : _a[column.id])) !== null && _b !== void 0 ? _b : true);
        };
        column.getCanHide = () => {
            var _a, _b;
            return ((_a = column.columnDef.enableHiding) !== null && _a !== void 0 ? _a : true) && ((_b = table.options.enableHiding) !== null && _b !== void 0 ? _b : true);
        };
        column.getToggleVisibilityHandler = () => {
            return (e) => {
                var _a;
                (_a = column.toggleVisibility) === null || _a === void 0 ? void 0 : _a.call(column, e.target.checked);
            };
        };
    },
    createRow: (row, table) => {
        row._getAllVisibleCells = memo(() => [row.getAllCells(), table.getState().columnVisibility], (cells) => {
            return cells.filter((cell) => cell.column.getIsVisible());
        }, getMemoOptions(table.options, 'debugRows', '_getAllVisibleCells'));
        row.getVisibleCells = memo(() => [row.getLeftVisibleCells(), row.getCenterVisibleCells(), row.getRightVisibleCells()], (left, center, right) => [...left, ...center, ...right], getMemoOptions(table.options, 'debugRows', 'getVisibleCells'));
    },
    createTable: (table) => {
        const makeVisibleColumnsMethod = (key, getColumns) => {
            return memo(() => [
                getColumns(),
                getColumns()
                    .filter((d) => d.getIsVisible())
                    .map((d) => d.id)
                    .join('_'),
            ], (columns) => {
                return columns.filter((d) => { var _a; return (_a = d.getIsVisible) === null || _a === void 0 ? void 0 : _a.call(d); });
            }, getMemoOptions(table.options, 'debugColumns', key));
        };
        table.getVisibleFlatColumns = makeVisibleColumnsMethod('getVisibleFlatColumns', () => table.getAllFlatColumns());
        table.getVisibleLeafColumns = makeVisibleColumnsMethod('getVisibleLeafColumns', () => table.getAllLeafColumns());
        table.getLeftVisibleLeafColumns = makeVisibleColumnsMethod('getLeftVisibleLeafColumns', () => table.getLeftLeafColumns());
        table.getRightVisibleLeafColumns = makeVisibleColumnsMethod('getRightVisibleLeafColumns', () => table.getRightLeafColumns());
        table.getCenterVisibleLeafColumns = makeVisibleColumnsMethod('getCenterVisibleLeafColumns', () => table.getCenterLeafColumns());
        table.setColumnVisibility = (updater) => { var _a, _b; return (_b = (_a = table.options).onColumnVisibilityChange) === null || _b === void 0 ? void 0 : _b.call(_a, updater); };
        table.resetColumnVisibility = (defaultState) => {
            var _a;
            table.setColumnVisibility(defaultState ? {} : ((_a = table.initialState.columnVisibility) !== null && _a !== void 0 ? _a : {}));
        };
        table.toggleAllColumnsVisible = (value) => {
            value = value !== null && value !== void 0 ? value : !table.getIsAllColumnsVisible();
            table.setColumnVisibility(table.getAllLeafColumns().reduce((obj, column) => {
                var _a;
                return ({
                    ...obj,
                    [column.id]: !value ? !((_a = column.getCanHide) === null || _a === void 0 ? void 0 : _a.call(column)) : value,
                });
            }, {}));
        };
        table.getIsAllColumnsVisible = () => !table.getAllLeafColumns().some((column) => { var _a; return !((_a = column.getIsVisible) === null || _a === void 0 ? void 0 : _a.call(column)); });
        table.getIsSomeColumnsVisible = () => table.getAllLeafColumns().some((column) => { var _a; return (_a = column.getIsVisible) === null || _a === void 0 ? void 0 : _a.call(column); });
        table.getToggleAllColumnsVisibilityHandler = () => {
            return (e) => {
                var _a;
                table.toggleAllColumnsVisible((_a = e.target) === null || _a === void 0 ? void 0 : _a.checked);
            };
        };
    },
};
export function _getVisibleLeafColumns(table, position) {
    if (!position)
        return table.getVisibleLeafColumns();
    if (position === 'center')
        return table.getCenterVisibleLeafColumns();
    if (position === 'left')
        return table.getLeftVisibleLeafColumns();
    return table.getRightVisibleLeafColumns();
}
