'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { transformColumnsToSearchParams } from 'payload/shared';
import React, { startTransition, useCallback, useRef } from 'react';
import { useConfig } from '../../providers/Config/index.js';
import { useListQuery } from '../../providers/ListQuery/index.js';
import { TableColumnContext } from './context.js';
export { useTableColumns } from './context.js';
export const TableColumnsProvider = ({ children, collectionSlug, columnState: columnStateFromProps, LinkedCellOverride })=>{
    const { getEntityConfig } = useConfig();
    const { query: currentQuery, refineListData } = useListQuery();
    const { admin: { defaultColumns } = {} } = getEntityConfig({
        collectionSlug
    });
    const [columnState, setOptimisticColumnState] = React.useOptimistic(columnStateFromProps, (state, action)=>action);
    const contextRef = useRef({});
    const toggleColumn = useCallback(async (column)=>{
        const newColumnState = (columnState || []).map((col)=>{
            if (col.accessor === column) {
                return {
                    ...col,
                    active: !col.active
                };
            }
            return col;
        });
        startTransition(()=>{
            setOptimisticColumnState(newColumnState);
        });
        await refineListData({
            columns: transformColumnsToSearchParams(newColumnState)
        });
    }, [
        refineListData,
        columnState,
        setOptimisticColumnState
    ]);
    const moveColumn = useCallback(async (args)=>{
        const { fromIndex, toIndex } = args;
        const newColumnState = [
            ...columnState || []
        ];
        const [columnToMove] = newColumnState.splice(fromIndex, 1);
        newColumnState.splice(toIndex, 0, columnToMove);
        startTransition(()=>{
            setOptimisticColumnState(newColumnState);
        });
        await refineListData({
            columns: transformColumnsToSearchParams(newColumnState)
        });
    }, [
        columnState,
        refineListData,
        setOptimisticColumnState
    ]);
    const setActiveColumns = useCallback(async (columns)=>{
        const newColumnState = currentQuery.columns;
        columns.forEach((colName)=>{
            const colIndex = newColumnState.findIndex((c)=>colName === c);
            // ensure the name does not begin with a `-` which denotes an inactive column
            if (colIndex !== undefined && newColumnState[colIndex][0] === '-') {
                newColumnState[colIndex] = colName.slice(1);
            }
        });
        await refineListData({
            columns: newColumnState
        });
    }, [
        currentQuery,
        refineListData
    ]);
    const resetColumnsState = React.useCallback(async ()=>{
        await refineListData({
            columns: defaultColumns || []
        });
    }, [
        defaultColumns,
        refineListData
    ]);
    return /*#__PURE__*/ _jsxDEV(TableColumnContext, {
        value: {
            columns: columnState,
            LinkedCellOverride,
            moveColumn,
            resetColumnsState,
            setActiveColumns,
            toggleColumn,
            ...contextRef.current
        },
        children: children
    }, void 0, false, {
        fileName: "src/providers/TableColumns/index.tsx",
        lineNumber: 95,
        columnNumber: 5
    }, this);
};

//# sourceMappingURL=index.js.map