import { startTransition, useState } from 'react';

export const useResearchSelection = () => {
  const [selectedNoteIds, setSelectedNoteIds] = useState<string[]>([]);
  const [selectedGhostId, setSelectedGhostId] = useState<string | null>(null);
  const [inspectorOpen, setInspectorOpen] = useState(true);

  const selectNote = (noteId: string, multiSelect = false) => {
    startTransition(() => {
      setSelectedGhostId(null);
      setSelectedNoteIds((current) =>
        multiSelect
          ? current.includes(noteId)
            ? current.filter((id) => id !== noteId)
            : [...current, noteId]
          : [noteId],
      );
      setInspectorOpen(true);
    });
  };

  const selectGhost = (ghostId: string) => {
    startTransition(() => {
      setSelectedNoteIds([]);
      setSelectedGhostId(ghostId);
      setInspectorOpen(true);
    });
  };

  const clearSelection = () => {
    startTransition(() => {
      setSelectedNoteIds([]);
      setSelectedGhostId(null);
    });
  };

  return {
    selectedNoteIds,
    selectedGhostId,
    inspectorOpen,
    setInspectorOpen,
    selectNote,
    selectGhost,
    clearSelection,
  };
};
