import { useEffect, useState } from "react";
import { localStorageService } from "../../services/localStorageService";
const PROMPT_HISTORY_KEY = "editor.agent.promptHistory";
const getDraftPromptKey = (agentId) => `editor.agent.draftPrompt.${agentId}`;
export function useAgentPromptDraft(input) {
    const { agentId, isDraftOwner } = input;
    const [prompt, setPrompt] = useState(() => localStorageService.getItem(getDraftPromptKey(agentId)) || "");
    const [promptHistory, setPromptHistory] = useState(() => {
        return localStorageService.getItem(PROMPT_HISTORY_KEY) || [];
    });
    const [currentHistoryIndex, setCurrentHistoryIndex] = useState(-1);
    useEffect(() => {
        localStorageService.setItem(PROMPT_HISTORY_KEY, promptHistory);
    }, [promptHistory]);
    useEffect(() => {
        setPrompt(localStorageService.getItem(getDraftPromptKey(agentId)) || "");
        setCurrentHistoryIndex(-1);
    }, [agentId]);
    useEffect(() => {
        if (!isDraftOwner)
            return;
        const key = getDraftPromptKey(agentId);
        if (prompt) {
            localStorageService.setItem(key, prompt);
        }
        else {
            localStorageService.removeItem(key);
        }
    }, [agentId, isDraftOwner, prompt]);
    return {
        prompt,
        setPrompt,
        promptHistory,
        setPromptHistory,
        currentHistoryIndex,
        setCurrentHistoryIndex,
    };
}
//# sourceMappingURL=useAgentPromptDraft.js.map