// stores/useAppShellStore.ts
import { create } from 'zustand';

interface AppShellState {
  activeMain: string;
  activeLink: string;
  mobileOpened: boolean;
  desktopOpened: boolean;
  setActiveMain: (main: string) => void;
  setActiveLink: (link: string) => void;
  toggleMobile: () => void;
  toggleDesktop: () => void;
  openDesktop: () => void;
  closeMobile: () => void;
}

// Persistencia en localStorage (solo para rutas, si lo quieres)
const STORAGE_KEY = 'lastSubmenuRoutes';
function getLastSubmenuRoute(section: string): string | null {
  try {
    const raw = localStorage.getItem(STORAGE_KEY);
    const parsed = raw ? JSON.parse(raw) : {};
    return parsed[section] ?? null;
  } catch {
    return null;
  }
}
function setLastSubmenuRoute(section: string, route: string) {
  try {
    const raw = localStorage.getItem(STORAGE_KEY);
    const parsed = raw ? JSON.parse(raw) : {};
    parsed[section] = route;
    localStorage.setItem(STORAGE_KEY, JSON.stringify(parsed));
  } catch {}
}

export const useAppShellStore = create<AppShellState>((set) => ({
  activeMain: 'Home',
  activeLink: '',
  mobileOpened: false,
  desktopOpened: true,
  setActiveMain: (main) => set({ activeMain: main }),
  setActiveLink: (link) => set({ activeLink: link }),
  toggleMobile: () => set((s) => ({ mobileOpened: !s.mobileOpened })),
  toggleDesktop: () => set((s) => ({ desktopOpened: !s.desktopOpened })),
  openDesktop: () => set({ desktopOpened: true }),
  closeMobile: () => set({ mobileOpened: false }),
}));

export { getLastSubmenuRoute, setLastSubmenuRoute };
