"use client";

import { create } from "zustand";
import type { MoveEmbedMode } from "@/components/documents/move-embed-picker-dialog";

type OnPick = (mode: MoveEmbedMode, targetId: string | null) => void | Promise<void>;

interface MoveEmbedPickerState {
  open: boolean;
  workspaceId: string | null;
  defaultMode: MoveEmbedMode;
  modes: MoveEmbedMode[];
  allowRoot: boolean;
  excludeIds: string[];
  onPick: OnPick | null;
  setWorkspaceId: (workspaceId: string | null) => void;
  openPicker: (args: {
    workspaceId: string | null;
    defaultMode: MoveEmbedMode;
    modes?: MoveEmbedMode[];
    allowRoot?: boolean;
    excludeIds?: string[];
    onPick: OnPick;
  }) => void;
  close: () => void;
}

export const useMoveEmbedPickerStore = create<MoveEmbedPickerState>((set) => ({
  open: false,
  workspaceId: null,
  defaultMode: "move",
  modes: ["move", "embed"],
  allowRoot: true,
  excludeIds: [],
  onPick: null,
  setWorkspaceId: (workspaceId) => set({ workspaceId }),
  openPicker: ({ workspaceId, defaultMode, modes, allowRoot, excludeIds, onPick }) =>
    set({
      open: true,
      workspaceId,
      defaultMode,
      modes: modes ?? ["move", "embed"],
      allowRoot: allowRoot ?? true,
      excludeIds: excludeIds ?? [],
      onPick,
    }),
  close: () =>
    set({
      open: false,
      onPick: null,
      excludeIds: [],
    }),
}));
