"use client";

import { useState, useEffect } from "react";
import {
  getProjectPath,
  setProjectPath as saveProjectPath,
} from "@/lib/project/storage";

/**
 * React hook for project path with localStorage persistence
 *
 * Usage:
 *   const { projectPath, setProjectPath, isLoading } = useProjectPath();
 */
export function useProjectPath() {
  const [projectPath, setProjectPathState] = useState<string>("");
  const [isLoading, setIsLoading] = useState(true);

  // Load from localStorage on mount (client-side only)
  useEffect(() => {
    setProjectPathState(getProjectPath());
    setIsLoading(false);
  }, []);

  // Update localStorage when path changes
  const setProjectPath = (path: string) => {
    const trimmed = path.trim();
    setProjectPathState(trimmed);
    saveProjectPath(trimmed);
  };

  return {
    projectPath,
    setProjectPath,
    isLoading,
  };
}
