'use client';
import type { ReactElement } from 'react';
import {
SHIPMENT_EXPRESSION_MAX_DEPTH,
SHIPMENT_EXPRESSION_MAX_LEAVES,
type ShipmentExpression,
} from '@/game/shipment-promises';
import { shipmentExpressionInputError, shipmentExpressionInputSize } from '@/game/shipment-expression-input';
import { ShipmentClauseFields } from './shipment-claim-fields';
import { Button } from './ui/button';
const newLeaf = (): ShipmentExpression => ({ territory: 'carthag', minimum: 1 });
/** Hook-free controlled tree. Stateful leaf descendants are rendered by React. */
export function ShipmentExpressionFields({ value, id = 'truth-shipment', disabled, onChange }: {
value: ShipmentExpression;
id?: string;
disabled: boolean;
onChange: (expression: ShipmentExpression) => void;
}) {
const size = shipmentExpressionInputSize(value);
const commit = (next: ShipmentExpression) => {
if (!disabled && !shipmentExpressionInputError(next)) onChange(next);
};
const render = (
node: ShipmentExpression,
path: string,
replace: (next: ShipmentExpression) => ShipmentExpression,
remove?: () => ShipmentExpression,
): ReactElement => {
const group = 'op' in node;
const allowed = (next: ShipmentExpression) => !disabled && !shipmentExpressionInputError(replace(next));
const change = (next: ShipmentExpression) => commit(replace(next));
const wrap = (op: 'and' | 'or'): ShipmentExpression => ({ op, terms: [node, newLeaf()] });
return
;
};
return ;
}