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 parseAliasSelectionStrategy(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (parsed === 'none' || parsed === 'sticky-queue') {
            return parsed;
        }
        return null;
    }
    catch {
        return null;
    }
}
function parseAliasQueuePinOutput(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return null;
        }
        const row = parsed;
        if (!Array.isArray(row.queue) || !Array.isArray(row.selectedCandidates)) {
            return null;
        }
        const queue = row.queue.filter((v) => typeof v === 'string');
        const selectedCandidates = row.selectedCandidates.filter((v) => typeof v === 'string');
        return {
            queue,
            selectedCandidates
        };
    }
    catch {
        return null;
    }
}
export function resolveAliasSelectionStrategyWithNative(providerId, cfg) {
    const capability = 'resolveAliasSelectionStrategyJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    const fn = readNativeFunction('resolveAliasSelectionStrategyJson');
    if (!fn) {
        return fail();
    }
    const cfgJson = safeStringify(cfg ?? null);
    if (!cfgJson) {
        return fail('json stringify failed');
    }
    try {
        const raw = fn(String(providerId || ''), cfgJson);
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parseAliasSelectionStrategy(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
export function pinAliasQueueWithNative(payload) {
    const capability = 'pinAliasQueueJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    const fn = readNativeFunction('pinAliasQueueJson');
    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 = parseAliasQueuePinOutput(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
