import { failNativeRequired, isNativeDisabledByEnv } from './native-router-hotpath-policy.js';
import { loadNativeRouterHotpathBindingForInternalUse } from './native-router-hotpath.js';
function parsePayload(raw) {
    try {
        const parsed = JSON.parse(raw);
        if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
            return null;
        }
        const row = parsed;
        if (typeof row.skipForServerToolFollowup !== 'boolean') {
            return null;
        }
        return {
            skipForServerToolFollowup: row.skipForServerToolFollowup
        };
    }
    catch {
        return null;
    }
}
export function resolveClockReminderFlowPlanWithNative(runtimeMetadata) {
    const capability = 'resolveClockReminderFlowPlanJson';
    const fail = (reason) => failNativeRequired(capability, reason);
    if (isNativeDisabledByEnv()) {
        return fail('native disabled');
    }
    const binding = loadNativeRouterHotpathBindingForInternalUse();
    const fn = binding?.resolveClockReminderFlowPlanJson;
    if (typeof fn !== 'function') {
        return fail();
    }
    try {
        const raw = fn(JSON.stringify(runtimeMetadata ?? {}));
        if (typeof raw !== 'string' || !raw) {
            return fail('empty result');
        }
        const parsed = parsePayload(raw);
        return parsed ?? fail('invalid payload');
    }
    catch (error) {
        const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
        return fail(reason);
    }
}
