import { IF_CONDITION_OPS, IF_MAX_ROWS, ifDefinitionIssue } from "./types"; import type { IfCondition, IfDefinition, IfResult, RollupConditionOp } from "./types"; export const IF_OP_LABELS: Record = { eq: "is", neq: "is not", contains: "contains", not_contains: "does not contain", is_empty: "is empty", is_not_empty: "is not empty", gt: "is greater than", gte: "is at least", lt: "is less than", lte: "is at most", }; type SourceField = { key: string; label: string }; export const emptyIfDefinition = (): IfDefinition => ({ rows: [], default: { value: "" } }); function FieldOptions({ fields, current }: { fields: readonly SourceField[]; current: string }) { return <> {!fields.some((field) => field.key === current) && } {fields.map((field) => )} ; } export function IfResultEditor({ label, result, fields, onChange }: { label: string; result: IfResult; fields: readonly SourceField[]; onChange: (result: IfResult) => void; }) { const kind = "field" in result ? "field" : result.value === null ? "null" : typeof result.value === "number" ? "number" : typeof result.value === "boolean" ? "boolean" : "text"; return <> {kind === "field" ? : kind === "boolean" ? : kind === "number" ? { const parsed = Number(event.target.value); const value = event.target.value.trim() && Number.isFinite(parsed) ? parsed : 0; event.target.value = String(value); onChange({ value }); }} /> : kind === "text" ? onChange({ value: event.target.value })} /> : null} ; } /** Reuse the flat rollup row anatomy, without conjunctions or statistic thresholds. */ export function IfStatementEditor({ definition, fields, onChange }: { definition: IfDefinition; fields: readonly SourceField[]; onChange: (definition: IfDefinition) => void; }) { const patch = (index: number, next: IfCondition) => onChange({ ...definition, rows: definition.rows.map((row, i) => i === index ? next : row) }); const move = (index: number, direction: number) => { const to = index + direction; if (to < 0 || to >= definition.rows.length) return; const rows = [...definition.rows]; [rows[index], rows[to]] = [rows[to], rows[index]]; onChange({ ...definition, rows }); }; const issue = ifDefinitionIssue(definition, fields); return
Conditions
{definition.rows.map((row, index) =>
patch(index, { ...row, value: event.target.value })} />
Result patch(index, { ...row, then })} />
)} Default onChange({ ...definition, default: fallback })} /> {issue &&
{issue}
}
; }