import { useMemory } from "../MemoryProvider/useMemory";

export interface Draft {
    dialog: string;
    message: string;
}

export function useDrafts(dialog: string) {
    const [drafts, setDrafts] = useMemory<Draft[]>("drafts", [], true);

    const getDraft = (): string => {
        const draft = drafts.find(d => d.dialog === dialog);
        return draft ? draft.message : "";
    };

    const saveDraft = (message: string) => {
        setDrafts(prevDrafts => {
            const otherDrafts = prevDrafts.filter(d => d.dialog !== dialog);
            return [...otherDrafts, { dialog, message }];
        });
    };

    const deleteDraft = () => {
        setDrafts(prevDrafts => prevDrafts.filter(d => d.dialog !== dialog));
    };

    return {
        getDraft,
        saveDraft,
        deleteDraft,
    };
}