'use client' import React from 'react' import { v4 as uuidv4 } from 'uuid' import type { ConditionGroup, ConditionNode, FieldCatalogEntry, LogicalOperator, ComparisonOperator } from '@/types' import { ConditionNodeEditor } from './ConditionNodeEditor' interface Props { group: ConditionGroup fieldCatalog: FieldCatalogEntry[] onChange: (group: ConditionGroup) => void readOnly?: boolean depth: number } const GROUP_COLORS = [ 'border-blue-300 bg-blue-50', 'border-purple-300 bg-purple-50', 'border-teal-300 bg-teal-50', 'border-orange-300 bg-orange-50', ] export function ConditionGroupEditor({ group, fieldCatalog, onChange, readOnly, depth }: Props) { const colorClass = GROUP_COLORS[depth % GROUP_COLORS.length] const updateOperator = (operator: LogicalOperator) => onChange({ ...group, operator }) const addCondition = () => { const newNode: ConditionNode = { id: uuidv4(), isGroup: false, field: '', operator: 'EQUALS', value: '', valueType: 'LITERAL', } onChange({ ...group, rules: [...group.rules, newNode] }) } const addGroup = () => { const nestedGroup: ConditionGroup = { id: uuidv4(), operator: 'AND', rules: [], } const newNode: ConditionNode = { id: uuidv4(), isGroup: true, group: nestedGroup, } onChange({ ...group, rules: [...group.rules, newNode] }) } const updateNode = (index: number, updated: ConditionNode) => { const next = [...group.rules] next[index] = updated onChange({ ...group, rules: next }) } const deleteNode = (index: number) => { onChange({ ...group, rules: group.rules.filter((_, i) => i !== index) }) } return (
{/* Group Header */}
Match
{(['AND', 'OR', 'NOT'] as LogicalOperator[]).map((op) => ( ))}
{group.operator === 'AND' ? 'all of the following' : group.operator === 'OR' ? 'any of the following' : 'NOT the following'}
{/* Conditions */}
{group.rules.length === 0 && (
{readOnly ? 'No conditions' : 'Add conditions below'}
)} {group.rules.map((node, idx) => (
{idx > 0 && (
{group.operator}
)}
{node.isGroup && node.group ? ( updateNode(idx, { ...node, group: updated }) } /> ) : ( updateNode(idx, updated)} onDelete={() => deleteNode(idx)} /> )}
{!readOnly && ( )}
))}
{/* Add Buttons */} {!readOnly && (
{depth < 3 && ( )}
)}
) }