import { failNativeRequired } from './native-router-hotpath-policy.js';
import { loadNativeRouterHotpathBindingForInternalUse } from './native-router-hotpath.js';
function parseToolGovernanceOutput(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return null;
        }
        const row = parsed;
        const processedRequest = row.processedRequest;
        const nodeResult = row.nodeResult;
        if (!processedRequest || typeof processedRequest !== 'object' || Array.isArray(processedRequest)) {
            return null;
        }
        if (!nodeResult || typeof nodeResult !== 'object' || Array.isArray(nodeResult)) {
            return null;
        }
        return {
            processedRequest: processedRequest,
            nodeResult: nodeResult
        };
    }
    catch {
        return null;
    }
}
export function applyReqProcessToolGovernanceWithNative(input) {
    const capability = 'applyReqProcessToolGovernanceJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    const fn = readNativeFunction('applyReqProcessToolGovernanceJson');
    if (!fn) {
        return fail();
    }
    const inputJson = safeStringify(input);
    if (!inputJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(inputJson);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseToolGovernanceOutput(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function applyHubOperationsWithNative(request, operations) {
    const capability = 'applyHubOperationsJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    const fn = readNativeFunction('applyHubOperationsJson');
    if (!fn) {
        return fail();
    }
    const requestJson = safeStringify(request);
    const operationsJson = safeStringify(operations ?? []);
    if (!requestJson || !operationsJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(requestJson, operationsJson);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseOutputRecord(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
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 parseOutput(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return null;
        }
        const row = parsed;
        const request = row.request;
        const normalizedMetadata = row.normalizedMetadata;
        if (!request || typeof request !== 'object' || Array.isArray(request)) {
            return null;
        }
        if (!normalizedMetadata || typeof normalizedMetadata !== 'object' || Array.isArray(normalizedMetadata)) {
            return null;
        }
        return {
            request: request,
            normalizedMetadata: normalizedMetadata
        };
    }
    catch {
        return null;
    }
}
function parseOutputRecord(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return null;
        }
        return parsed;
    }
    catch {
        return null;
    }
}
export function applyReqProcessRouteSelectionWithNative(input) {
    const capability = 'applyReqProcessRouteSelectionJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    const fn = readNativeFunction('applyReqProcessRouteSelectionJson');
    if (!fn) {
        return fail();
    }
    const inputJson = safeStringify(input);
    if (!inputJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(inputJson);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseOutput(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
