import { failNativeRequired, isNativeDisabledByEnv } from './native-router-hotpath-policy.js';
import { loadNativeRouterHotpathBindingForInternalUse } from './native-router-hotpath.js';
function parseJson(raw) {
    return JSON.parse(raw);
}
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;
    }
}
export function buildChatProcessContextMetadataWithNative(metadata) {
    const capability = 'buildChatProcessContextMetadataJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction('buildChatProcessContextMetadataJson');
    if (!fn) {
        return fail();
    }
    const metadataJson = safeStringify(metadata ?? null);
    if (!metadataJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(metadataJson);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseJson(raw);
        if (parsed === null) {
            return undefined;
        }
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return fail('invalid payload');
        }
        return parsed;
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function applyChatProcessedRequestWithNative(request, timestampMs) {
    const capability = 'applyChatProcessedRequestJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction('applyChatProcessedRequestJson');
    if (!fn) {
        return fail();
    }
    const requestJson = safeStringify(request);
    if (!requestJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(requestJson, Number.isFinite(timestampMs) ? timestampMs : Date.now());
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseJson(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return fail('invalid payload');
        }
        return parsed;
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function buildChatProcessedDescriptorWithNative(timestampMs, streamingEnabled) {
    const capability = 'buildChatProcessedDescriptorJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction('buildChatProcessedDescriptorJson');
    if (!fn) {
        return fail();
    }
    try {
        const raw = fn(Number.isFinite(timestampMs) ? timestampMs : Date.now(), streamingEnabled === true);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseJson(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return fail('invalid payload');
        }
        return parsed;
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function buildChatNodeResultMetadataWithNative(startTimeMs, endTimeMs, messagesCount, toolsCount, includeDataProcessed) {
    const capability = 'buildChatNodeResultMetadataJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction('buildChatNodeResultMetadataJson');
    if (!fn) {
        return fail();
    }
    try {
        const raw = fn(Number.isFinite(startTimeMs) ? startTimeMs : 0, Number.isFinite(endTimeMs) ? endTimeMs : Date.now(), Math.floor(Number.isFinite(messagesCount) ? messagesCount : 0), Math.floor(Number.isFinite(toolsCount) ? toolsCount : 0), includeDataProcessed === true);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseJson(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return fail('invalid payload');
        }
        return parsed;
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function buildProcessedRequestFromChatResponseWithNative(chatResponse, streamEnabled) {
    const capability = 'buildProcessedRequestFromChatResponseJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction(capability);
    if (!fn) {
        return fail();
    }
    const payloadJson = safeStringify(chatResponse);
    if (!payloadJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(payloadJson, streamEnabled === true);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseJson(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return fail('invalid payload');
        }
        return parsed;
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
