"use client"

import { create } from "zustand"
import type { AiChatContext } from "@/lib/ai/chat-context"

type AiPanelState = {
  open: boolean
  context: AiChatContext
  openPanel: (context?: Partial<AiChatContext>) => void
  closePanel: () => void
  setContext: (patch: Partial<AiChatContext>) => void
}

const DEFAULT_CONTEXT: AiChatContext = {
  app: "standalone",
  temporary: true,
}

export const useAiPanelStore = create<AiPanelState>((set) => ({
  open: false,
  context: DEFAULT_CONTEXT,
  openPanel: (patch) =>
    set((s) => ({
      open: true,
      context: { ...s.context, ...patch, temporary: patch?.temporary ?? true },
    })),
  closePanel: () => set({ open: false }),
  setContext: (patch) =>
    set((s) => ({
      context: { ...s.context, ...patch },
    })),
}))
