import { useCallback, useEffect, useRef, useState } from "react";
import { buildSystemPrompt } from "../../system-prompt.js";
/**
 * Owns the `planMode` runtime state and the system-prompt rebuild cluster
 * (`rebuildSystemPrompt`, `replaceSystemPrompt`, `setPlanModeAndPrompt`).
 * Extracted from `App.tsx` as a self-contained controller.
 */
export function useModeState({ initialPlanMode, initialGoalMode = "off", skills, planModeRef, goalModeRef, sessionStore, cwdRef, currentToolsRef, providerRef, approvedPlanPathRef, injectedLanguagesRef, messagesRef, }) {
    const [planMode, setPlanMode] = useState(initialPlanMode);
    const [goalMode, setGoalMode] = useState(initialGoalMode);
    const planModeStateRef = useRef(planMode);
    const goalModeStateRef = useRef(goalMode);
    useEffect(() => {
        planModeStateRef.current = planMode;
        if (planModeRef)
            planModeRef.current = planMode;
    }, [planMode, planModeRef]);
    useEffect(() => {
        goalModeStateRef.current = goalMode;
        if (goalModeRef)
            goalModeRef.current = goalMode;
    }, [goalMode, goalModeRef]);
    const rebuildSystemPrompt = useCallback(async (options) => {
        const approvedPlanPath = options?.clearApprovedPlan
            ? undefined
            : (options?.approvedPlanPath ?? approvedPlanPathRef.current);
        return buildSystemPrompt(options?.cwd ?? cwdRef.current, skills, options?.planMode ?? planModeStateRef.current, approvedPlanPath, (options?.tools ?? currentToolsRef.current).map((tool) => tool.name), options?.activeLanguages ?? injectedLanguagesRef.current, providerRef.current, goalModeStateRef.current);
    }, [skills, approvedPlanPathRef, cwdRef, currentToolsRef, providerRef, injectedLanguagesRef]);
    const replaceSystemPrompt = useCallback(async (options) => {
        const newPrompt = await rebuildSystemPrompt(options);
        if (messagesRef.current[0]?.role === "system") {
            messagesRef.current[0] = { role: "system", content: newPrompt };
        }
        return newPrompt;
    }, [rebuildSystemPrompt, messagesRef]);
    const setPlanModeAndPrompt = useCallback(async (nextMode) => {
        planModeStateRef.current = nextMode;
        if (planModeRef)
            planModeRef.current = nextMode;
        if (sessionStore)
            sessionStore.planMode = nextMode;
        setPlanMode(nextMode);
        await replaceSystemPrompt({ planMode: nextMode });
    }, [planModeRef, sessionStore, replaceSystemPrompt]);
    const setGoalModeAndPrompt = useCallback(async (nextMode) => {
        goalModeStateRef.current = nextMode;
        if (goalModeRef)
            goalModeRef.current = nextMode;
        if (sessionStore)
            sessionStore.goalMode = nextMode;
        setGoalMode(nextMode);
        await replaceSystemPrompt();
    }, [goalModeRef, sessionStore, replaceSystemPrompt]);
    return {
        planMode,
        goalMode,
        planModeStateRef,
        goalModeStateRef,
        rebuildSystemPrompt,
        replaceSystemPrompt,
        setPlanModeAndPrompt,
        setGoalModeAndPrompt,
    };
}
//# sourceMappingURL=useModeState.js.map