/**
 * useThreadForm — state management hook for ThreadForm.
 * Manages data mutations, auto-save, and action dispatching.
 */
'use client';
import { useState, useCallback, useRef, useEffect } from 'react';
export function use_thread_form({ initial_data, on_form_change, auto_save_ms = 2000 }) {
    const [data, set_data] = useState(initial_data);
    const save_timer = useRef(null);
    const latest_data = useRef(data);
    latest_data.current = data;
    // Auto-save debounce
    const schedule_save = useCallback(() => {
        if (save_timer.current)
            clearTimeout(save_timer.current);
        save_timer.current = setTimeout(() => {
            on_form_change?.(latest_data.current);
        }, auto_save_ms);
    }, [on_form_change, auto_save_ms]);
    // Cleanup timer on unmount
    useEffect(() => {
        return () => {
            if (save_timer.current)
                clearTimeout(save_timer.current);
        };
    }, []);
    const add_message = useCallback((task_id, msg) => {
        set_data(prev => {
            const updated = {
                ...prev,
                updated_at: new Date().toISOString(),
                tasks: prev.tasks.map(t => t.task_id === task_id
                    ? { ...t, thread: [...t.thread, msg] }
                    : t),
            };
            return updated;
        });
        schedule_save();
    }, [schedule_save]);
    const update_task = useCallback((updated) => {
        set_data(prev => ({
            ...prev,
            updated_at: new Date().toISOString(),
            tasks: prev.tasks.map(t => t.task_id === updated.task_id ? updated : t),
        }));
        schedule_save();
    }, [schedule_save]);
    const add_task = useCallback((task) => {
        set_data(prev => ({
            ...prev,
            updated_at: new Date().toISOString(),
            tasks: [...prev.tasks, task],
        }));
        schedule_save();
    }, [schedule_save]);
    const update_selections = useCallback((selections) => {
        set_data(prev => ({
            ...prev,
            collected_data_selections: { ...prev.collected_data_selections, ...selections },
        }));
        schedule_save();
    }, [schedule_save]);
    const update_status = useCallback((status) => {
        set_data(prev => ({
            ...prev,
            status,
            updated_at: new Date().toISOString(),
        }));
        schedule_save();
    }, [schedule_save]);
    /** Edit a message's text and/or content items */
    const edit_message = useCallback((task_id, message_id, updated) => {
        set_data(prev => ({
            ...prev,
            updated_at: new Date().toISOString(),
            tasks: prev.tasks.map(t => {
                if (t.task_id !== task_id)
                    return t;
                return {
                    ...t,
                    thread: t.thread.map(m => {
                        if (m.message_id !== message_id)
                            return m;
                        return { ...m, content_text: updated.content_text, content_items: updated.content_items };
                    }),
                };
            }),
        }));
        schedule_save();
    }, [schedule_save]);
    /** Delete a message from a task's thread */
    const delete_message = useCallback((task_id, message_id) => {
        set_data(prev => ({
            ...prev,
            updated_at: new Date().toISOString(),
            tasks: prev.tasks.map(t => {
                if (t.task_id !== task_id)
                    return t;
                return { ...t, thread: t.thread.filter(m => m.message_id !== message_id) };
            }),
        }));
        schedule_save();
    }, [schedule_save]);
    /** Replace the entire form data (for operations that need full replacement) */
    const replace_data = useCallback((new_data) => {
        set_data(new_data);
        schedule_save();
    }, [schedule_save]);
    /** Update fields on a specific ContentItem (functional update — safe from stale closures) */
    const update_content_item = useCallback((content_id, updates) => {
        set_data(prev => ({
            ...prev,
            updated_at: new Date().toISOString(),
            tasks: prev.tasks.map(t => ({
                ...t,
                thread: t.thread.map(m => ({
                    ...m,
                    content_items: m.content_items?.map(ci => ci.content_id === content_id ? { ...ci, ...updates } : ci),
                })),
            })),
        }));
        schedule_save();
    }, [schedule_save]);
    return { data, add_message, update_task, add_task, update_selections, update_status, edit_message, delete_message, replace_data, update_content_item };
}
//# sourceMappingURL=use_thread_form.js.map