import { a as normalizeLowercaseStringOrEmpty, c as normalizeOptionalString } from "./string-coerce-mnp54Vah.js";
import { c as parseAgentSessionKey } from "./session-key-utils-Bx3apsJ3.js";
import "./string-coerce-runtime-CmKEt2-v.js";
import "./routing-CMPOdbWm.js";
import { r as createChannelApprovalCapability } from "./approval-delivery-helpers-9H-Zo5hK.js";
import "./approval-delivery-runtime-BG5kfKW7.js";
import { n as createLazyChannelApprovalNativeRuntimeAdapter } from "./approval-handler-adapter-runtime-DLWtIqHq.js";
import { a as createNativeApprovalForwardingFallbackSuppressor, c as shouldSuppressLocalNativeExecApprovalPrompt, i as createNativeApprovalChannelRouteGates, n as createChannelApproverDmTargetResolver, r as createChannelNativeOriginTargetResolver } from "./approval-native-helpers-BmJHB1I4.js";
import "./approval-native-runtime-GsJ3sx06.js";
import { a as buildApprovalReactionPendingContentForRequest } from "./approval-reaction-runtime-D4V1vU_G.js";
import { i as resolveSignalAccount, n as listSignalAccountIds, r as resolveDefaultSignalAccountId } from "./accounts-_x5BVriQ.js";
import { d as normalizeSignalMessagingTarget } from "./identity-C9k0AVBB.js";
import { i as signalApprovalAuth, r as getSignalApprovalApprovers } from "./format-CTWPwKgx.js";
//#region extensions/signal/src/approval-native.ts
const DEFAULT_APPROVAL_FORWARDING_MODE = "session";
function isSignalApprovalTransportEnabled(params) {
	return resolveSignalAccount({
		cfg: params.cfg,
		accountId: params.accountId
	}).enabled;
}
function normalizeSignalForwardTarget(target) {
	if (normalizeLowercaseStringOrEmpty(target.channel) !== "signal") return null;
	const to = normalizeSignalMessagingTarget(target.to);
	if (!to) return null;
	return {
		to,
		accountId: normalizeOptionalString(target.accountId),
		threadId: target.threadId ?? null
	};
}
function resolveTurnSourceSignalOriginTarget(request) {
	if (normalizeLowercaseStringOrEmpty(request.request.turnSourceChannel) !== "signal") return null;
	const to = normalizeSignalMessagingTarget(request.request.turnSourceTo ?? "");
	if (!to) return null;
	return {
		to,
		accountId: normalizeOptionalString(request.request.turnSourceAccountId)
	};
}
function resolveSessionSignalOriginTarget(sessionTarget) {
	const to = normalizeSignalMessagingTarget(sessionTarget.to);
	return to ? {
		to,
		accountId: normalizeOptionalString(sessionTarget.accountId)
	} : null;
}
const { canApprovalPotentiallyRouteToChannel: canApprovalPotentiallyRouteToSignal, canAnyApprovalPotentiallyRouteToChannel: canAnyApprovalPotentiallyRouteToSignal, isNativeApprovalHandlerConfigured: isSignalNativeApprovalHandlerConfiguredBase, isSessionApprovalEligible: isSignalSessionApprovalEligible, shouldHandleApprovalRequest: shouldHandleSignalApprovalRequest } = createNativeApprovalChannelRouteGates({
	channel: "signal",
	defaultForwardingMode: DEFAULT_APPROVAL_FORWARDING_MODE,
	isTransportEnabled: isSignalApprovalTransportEnabled,
	listAccountIds: listSignalAccountIds,
	resolveDefaultAccountId: resolveDefaultSignalAccountId,
	normalizeForwardTarget: normalizeSignalForwardTarget,
	resolveTurnSourceTarget: resolveTurnSourceSignalOriginTarget
});
function isSignalNativeApprovalHandlerConfigured(params) {
	return isSignalNativeApprovalHandlerConfiguredBase(params);
}
function resolveSignalSessionTargetFromSessionKey(sessionKey) {
	const rest = parseAgentSessionKey(sessionKey)?.rest ?? normalizeOptionalString(sessionKey);
	if (!rest || !normalizeLowercaseStringOrEmpty(rest).startsWith("signal:")) return null;
	return normalizeSignalMessagingTarget(rest.slice(7)) ?? null;
}
function shouldSuppressLocalSignalExecApprovalPrompt(params) {
	return shouldSuppressLocalNativeExecApprovalPrompt({
		...params,
		isTransportEnabled: isSignalApprovalTransportEnabled,
		isSessionRouteEligible: ({ cfg, accountId, metadata }) => {
			if (getSignalApprovalApprovers({
				cfg,
				accountId
			}).length > 0) return true;
			const sessionTarget = resolveSignalSessionTargetFromSessionKey(metadata.sessionKey);
			return Boolean(sessionTarget && !isSignalGroupTarget(sessionTarget));
		}
	});
}
const resolveSignalOriginTargetBase = createChannelNativeOriginTargetResolver({
	channel: "signal",
	shouldHandleRequest: shouldHandleSignalApprovalRequest,
	resolveTurnSourceTarget: resolveTurnSourceSignalOriginTarget,
	resolveSessionTarget: resolveSessionSignalOriginTarget,
	normalizeTarget: (target) => {
		const to = normalizeSignalMessagingTarget(target.to);
		return to ? {
			...target,
			to
		} : null;
	}
});
function isSignalGroupTarget(to) {
	return normalizeLowercaseStringOrEmpty(to).startsWith("group:");
}
function resolveSignalOriginTarget(params) {
	const target = resolveSignalOriginTargetBase(params);
	if (!target) return null;
	if (isSignalGroupTarget(target.to) && getSignalApprovalApprovers({
		cfg: params.cfg,
		accountId: params.accountId
	}).length === 0) return null;
	return target;
}
const resolveSignalApproverDmTargets = createChannelApproverDmTargetResolver({
	shouldHandleRequest: shouldHandleSignalApprovalRequest,
	resolveApprovers: getSignalApprovalApprovers,
	mapApprover: (approver, params) => {
		const to = normalizeSignalMessagingTarget(approver);
		if (!to) return null;
		return {
			to,
			accountId: normalizeOptionalString(params.accountId)
		};
	}
});
const shouldSuppressSignalForwardingFallback = createNativeApprovalForwardingFallbackSuppressor({
	channel: "signal",
	normalizeForwardTarget: normalizeSignalForwardTarget,
	resolveAccountId: ({ forwardingTarget, request }) => forwardingTarget.accountId ?? normalizeOptionalString(request.request.turnSourceAccountId),
	resolveForwardingTargetForMatch: ({ forwardingTarget, accountId }) => ({
		...forwardingTarget,
		accountId
	}),
	isSessionRouteEligible: isSignalSessionApprovalEligible,
	resolveOriginTarget: resolveSignalOriginTarget,
	resolveApproverDmTargets: resolveSignalApproverDmTargets
});
function buildSignalExecPendingPayload(params) {
	return buildApprovalReactionPendingContentForRequest(params).manualFallbackPayload;
}
function buildSignalPluginPendingPayload(params) {
	return buildApprovalReactionPendingContentForRequest(params).manualFallbackPayload;
}
const signalApprovalCapability = createChannelApprovalCapability({
	...signalApprovalAuth,
	getActionAvailabilityState: ({ cfg, accountId, approvalKind }) => (approvalKind ? canApprovalPotentiallyRouteToSignal({
		cfg,
		accountId,
		approvalKind
	}) : canAnyApprovalPotentiallyRouteToSignal({
		cfg,
		accountId
	})) ? { kind: "enabled" } : { kind: "disabled" },
	getExecInitiatingSurfaceState: ({ cfg, accountId }) => canApprovalPotentiallyRouteToSignal({
		cfg,
		accountId,
		approvalKind: "exec"
	}) ? { kind: "enabled" } : { kind: "disabled" },
	describeExecApprovalSetup: ({ accountId }) => {
		return `Signal supports native exec approvals for this account when \`approvals.exec.enabled\` is true and the route allows Signal. Link Signal and keep the gateway running; configure \`${accountId && accountId !== "default" ? `channels.signal.accounts.${accountId}` : "channels.signal"}.allowFrom\` to restrict approvers.`;
	},
	delivery: {
		hasConfiguredDmRoute: ({ cfg }) => listSignalAccountIds(cfg).some((accountId) => {
			if (!canAnyApprovalPotentiallyRouteToSignal({
				cfg,
				accountId,
				nativeSessionOnly: true
			})) return false;
			return getSignalApprovalApprovers({
				cfg,
				accountId
			}).length > 0;
		}),
		shouldSuppressForwardingFallback: shouldSuppressSignalForwardingFallback
	},
	render: {
		exec: { buildPendingPayload: ({ request, nowMs }) => buildSignalExecPendingPayload({
			request,
			nowMs
		}) },
		plugin: { buildPendingPayload: ({ request, nowMs }) => buildSignalPluginPendingPayload({
			request,
			nowMs
		}) }
	},
	native: {
		describeDeliveryCapabilities: ({ cfg, accountId, approvalKind, request }) => {
			const originTarget = resolveSignalOriginTarget({
				cfg,
				accountId,
				approvalKind,
				request
			});
			const approverTargets = resolveSignalApproverDmTargets({
				cfg,
				accountId,
				approvalKind,
				request
			});
			return {
				enabled: Boolean(originTarget) || approverTargets.length > 0,
				preferredSurface: originTarget ? "origin" : "approver-dm",
				supportsOriginSurface: Boolean(originTarget),
				supportsApproverDmSurface: approverTargets.length > 0,
				notifyOriginWhenDmOnly: true
			};
		},
		resolveOriginTarget: resolveSignalOriginTarget,
		resolveApproverDmTargets: resolveSignalApproverDmTargets
	},
	nativeRuntime: createLazyChannelApprovalNativeRuntimeAdapter({
		eventKinds: ["exec", "plugin"],
		isConfigured: ({ cfg, accountId, context }) => Boolean(context) && isSignalNativeApprovalHandlerConfigured({
			cfg,
			accountId
		}),
		shouldHandle: ({ cfg, accountId, context, request }) => Boolean(context) && shouldHandleSignalApprovalRequest({
			cfg,
			accountId,
			request
		}),
		load: async () => (await import("./approval-handler.runtime-B9LWn9a-.js")).signalApprovalNativeRuntime
	})
});
//#endregion
export { shouldSuppressLocalSignalExecApprovalPrompt as n, signalApprovalCapability as r, isSignalNativeApprovalHandlerConfigured as t };
