import { failNativeRequired, isNativeDisabledByEnv } from './native-router-hotpath-policy.js';
import { loadNativeRouterHotpathBindingForInternalUse } from './native-router-hotpath.js';
function parseAliasMapPayload(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (parsed === null) {
            return undefined;
        }
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return null;
        }
        const out = {};
        for (const [key, value] of Object.entries(parsed)) {
            if (!key || typeof value !== 'string') {
                return null;
            }
            out[key] = value;
        }
        return out;
    }
    catch {
        return null;
    }
}
function parseGovernanceContextPayload(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return null;
        }
        const row = parsed;
        const entryEndpoint = typeof row.entryEndpoint === 'string' ? row.entryEndpoint : '';
        const providerProtocol = typeof row.providerProtocol === 'string' ? row.providerProtocol : '';
        const inboundStreamIntent = row.inboundStreamIntent === true;
        const metadata = row.metadata && typeof row.metadata === 'object' && !Array.isArray(row.metadata)
            ? row.metadata
            : null;
        if (!entryEndpoint || !providerProtocol || !metadata) {
            return null;
        }
        const rawRequestBody = row.rawRequestBody && typeof row.rawRequestBody === 'object' && !Array.isArray(row.rawRequestBody)
            ? row.rawRequestBody
            : undefined;
        return {
            entryEndpoint,
            metadata,
            providerProtocol,
            metadataToolHints: row.metadataToolHints === null ? undefined : row.metadataToolHints,
            inboundStreamIntent,
            ...(rawRequestBody ? { rawRequestBody } : {})
        };
    }
    catch {
        return null;
    }
}
function parseCastToolsPayload(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (parsed === null) {
            return undefined;
        }
        if (!Array.isArray(parsed)) {
            return null;
        }
        return parsed;
    }
    catch {
        return null;
    }
}
function parseWebSearchOperationsPayload(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!Array.isArray(parsed)) {
            return null;
        }
        return parsed;
    }
    catch {
        return null;
    }
}
function parseRecord(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return null;
        }
        return parsed;
    }
    catch {
        return null;
    }
}
function parseRespProcessToolGovernancePayload(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return null;
        }
        const row = parsed;
        const governedPayloadRaw = row.governed_payload && typeof row.governed_payload === 'object' && !Array.isArray(row.governed_payload)
            ? row.governed_payload
            : row.governedPayload && typeof row.governedPayload === 'object' && !Array.isArray(row.governedPayload)
                ? row.governedPayload
                : null;
        const summaryRaw = row.summary && typeof row.summary === 'object' && !Array.isArray(row.summary)
            ? row.summary
            : null;
        if (!governedPayloadRaw || !summaryRaw) {
            return null;
        }
        const applied = summaryRaw.applied === true;
        const toolCallsNormalizedRaw = typeof summaryRaw.tool_calls_normalized === 'number'
            ? summaryRaw.tool_calls_normalized
            : typeof summaryRaw.toolCallsNormalized === 'number'
                ? summaryRaw.toolCallsNormalized
                : NaN;
        const applyPatchRepairedRaw = typeof summaryRaw.apply_patch_repaired === 'number'
            ? summaryRaw.apply_patch_repaired
            : typeof summaryRaw.applyPatchRepaired === 'number'
                ? summaryRaw.applyPatchRepaired
                : NaN;
        if (!Number.isFinite(toolCallsNormalizedRaw) || !Number.isFinite(applyPatchRepairedRaw)) {
            return null;
        }
        return {
            governedPayload: governedPayloadRaw,
            summary: {
                applied,
                toolCallsNormalized: Math.floor(toolCallsNormalizedRaw),
                applyPatchRepaired: Math.floor(applyPatchRepairedRaw)
            }
        };
    }
    catch {
        return null;
    }
}
function parseRespProcessFinalizePayload(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return null;
        }
        const row = parsed;
        const finalizedPayloadRaw = row.finalized_payload && typeof row.finalized_payload === 'object' && !Array.isArray(row.finalized_payload)
            ? row.finalized_payload
            : row.finalizedPayload && typeof row.finalizedPayload === 'object' && !Array.isArray(row.finalizedPayload)
                ? row.finalizedPayload
                : null;
        return finalizedPayloadRaw;
    }
    catch {
        return null;
    }
}
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 buildAnthropicToolAliasMapWithNative(sourceTools) {
    const capability = 'buildAnthropicToolAliasMapJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction('buildAnthropicToolAliasMapJson');
    if (!fn) {
        return fail();
    }
    const sourceToolsJson = safeStringify(sourceTools ?? null);
    if (!sourceToolsJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(sourceToolsJson);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseAliasMapPayload(raw);
        return parsed === null ? fail('invalid payload') : parsed;
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function resolveGovernanceContextWithNative(request, context) {
    const capability = 'resolveGovernanceContextJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction('resolveGovernanceContextJson');
    if (!fn) {
        return fail();
    }
    const requestJson = safeStringify(request ?? null);
    const contextJson = safeStringify(context ?? null);
    if (!requestJson || !contextJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(requestJson, contextJson);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseGovernanceContextPayload(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function castGovernedToolsWithNative(tools) {
    const capability = 'castGovernedToolsJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction('castGovernedToolsJson');
    if (!fn) {
        return fail();
    }
    const toolsJson = safeStringify(tools ?? null);
    if (!toolsJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(toolsJson);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseCastToolsPayload(raw);
        return parsed === null ? fail('invalid payload') : parsed;
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function buildWebSearchToolAppendOperationsWithNative(engines) {
    const capability = 'buildWebSearchToolAppendOperationsJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction('buildWebSearchToolAppendOperationsJson');
    if (!fn) {
        return fail();
    }
    const enginesJson = safeStringify(engines ?? null);
    if (!enginesJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(enginesJson);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseWebSearchOperationsPayload(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function applyGovernedControlOperationsWithNative(request, governed, inboundStreamIntent) {
    const capability = 'applyGovernedControlOperationsJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction('applyGovernedControlOperationsJson');
    if (!fn) {
        return fail();
    }
    const requestJson = safeStringify(request);
    const governedJson = safeStringify(governed);
    if (!requestJson || !governedJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(requestJson, governedJson, inboundStreamIntent === true);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseRecord(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function applyGovernedMergeRequestWithNative(request, governed, inboundStreamIntent, governanceTimestampMs) {
    const capability = 'applyGovernedMergeRequestJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction('applyGovernedMergeRequestJson');
    if (!fn) {
        return fail();
    }
    const requestJson = safeStringify(request);
    const governedJson = safeStringify(governed);
    if (!requestJson || !governedJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(requestJson, governedJson, inboundStreamIntent === true, Number.isFinite(governanceTimestampMs) ? governanceTimestampMs : Date.now());
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseRecord(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function mergeGovernanceSummaryIntoMetadataWithNative(metadata, summary) {
    const capability = 'mergeGovernanceSummaryIntoMetadataJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction('mergeGovernanceSummaryIntoMetadataJson');
    if (!fn) {
        return fail();
    }
    const metadataJson = safeStringify(metadata ?? {});
    const summaryJson = safeStringify(summary ?? {});
    if (!metadataJson || !summaryJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(metadataJson, summaryJson);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseRecord(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function finalizeGovernedRequestWithNative(request, summary) {
    const capability = 'finalizeGovernedRequestJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction('finalizeGovernedRequestJson');
    if (!fn) {
        return fail();
    }
    const requestJson = safeStringify(request ?? {});
    const summaryJson = safeStringify(summary ?? {});
    if (!requestJson || !summaryJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(requestJson, summaryJson);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseRecord(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function applyRespProcessToolGovernanceWithNative(input) {
    const capability = 'governResponseJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction('governResponseJson');
    if (!fn) {
        return fail();
    }
    const inputJson = safeStringify({
        payload: input.payload,
        client_protocol: input.clientProtocol,
        entry_endpoint: input.entryEndpoint,
        request_id: input.requestId
    });
    if (!inputJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(inputJson);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseRespProcessToolGovernancePayload(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function stripOrphanFunctionCallsTagWithNative(payload) {
    const capability = 'stripOrphanFunctionCallsTagJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction(capability);
    if (!fn) {
        return fail();
    }
    const payloadJson = safeStringify(payload);
    if (!payloadJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(payloadJson);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseRecord(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export async function finalizeRespProcessChatResponseWithNative(input) {
    const capability = 'finalizeChatResponseJson';
    const fail = async (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const fn = readNativeFunction('finalizeChatResponseJson');
    if (!fn) {
        return fail();
    }
    const inputJson = safeStringify({
        payload: input.payload,
        stream: input.stream === true,
        reasoningMode: input.reasoningMode,
        endpoint: input.endpoint,
        requestId: input.requestId
    });
    if (!inputJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(inputJson);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseRespProcessFinalizePayload(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
