import { n as GATEWAY_CLIENT_IDS } from "./client-info-UYcIi_5g.js";
import { p as isApprovalRecordVisibleToClient } from "./approval-shared-Igxpk-Ca.js";
//#region src/gateway/server-methods/approval-request-delivery.ts
function resolveFirstSuccessfulApprovalDelivery(deliveryTasks) {
	return new Promise((resolve) => {
		let remaining = deliveryTasks.length;
		for (const delivery of deliveryTasks) delivery.then((delivered) => {
			if (delivered) {
				resolve(true);
				return;
			}
			remaining -= 1;
			if (remaining === 0) resolve(false);
		});
	});
}
/** Runs external approval deliveries concurrently and reports whether any route accepted. */
function runApprovalRequestDeliveries(params) {
	const isTargetVisible = (target) => isApprovalRecordVisibleToClient({
		record: params.record,
		client: { connect: {
			client: { id: GATEWAY_CLIENT_IDS.IOS_APP },
			device: { id: target.deviceId },
			scopes: [...target.scopes]
		} }
	});
	const deliveryTasks = [params.forward, params.iosPush].flatMap((delivery) => {
		if (!delivery) return [];
		const [run, errorLabel] = delivery;
		return [run(isTargetVisible).catch((err) => {
			params.context.logGateway?.error?.(`${errorLabel}: ${String(err)}`);
			return false;
		})];
	});
	try {
		const webPushDelivery = params.context.approvalWebPushDelivery?.handleRequested(params.record);
		if (webPushDelivery !== false && webPushDelivery !== void 0) deliveryTasks.push(Promise.resolve(webPushDelivery).catch((err) => {
			params.context.logGateway?.error?.(`approval Web Push request failed: ${String(err)}`);
			return false;
		}));
	} catch (err) {
		params.context.logGateway?.error?.(`approval Web Push request failed: ${String(err)}`);
	}
	if (deliveryTasks.length === 0) return false;
	return resolveFirstSuccessfulApprovalDelivery(deliveryTasks);
}
//#endregion
export { runApprovalRequestDeliveries as t };
