import { useCallback, useEffect, useRef, useState } from "react";
import { buildSystemPrompt } from "../../system-prompt.js";
import { applyAsyncSubagentPolicy } from "../../core/subagent-policy.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, skills, planModeRef, sessionStore, cwdRef, currentToolsRef, providerRef, modelRef, thinkingLevelRef, approvedPlanPathRef, injectedLanguagesRef, messagesRef, }) {
    const [planMode, setPlanMode] = useState(initialPlanMode);
    const planModeStateRef = useRef(planMode);
    useEffect(() => {
        planModeStateRef.current = planMode;
        if (planModeRef)
            planModeRef.current = planMode;
    }, [planMode, planModeRef]);
    const rebuildSystemPrompt = useCallback(async (options) => {
        const approvedPlanPath = options?.clearApprovedPlan
            ? undefined
            : (options?.approvedPlanPath ?? approvedPlanPathRef.current);
        const toolNames = (options?.tools ?? currentToolsRef.current).map((tool) => tool.name);
        return applyAsyncSubagentPolicy(await buildSystemPrompt(options?.cwd ?? cwdRef.current, skills, options?.planMode ?? planModeStateRef.current, approvedPlanPath, toolNames, options?.activeLanguages ?? injectedLanguagesRef.current, providerRef.current), providerRef.current, modelRef.current, thinkingLevelRef.current, toolNames);
    }, [
        skills,
        approvedPlanPathRef,
        cwdRef,
        currentToolsRef,
        providerRef,
        modelRef,
        thinkingLevelRef,
        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]);
    return {
        planMode,
        planModeStateRef,
        rebuildSystemPrompt,
        replaceSystemPrompt,
        setPlanModeAndPrompt,
    };
}
//# sourceMappingURL=useModeState.js.map