import { useUserState } from 'state/useUserState';

type PinnedFile = { path: string; name: string; pinnedAt: number };

export const usePinnedFiles = () => {
  const [pinned, setPinned] = useUserState<PinnedFile[]>('pinnedFiles', []);

  const togglePin = (path: string, name: string) => {
    setPinned((prev) => {
      const exists = prev.some((f) => f.path === path);
      if (exists) return prev.filter((f) => f.path !== path);
      return [{ path, name, pinnedAt: Date.now() }, ...prev];
    });
  };

  const isPinned = (path: string) => pinned.some((f) => f.path === path);

  return { pinned, togglePin, isPinned };
};
