import { stripClockClearDirectiveText } from '../../../router/virtual-router/engine-selection/native-router-hotpath.js';
import { extractClockScheduleDirectiveTextPartsWithNative } from '../../../router/virtual-router/engine-selection/native-chat-process-clock-directive-parser.js';
import { hydrateClockScheduleDirectiveCandidate } from './chat-process-clock-directive-parser.js';
export function stripClockClearDirectiveFromText(text) {
    const analysis = stripClockClearDirectiveText(String(text || ''));
    return {
        hadClear: analysis.hadClear,
        next: analysis.next
    };
}
export function extractClockScheduleDirectivesFromText(text) {
    const raw = String(text || '');
    const parts = extractClockScheduleDirectiveTextPartsWithNative(raw);
    const directives = [];
    const nextParts = [];
    for (const part of parts) {
        if (part.kind === 'text') {
            nextParts.push(part.text);
            continue;
        }
        const parsed = part.candidate ? hydrateClockScheduleDirectiveCandidate(part.candidate) : null;
        if (!parsed) {
            nextParts.push(part.full);
            continue;
        }
        directives.push(parsed);
    }
    return {
        directives,
        next: nextParts.join('').replace(/\n{3,}/g, '\n\n').trim()
    };
}
export function extractClockScheduleDirectivesFromContent(content) {
    if (typeof content === 'string') {
        const result = extractClockScheduleDirectivesFromText(content);
        return { directives: result.directives, next: result.next };
    }
    if (Array.isArray(content)) {
        const directives = [];
        const next = content.map((part) => {
            if (typeof part === 'string') {
                const result = extractClockScheduleDirectivesFromText(part);
                if (result.directives.length) {
                    directives.push(...result.directives);
                }
                return result.next;
            }
            if (part && typeof part === 'object' && !Array.isArray(part)) {
                const block = part;
                const text = typeof block.text === 'string' ? block.text : undefined;
                if (!text) {
                    return part;
                }
                const result = extractClockScheduleDirectivesFromText(text);
                if (result.directives.length) {
                    directives.push(...result.directives);
                }
                return { ...block, text: result.next };
            }
            return part;
        });
        return { directives, next };
    }
    return { directives: [], next: content };
}
export function stripClockClearDirectiveFromContent(content) {
    if (typeof content === 'string') {
        const { hadClear, next } = stripClockClearDirectiveFromText(content);
        return { hadClear, next };
    }
    if (Array.isArray(content)) {
        let hadClear = false;
        const next = content.map((part) => {
            if (typeof part === 'string') {
                const stripped = stripClockClearDirectiveFromText(part);
                if (stripped.hadClear) {
                    hadClear = true;
                }
                return stripped.next;
            }
            if (part && typeof part === 'object' && !Array.isArray(part)) {
                const block = part;
                const text = typeof block.text === 'string' ? block.text : undefined;
                if (!text) {
                    return part;
                }
                const stripped = stripClockClearDirectiveFromText(text);
                if (stripped.hadClear) {
                    hadClear = true;
                }
                return { ...block, text: stripped.next };
            }
            return part;
        });
        return { hadClear, next };
    }
    return { hadClear: false, next: content };
}
