// Copyright (c) 2026 PowerOn AG // All rights reserved. /** * Helpers for optional select/multiselect rows on workflow form field definitions. */ export type FormFieldOptionRow = { value: string; label: string }; /** Field types where the author defines explicit { value, label } choices. */ export function formFieldTypeHasConfigurableOptions(typeId: string | undefined): boolean { if (!typeId) return false; return typeId === 'select' || typeId === 'enum'; } export function normalizeFormFieldOptions(raw: unknown): FormFieldOptionRow[] { if (!Array.isArray(raw)) return []; return raw.map((o, i) => { if (o && typeof o === 'object' && !Array.isArray(o)) { const r = o as Record; const value = String(r.value ?? r.id ?? ''); const label = String(r.label ?? r.value ?? r.id ?? `Option ${i + 1}`); return { value, label }; } const s = String(o ?? ''); return { value: s, label: s }; }); } /** * Stable key for `payload.*` / data refs. From the visible label; empty label → `field_`. */ export function deriveFormFieldPayloadKey(label: string, index: number): string { const trimmed = label.trim(); if (!trimmed) return `field_${index + 1}`; const deaccent = trimmed.normalize('NFKD').replace(/[\u0300-\u036f]/g, ''); let s = deaccent .toLowerCase() .replace(/[^a-z0-9]+/g, '_') .replace(/^_+|_+$/g, ''); if (!s) return `field_${index + 1}`; return s; }