"use client";

import { create } from "zustand";

type AiAgentUiState = {
  globalAgentOpen: boolean;
  globalAgentActivated: boolean;
  documentAgentAvailable: boolean;
  documentAgentOpen: boolean;
  documentAgentActivated: boolean;
  setGlobalAgentOpen: (open: boolean) => void;
  toggleGlobalAgentOpen: () => void;
  setDocumentAgentAvailable: (available: boolean) => void;
  setDocumentAgentOpen: (open: boolean) => void;
  toggleDocumentAgentOpen: () => void;
};

export const useAiAgentUiStore = create<AiAgentUiState>((set, get) => ({
  globalAgentOpen: false,
  globalAgentActivated: false,
  documentAgentAvailable: false,
  documentAgentOpen: false,
  documentAgentActivated: false,
  setGlobalAgentOpen: (open) =>
    set((state) => ({
      globalAgentOpen: open,
      globalAgentActivated: state.globalAgentActivated || open,
    })),
  toggleGlobalAgentOpen: () => {
    const s = get();
    set({
      globalAgentOpen: !s.globalAgentOpen,
      globalAgentActivated: s.globalAgentActivated || !s.globalAgentOpen,
    });
  },
  setDocumentAgentAvailable: (available) => set({ documentAgentAvailable: available }),
  setDocumentAgentOpen: (open) =>
    set((state) => ({
      documentAgentOpen: open,
      documentAgentActivated: state.documentAgentActivated || open,
    })),
  toggleDocumentAgentOpen: () => {
    const s = get();
    if (!s.documentAgentAvailable) return;
    set({
      documentAgentOpen: !s.documentAgentOpen,
      documentAgentActivated: s.documentAgentActivated || !s.documentAgentOpen,
    });
  },
}));
