/**
 * Column Visibility Store (Zustand)
 * Manages column visibility settings per page with localStorage persistence
 * Uses the same pattern as siteStore and sectorStore
 */
import { create } from 'zustand';
import { persist } from 'zustand/middleware';

interface ColumnVisibilityState {
  // Map of page pathname to Set of visible column keys
  pageColumns: Record<string, string[]>;
  
  // Actions
  setPageColumns: (pathname: string, columnKeys: string[]) => void;
  getPageColumns: (pathname: string) => string[];
  toggleColumn: (pathname: string, columnKey: string) => void;
  resetPageColumns: (pathname: string) => void;
}

export const useColumnVisibilityStore = create<ColumnVisibilityState>()(
  persist<ColumnVisibilityState>(
    (set, get) => ({
      pageColumns: {},
      
      setPageColumns: (pathname: string, columnKeys: string[]) => {
        set((state) => ({
          pageColumns: {
            ...state.pageColumns,
            [pathname]: columnKeys,
          },
        }));
      },
      
      getPageColumns: (pathname: string) => {
        return get().pageColumns[pathname] || [];
      },
      
      toggleColumn: (pathname: string, columnKey: string) => {
        set((state) => {
          const currentColumns = state.pageColumns[pathname] || [];
          const newColumns = currentColumns.includes(columnKey)
            ? currentColumns.filter((key) => key !== columnKey)
            : [...currentColumns, columnKey];
          
          return {
            pageColumns: {
              ...state.pageColumns,
              [pathname]: newColumns,
            },
          };
        });
      },
      
      resetPageColumns: (pathname: string) => {
        set((state) => {
          const newPageColumns = { ...state.pageColumns };
          delete newPageColumns[pathname];
          return { pageColumns: newPageColumns };
        });
      },
    }),
    {
      name: 'igny8-column-visibility',
      partialize: (state) => ({
        pageColumns: state.pageColumns,
      }),
    }
  )
);

