import { failNativeRequired, isNativeDisabledByEnv, } 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 parseRecord(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
            return null;
        }
        return parsed;
    }
    catch {
        return null;
    }
}
export function normalizeResponsePayloadWithNative(payload, config) {
    const capability = "normalizeResponsePayloadJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail("native disabled");
    }
    const fn = readNativeFunction(capability);
    if (!fn) {
        return fail();
    }
    const payloadJson = safeStringify(payload);
    const configJson = config ? safeStringify(config) : "{}";
    if (!payloadJson || !configJson) {
        return fail("json stringify failed");
    }
    try {
        const raw = fn(payloadJson, configJson);
        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 validateResponsePayloadWithNative(payload) {
    const capability = "validateResponsePayloadJson";
    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 {
        fn(payloadJson);
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? "unknown");
        throw new Error(reason);
    }
}
export function applyRequestRulesWithNative(payload, config) {
    const capability = "applyRequestRulesJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const configJson = config ? safeStringify(config) : "{}";
    if (!payloadJson || !configJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, configJson);
        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 applyResponseBlacklistWithNative(payload, config) {
    const capability = "applyResponseBlacklistJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const configJson = config ? safeStringify(config) : "{}";
    if (!payloadJson || !configJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, configJson);
        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 normalizeToolCallIdsWithNative(payload) {
    const capability = "normalizeToolCallIdsJson";
    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 function enforceLmstudioResponsesFcToolCallIdsWithNative(payload) {
    const capability = "enforceLmstudioResponsesFcToolCallIdsJson";
    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 function applyAnthropicClaudeCodeUserIdWithNative(payload, adapterContext) {
    const capability = "applyAnthropicClaudeCodeUserIdJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const contextJson = adapterContext ? safeStringify(adapterContext) : "{}";
    if (!payloadJson || !contextJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, contextJson);
        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 applyGeminiWebSearchRequestCompatWithNative(payload, adapterContext) {
    const capability = "applyGeminiWebSearchRequestCompatJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const contextJson = adapterContext ? safeStringify(adapterContext) : "{}";
    if (!payloadJson || !contextJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, contextJson);
        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 prepareAntigravityThoughtSignatureForGeminiRequestWithNative(payload, adapterContext) {
    const capability = "prepareAntigravityThoughtSignatureForGeminiRequestJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const contextJson = adapterContext ? safeStringify(adapterContext) : "{}";
    if (!payloadJson || !contextJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, contextJson);
        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 applyIflowToolTextFallbackWithNative(payload, adapterContext, models) {
    const capability = "applyIflowToolTextFallbackJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const contextJson = adapterContext ? safeStringify(adapterContext) : "{}";
    const modelsJson = safeStringify(Array.isArray(models) ? models : []);
    if (!payloadJson || !contextJson || !modelsJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, contextJson, modelsJson);
        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 applyToolTextRequestGuidanceWithNative(payload, config) {
    const capability = "applyToolTextRequestGuidanceJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const configJson = config ? safeStringify(config) : "{}";
    if (!payloadJson || !configJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, configJson);
        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 applyUniversalShapeRequestFilterWithNative(payload, config) {
    const capability = "applyUniversalShapeRequestFilterJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const configJson = config ? safeStringify(config) : "{}";
    if (!payloadJson || !configJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, configJson);
        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 applyUniversalShapeResponseFilterWithNative(payload, config, adapterContext) {
    const capability = "applyUniversalShapeResponseFilterJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const configJson = config ? safeStringify(config) : "{}";
    const contextJson = adapterContext ? safeStringify(adapterContext) : "{}";
    if (!payloadJson || !configJson || !contextJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, configJson, contextJson);
        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 buildOpenAIChatFromAnthropicWithNative(payload, options) {
    const capability = "buildOpenaiChatFromAnthropicJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const optionsJson = options ? safeStringify(options) : "{}";
    if (!payloadJson || !optionsJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, optionsJson);
        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 buildAnthropicFromOpenAIChatWithNative(payload, options) {
    const capability = "buildAnthropicFromOpenaiChatJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const optionsJson = options ? safeStringify(options) : "{}";
    if (!payloadJson || !optionsJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, optionsJson);
        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 runOpenAIRequestCodecWithNative(payload, options) {
    const capability = "runOpenaiOpenaiRequestCodecJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const optionsJson = options ? safeStringify(options) : "{}";
    if (!payloadJson || !optionsJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, optionsJson);
        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 runOpenAIResponseCodecWithNative(payload, options) {
    const capability = "runOpenaiOpenaiResponseCodecJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const optionsJson = options ? safeStringify(options) : "{}";
    if (!payloadJson || !optionsJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, optionsJson);
        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 runResponsesOpenAIRequestCodecWithNative(payload, options) {
    const capability = "runResponsesOpenaiRequestCodecJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const optionsJson = options ? safeStringify(options) : "{}";
    if (!payloadJson || !optionsJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, optionsJson);
        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 runResponsesOpenAIResponseCodecWithNative(payload, context) {
    const capability = "runResponsesOpenaiResponseCodecJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const contextJson = safeStringify(context);
    if (!payloadJson || !contextJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, contextJson);
        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 runGeminiOpenAIRequestCodecWithNative(payload, options) {
    const capability = "runGeminiOpenaiRequestCodecJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const optionsJson = options ? safeStringify(options) : "{}";
    if (!payloadJson || !optionsJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, optionsJson);
        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 runGeminiOpenAIResponseCodecWithNative(payload, options) {
    const capability = "runGeminiOpenaiResponseCodecJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const optionsJson = options ? safeStringify(options) : "{}";
    if (!payloadJson || !optionsJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, optionsJson);
        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 runGeminiFromOpenAIChatCodecWithNative(payload, options) {
    const capability = "runGeminiFromOpenaiChatCodecJson";
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv())
        return fail("native disabled");
    const fn = readNativeFunction(capability);
    if (!fn)
        return fail();
    const payloadJson = safeStringify(payload);
    const optionsJson = options ? safeStringify(options) : "{}";
    if (!payloadJson || !optionsJson)
        return fail("json stringify failed");
    try {
        const raw = fn(payloadJson, optionsJson);
        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);
    }
}
