import { useUserState } from 'state/useUserState';

type RecentFile = { path: string; name: string; openedAt: number };

const MAX_RECENTS = 20;

export const useRecentFiles = () => {
  const [recents, setRecents] = useUserState<RecentFile[]>('recentFiles', []);

  const addRecent = (path: string, name: string) => {
    setRecents((prev) => {
      const filtered = prev.filter((f) => f.path !== path);
      return [{ path, name, openedAt: Date.now() }, ...filtered].slice(0, MAX_RECENTS);
    });
  };

  return { recents, addRecent };
};
