import { useCallback, useState } from "react";
import { getNextPendingTask, loadTasksSync, markTaskInProgress, saveTasksSync, } from "../../core/task-store.js";
export function useTaskPickerController({ displayedCwd, onStartTask, onRunAllTasksChange, }) {
    const [open, setOpen] = useState(false);
    const [tasks, setTasks] = useState(() => loadTasksSync(displayedCwd));
    const refresh = useCallback(() => setTasks(loadTasksSync(displayedCwd)), [displayedCwd]);
    const close = useCallback(() => setOpen(false), []);
    const openPicker = useCallback(() => {
        setTasks(loadTasksSync(displayedCwd));
        setOpen(true);
    }, [displayedCwd]);
    const toggle = useCallback(() => {
        setTasks(loadTasksSync(displayedCwd));
        setOpen((current) => !current);
    }, [displayedCwd]);
    const start = useCallback((task) => {
        setOpen(false);
        markTaskInProgress(displayedCwd, task.id);
        refresh();
        onStartTask(task.title, task.prompt, task.id);
    }, [displayedCwd, onStartTask, refresh]);
    const runAll = useCallback((task) => {
        setOpen(false);
        onRunAllTasksChange(true);
        const selected = task
            ? { id: task.id, title: task.title, prompt: task.prompt || task.text || task.title }
            : getNextPendingTask(displayedCwd);
        if (!selected)
            return;
        markTaskInProgress(displayedCwd, selected.id);
        refresh();
        onStartTask(selected.title, selected.prompt, selected.id);
    }, [displayedCwd, onRunAllTasksChange, onStartTask, refresh]);
    const deleteTask = useCallback((task) => {
        const nextTasks = loadTasksSync(displayedCwd).filter((candidate) => candidate.id !== task.id);
        saveTasksSync(displayedCwd, nextTasks);
        setTasks(nextTasks);
    }, [displayedCwd]);
    return { open, tasks, close, openPicker, toggle, start, runAll, deleteTask };
}
//# sourceMappingURL=useTaskPickerController.js.map