/**
 * Decide where a new send_back message should land in the form's tasks array.
 *
 * If there is already an open clarifications task (one whose `parent_task_id`
 * matches and whose `status !== 'completed'`), append the message to that task.
 * Otherwise, ask the caller to build a fresh clarifications task and append it
 * to the array.
 *
 * Pure: returns a new array; never mutates the input.
 */
export function merge_send_back_message(tasks, parent_task_id, new_msg, build_new_task) {
    const existing_index = tasks.findIndex(t => t.parent_task_id === parent_task_id && t.status !== 'completed');
    if (existing_index === -1) {
        return [...tasks, build_new_task(new_msg)];
    }
    const existing = tasks[existing_index];
    const updated = {
        ...existing,
        thread: [...existing.thread, new_msg],
    };
    return tasks.map((t, i) => (i === existing_index ? updated : t));
}
//# sourceMappingURL=merge_send_back.js.map