'use client';
import React, { createContext, useCallback, useContext, useEffect, useReducer, useRef } from 'react';
import { useComponentMap } from '../../providers/ComponentMapProvider/index.js';
import { useConfig } from '../../providers/Config/index.js';
import { usePreferences } from '../../providers/Preferences/index.js';
import { buildColumns } from './buildColumns.js';
import { columnReducer } from './columnReducer.js';
export const TableColumnContext = /*#__PURE__*/ createContext({});
export const useTableColumns = ()=>useContext(TableColumnContext);
export const TableColumnsProvider = ({ cellProps, children, collectionSlug, enableRowSelections = false, listPreferences })=>{
    const config = useConfig();
    const { componentMap } = useComponentMap();
    const { fieldMap } = componentMap.collections[collectionSlug];
    const collectionConfig = config.collections.find((collectionConfig)=>collectionConfig.slug === collectionSlug);
    const { admin: { defaultColumns, useAsTitle } } = collectionConfig;
    const preferenceKey = `${collectionSlug}-list`;
    const prevCollection = useRef(collectionSlug);
    const hasInitialized = useRef(false);
    const { getPreference, setPreference } = usePreferences();
    const [tableColumns, dispatchTableColumns] = useReducer(columnReducer, {}, ()=>{
        return buildColumns({
            cellProps,
            columnPreferences: listPreferences?.columns,
            defaultColumns,
            enableRowSelections,
            fieldMap,
            useAsTitle
        });
    });
    // /////////////////////////////////////
    // Get preferences on collection change
    // /////////////////////////////////////
    useEffect(()=>{
        const sync = async ()=>{
            const collectionHasChanged = prevCollection.current !== collectionSlug;
            if (collectionHasChanged) {
                const currentPreferences = await getPreference(preferenceKey);
                prevCollection.current = collectionSlug;
                if (currentPreferences.columns) {
                    dispatchTableColumns({
                        type: 'set',
                        payload: {
                            columns: buildColumns({
                                cellProps,
                                columnPreferences: currentPreferences?.columns,
                                defaultColumns,
                                enableRowSelections: true,
                                fieldMap,
                                useAsTitle
                            })
                        }
                    });
                }
            }
        };
        sync();
    }, [
        preferenceKey,
        getPreference,
        collectionSlug,
        fieldMap,
        cellProps,
        defaultColumns,
        useAsTitle
    ]);
    // /////////////////////////////////////
    // Set preferences on column change
    // /////////////////////////////////////
    useEffect(()=>{
        if (!hasInitialized.current) {
            hasInitialized.current = true;
            return;
        }
        const columns = tableColumns.map((c)=>({
                accessor: c?.accessor,
                active: c?.active
            }));
        void setPreference(preferenceKey, {
            columns
        }, true);
    }, [
        tableColumns,
        preferenceKey,
        setPreference
    ]);
    const setActiveColumns = useCallback((columns)=>{
    // dispatchTableColumns({
    //   type: 'set',
    //   payload: {
    //     // onSelect,
    //     cellProps,
    //     collection: { ...collectionConfig, fields: formatFields(collectionConfig) },
    //     columns: columns.map((column) => ({
    //       accessor: column,
    //       active: true,
    //     })),
    //     i18n,
    //   },
    // })
    }, [
        collectionConfig,
        cellProps
    ]);
    const moveColumn = useCallback((args)=>{
        const { fromIndex, toIndex } = args;
        dispatchTableColumns({
            type: 'move',
            payload: {
                fromIndex,
                toIndex
            }
        });
    }, [
        dispatchTableColumns
    ]);
    const toggleColumn = useCallback((column)=>{
        dispatchTableColumns({
            type: 'toggle',
            payload: {
                column
            }
        });
    }, [
        dispatchTableColumns
    ]);
    return /*#__PURE__*/ React.createElement(TableColumnContext.Provider, {
        value: {
            columns: tableColumns,
            dispatchTableColumns,
            moveColumn,
            setActiveColumns,
            toggleColumn
        }
    }, children);
};

//# sourceMappingURL=index.js.map