import { failNativeRequired } from './native-router-hotpath-policy.js';
import { loadNativeRouterHotpathBindingForInternalUse } from './native-router-hotpath.js';
function readNativeFunction(name) {
    const binding = loadNativeRouterHotpathBindingForInternalUse();
    const fn = binding?.[name];
    return typeof fn === 'function' ? fn : null;
}
function normalizeMode(value) {
    if (value !== 'on' && value !== 'off') {
        return undefined;
    }
    return value;
}
function parseStopMessageNativePayload(raw) {
    let parsed = null;
    try {
        parsed = JSON.parse(raw);
    }
    catch {
        return null;
    }
    if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
        return null;
    }
    const kindRaw = parsed.kind;
    if (kindRaw !== 'set' && kindRaw !== 'clear') {
        return null;
    }
    const kind = kindRaw;
    if (kind === 'clear') {
        return { kind: 'clear' };
    }
    const text = typeof parsed.text === 'string' ? parsed.text : '';
    const maxRepeats = typeof parsed.maxRepeats === 'number' && Number.isFinite(parsed.maxRepeats)
        ? Math.floor(parsed.maxRepeats)
        : 10;
    if (!text || maxRepeats <= 0) {
        return null;
    }
    const aiMode = normalizeMode(parsed.aiMode);
    return {
        kind: 'set',
        text,
        maxRepeats,
        ...(aiMode ? { aiMode } : {})
    };
}
export function parseStopMessageInstructionWithNative(instruction) {
    const capability = 'parseStopMessageInstructionJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    const fn = readNativeFunction('parseStopMessageInstructionJson');
    if (!fn) {
        return fail();
    }
    try {
        const result = fn(String(instruction || ''));
        if (typeof result !== 'string') {
            return fail('non-string result');
        }
        if (!result || result === 'null') {
            return null;
        }
        const parsed = parseStopMessageNativePayload(result);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
