import { checkPolicy } from "../../runtime/policy.js";
import { approve, reject } from "../../runtime/interrupts.js";
function applyPolicy(interrupts, policy) {
    return interrupts.map((interrupt) => {
        const decision = checkPolicy(policy, interrupt);
        return decision.type === "approve" ? approve() : reject();
    });
}
export async function runWithPolicy(invoke, policyStore, handlers) {
    let result = await invoke();
    while (handlers.hasInterrupts(result)) {
        const interrupts = result;
        const responses = applyPolicy(interrupts, policyStore.get());
        result = await handlers.respondToInterrupts(interrupts, responses);
    }
    return result;
}
