import { failNativeRequired } from './native-router-hotpath-policy.js';
import { loadNativeRouterHotpathBindingForInternalUse } from './native-router-hotpath.js';
const STOP_MESSAGE_FIELDS = new Set([
    'stopMessageSource',
    'stopMessageText',
    'stopMessageMaxRepeats',
    'stopMessageUsed',
    'stopMessageUpdatedAt',
    'stopMessageLastUsedAt',
    'stopMessageStageMode',
    'stopMessageAiMode',
    'stopMessageAiSeedPrompt',
    'stopMessageAiHistory'
]);
function readNativeFunction(name) {
    const binding = loadNativeRouterHotpathBindingForInternalUse();
    const fn = binding?.[name];
    return typeof fn === 'function' ? fn : null;
}
function safeStringify(value) {
    try {
        return JSON.stringify(value);
    }
    catch {
        return undefined;
    }
}
function parseActionPatchPayload(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return null;
        }
        const row = parsed;
        if (typeof row.applied !== 'boolean') {
            return null;
        }
        const setRaw = row.set;
        const unsetRaw = row.unset;
        const normalizedSet = {};
        if (setRaw && typeof setRaw === 'object' && !Array.isArray(setRaw)) {
            for (const [key, value] of Object.entries(setRaw)) {
                if (value !== undefined) {
                    normalizedSet[key] = value;
                }
            }
        }
        const normalizedUnset = Array.isArray(unsetRaw)
            ? unsetRaw.filter((key) => typeof key === 'string')
            : [];
        return {
            applied: row.applied,
            set: normalizedSet,
            unset: Array.from(new Set(normalizedUnset))
        };
    }
    catch {
        return null;
    }
}
export function applyStopMessageInstructionWithNative(instruction, state, nowMs) {
    const capability = 'applyStopMessageInstructionJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    const fn = readNativeFunction(capability);
    if (!fn) {
        return fail();
    }
    const instructionJson = safeStringify(instruction);
    const stateJson = safeStringify(state);
    if (!instructionJson || !stateJson) {
        return fail('json stringify failed');
    }
    try {
        const result = fn(instructionJson, stateJson, Math.floor(nowMs));
        if (typeof result !== 'string' || !result) {
            return fail('empty result');
        }
        const parsed = parseActionPatchPayload(result);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
