'use client';

import { create } from 'zustand';
import { createJSONStorage, devtools, persist } from 'zustand/middleware';

import { UIStore } from '@/types/stores';
import { createMainUiSlice } from '@/stores/ui/slices/main.slice';
import { createDrawerUISlice } from '@/stores/ui/slices/drawer.slice';
import { createTableUISlice } from '@/stores/ui/slices/table.slice';
import { createNavbarActionsSlice } from '@/stores/ui/slices/navbar.slice';

export const useUiStore = create<UIStore>()(
  devtools(
    persist(
      (...args) => ({
        ...createMainUiSlice(...args),
        ...createDrawerUISlice(...args),
        ...createTableUISlice(...args),
        ...createNavbarActionsSlice(...args),
      }),
      {
        name: 'search-store',
        storage: createJSONStorage(() => sessionStorage),
        partialize: (state) => ({
          key: state.key,
          path: state.path,
          searchValue: state.searchValue,
          previousPath: state.previousPath,
        }),
      }
    ),
    {
      name: 'UIStore',
    }
  )
);
