export class RelayPlacementError extends Error {
    code;
    capability;
    node;
    repo;
    attempts;
    constructor(code, message, context) {
        super(message);
        this.name = 'RelayPlacementError';
        this.code = code;
        this.capability = context.capability;
        this.node = context.node;
        this.repo = context.repo;
        this.attempts = context.attempts;
    }
}
export function nonEmptyPlacement(value, label) {
    const trimmed = value.trim();
    if (!trimmed)
        throw new Error(`${label} is required.`);
    return trimmed;
}
export function placementActionName(capability) {
    return capability.startsWith('spawn:') ? 'spawn' : capability;
}
export function placementActionInput(input, placement) {
    const payload = { ...(input ?? {}) };
    payload.capability = placement.capability;
    payload.node = placement.node;
    payload.target_node = placement.node;
    if (placement.repo)
        payload.repo = placement.repo;
    if (placement.ttlMs > 0) {
        payload.ttl_override_ms = placement.ttlMs;
    }
    if (placement.capability.startsWith('spawn:')) {
        // The broker picks the harness from `cli`, but node eligibility was gated on
        // the `spawn:<cli>` capability. An explicit, mismatched `cli` would select a
        // harness the chosen node never advertised — reject it instead of silently
        // dispatching the wrong harness.
        const capabilityCli = placement.capability.slice('spawn:'.length);
        if (typeof payload.cli === 'string' && payload.cli !== capabilityCli) {
            throw new RelayPlacementError('capability_mismatch', `Placement rejected: input cli "${payload.cli}" does not match capability "${placement.capability}"`, { capability: placement.capability, node: placement.node, repo: placement.repo, attempts: 0 });
        }
        payload.cli = capabilityCli;
    }
    return payload;
}
export function delay(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}
//# sourceMappingURL=relaycast-placement.js.map