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 safeStringify(value) {
    try {
        return JSON.stringify(value);
    }
    catch {
        return undefined;
    }
}
function parseArrayPayload(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!Array.isArray(parsed))
            return null;
        return parsed.filter((entry) => typeof entry === 'string');
    }
    catch {
        return null;
    }
}
function parseRecordPayload(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return null;
        }
        return parsed;
    }
    catch {
        return null;
    }
}
function parseRecordArrayPayload(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!Array.isArray(parsed)) {
            return null;
        }
        const records = parsed.filter((entry) => entry && typeof entry === 'object' && !Array.isArray(entry));
        return records.length === parsed.length ? records : null;
    }
    catch {
        return null;
    }
}
export function parseRoutingInstructionKindsWithNative(request) {
    const capability = 'parseRoutingInstructionKindsJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    const fn = readNativeFunction(capability);
    if (!fn) {
        return fail();
    }
    const requestJson = safeStringify(request);
    if (!requestJson) {
        return fail('json stringify failed');
    }
    try {
        const result = fn(requestJson);
        if (typeof result !== 'string' || !result) {
            return fail('empty result');
        }
        const parsed = parseArrayPayload(result);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function parseRoutingInstructionsWithNative(messages) {
    const capability = 'parseRoutingInstructionsJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    const fn = readNativeFunction(capability);
    if (!fn) {
        return fail();
    }
    const messagesJson = safeStringify(messages);
    if (!messagesJson) {
        return fail('json stringify failed');
    }
    try {
        const result = fn(messagesJson);
        if (typeof result !== 'string' || !result) {
            return fail('empty result');
        }
        const parsed = parseRecordArrayPayload(result);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function cleanRoutingInstructionMarkersWithNative(request) {
    const capability = 'cleanRoutingInstructionMarkersJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    const fn = readNativeFunction(capability);
    if (!fn) {
        return fail();
    }
    const requestJson = safeStringify(request);
    if (!requestJson) {
        return fail('json stringify failed');
    }
    try {
        const result = fn(requestJson);
        if (typeof result !== 'string' || !result) {
            return fail('empty result');
        }
        const parsed = parseRecordPayload(result);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
