import { useState, useEffect, useCallback, useMemo } from 'react';

function useTableColumns({ columns, initialVisibleColumns, onVisibleColumnsChange, onColumnReorder, }) {
    const defaultVisibleColumns = initialVisibleColumns || (columns ? columns.map((col) => col.id) : []);
    const defaultColumnOrder = columns ? columns.map((col) => col.id) : [];
    const [visibleColumns, setVisibleColumns] = useState(defaultVisibleColumns);
    const [columnOrder, setColumnOrder] = useState(defaultColumnOrder);
    // Sync column order when columns prop changes
    useEffect(() => {
        if (columns) {
            setColumnOrder(columns.map((col) => col.id));
        }
    }, [columns]);
    const handleVisibleColumnsChange = useCallback((newVisibleColumns) => {
        setVisibleColumns(newVisibleColumns);
        onVisibleColumnsChange === null || onVisibleColumnsChange === void 0 ? void 0 : onVisibleColumnsChange(newVisibleColumns);
    }, [onVisibleColumnsChange]);
    const handleToggleColumn = useCallback((columnId) => {
        const column = columns === null || columns === void 0 ? void 0 : columns.find(col => col.id === columnId);
        if (column === null || column === void 0 ? void 0 : column.locked)
            return;
        setVisibleColumns(prev => {
            const newVisibleColumns = prev.includes(columnId)
                ? prev.filter((id) => id !== columnId)
                : [...prev, columnId];
            onVisibleColumnsChange === null || onVisibleColumnsChange === void 0 ? void 0 : onVisibleColumnsChange(newVisibleColumns);
            return newVisibleColumns;
        });
    }, [columns, onVisibleColumnsChange]);
    const handleColumnReorder = useCallback((fromIndex, toIndex) => {
        setColumnOrder(prev => {
            const newColumnOrder = [...prev];
            const [movedColumn] = newColumnOrder.splice(fromIndex, 1);
            newColumnOrder.splice(toIndex, 0, movedColumn);
            return newColumnOrder;
        });
        onColumnReorder === null || onColumnReorder === void 0 ? void 0 : onColumnReorder(fromIndex, toIndex);
    }, [onColumnReorder]);
    const handleResetToDefault = useCallback(() => {
        const lockedColumnIds = (columns === null || columns === void 0 ? void 0 : columns.filter(col => col.locked).map(col => col.id)) || [];
        const resetVisibleColumns = Array.from(new Set([...defaultVisibleColumns, ...lockedColumnIds]));
        handleVisibleColumnsChange(resetVisibleColumns);
        if (columns) {
            setColumnOrder(columns.map((col) => col.id));
        }
    }, [columns, defaultVisibleColumns, handleVisibleColumnsChange]);
    const visibleColumnsList = useMemo(() => {
        if (!columns)
            return [];
        if (columnOrder.length > 0) {
            return columnOrder
                .map(colId => columns.find(col => col.id === colId))
                .filter((col) => col !== undefined && visibleColumns.includes(col.id));
        }
        return columns.filter((col) => visibleColumns.includes(col.id));
    }, [columns, columnOrder, visibleColumns]);
    return {
        visibleColumns,
        columnOrder,
        visibleColumnsList,
        handleToggleColumn,
        handleColumnReorder,
        handleResetToDefault,
    };
}

export { useTableColumns };
