import { findLastUserMessageIndex } from './chat-process-clock-reminder-messages.js';
import { extractClockReminderDirectivesWithNative } from '../../../router/virtual-router/engine-selection/native-chat-process-clock-reminder-directives.js';
import { hydrateClockScheduleDirectiveCandidate } from './chat-process-clock-directive-parser.js';
function resolveDirectivesFromCandidates(candidates) {
    const directives = [];
    let hasInvalid = false;
    for (const candidate of candidates) {
        const parsed = hydrateClockScheduleDirectiveCandidate(candidate);
        if (parsed) {
            directives.push(parsed);
        }
        else {
            hasInvalid = true;
        }
    }
    return { directives, hasInvalid };
}
function mergeResolvedBaseMessagesPreservingReferences(original, resolvedBaseMessages) {
    const lastUserIdx = findLastUserMessageIndex(original);
    const merged = original.slice();
    if (lastUserIdx >= 0) {
        const current = original[lastUserIdx];
        const resolved = resolvedBaseMessages[lastUserIdx];
        if (current && resolved) {
            merged[lastUserIdx] = { ...current, content: resolved.content };
        }
    }
    return merged;
}
export function extractClockReminderDirectives(messages) {
    const resolved = extractClockReminderDirectivesWithNative(messages);
    const resolvedDirectives = resolveDirectivesFromCandidates(resolved.directiveCandidates);
    const hasInvalidCandidate = resolvedDirectives.hasInvalid;
    const shouldKeepOriginalMessages = hasInvalidCandidate;
    return {
        hadClear: shouldKeepOriginalMessages ? false : resolved.hadClear,
        clockScheduleDirectives: shouldKeepOriginalMessages ? [] : resolvedDirectives.directives,
        baseMessages: shouldKeepOriginalMessages
            ? messages
            : mergeResolvedBaseMessagesPreservingReferences(messages, resolved.baseMessages)
    };
}
