import { initialForm, type FormState } from './form-state'; import type { AlertRuleResponse } from '../../../api/queries/alertRules'; export interface PrefillWarning { field: string; message: string; } export interface PrefillOptions { targetEnvAppSlugs?: string[]; /** IDs of outbound connections allowed in the target env. */ targetEnvAllowedConnectionIds?: string[]; } /** * Client-side prefill when promoting a rule from another env. Emits warnings for * fields that cross env boundaries (agent IDs, apps missing in target env, * outbound connections not allowed in target env). */ export function prefillFromPromotion( source: AlertRuleResponse, opts: PrefillOptions = {}, ): { form: FormState; warnings: PrefillWarning[] } { const form = initialForm(source); form.name = `${source.name ?? 'rule'} (copy)`; const warnings: PrefillWarning[] = []; // Agent IDs are per-env, can't transfer. if (form.agentId) { warnings.push({ field: 'scope.agentId', message: `Agent \`${form.agentId}\` is specific to the source env \u2014 cleared for target env.`, }); form.agentId = ''; if (form.scopeKind === 'agent') form.scopeKind = 'app'; } // App slug: warn if not present in target env. if (form.appSlug && opts.targetEnvAppSlugs && !opts.targetEnvAppSlugs.includes(form.appSlug)) { warnings.push({ field: 'scope.appSlug', message: `App \`${form.appSlug}\` does not exist in the target env. Update before saving.`, }); } // Webhook connections: warn if connection is not allowed in target env. if (opts.targetEnvAllowedConnectionIds) { for (const w of form.webhooks) { if (!opts.targetEnvAllowedConnectionIds.includes(w.outboundConnectionId)) { warnings.push({ field: `webhooks[${w.outboundConnectionId}]`, message: `Outbound connection is not allowed in the target env \u2014 remove or pick another before saving.`, }); } } } return { form, warnings }; }