"use client" import * as React from 'react' import type { ComponentType } from 'react' import { Lock, Paperclip, Plus, Trash2, X } from 'lucide-react' import { useT } from '@open-mercato/shared/lib/i18n/context' import { FormField } from '@open-mercato/ui/primitives/form-field' import { Button } from '@open-mercato/ui/primitives/button' import { Spinner } from '@open-mercato/ui/primitives/spinner' import { Input } from '@open-mercato/ui/primitives/input' import { Textarea } from '@open-mercato/ui/primitives/textarea' import { CheckboxField } from '@open-mercato/ui/primitives/checkbox-field' import { SwitchField } from '@open-mercato/ui/primitives/switch-field' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@open-mercato/ui/primitives/select' import { DatePicker } from '@open-mercato/ui/backend/inputs/DatePicker' import { DateTimePicker } from '@open-mercato/ui/backend/inputs/DateTimePicker' import { ScaleField } from '../../../runner/ScaleField' import { SignatureCanvas, computeClauseSha256 } from '../../../runner/SignatureField' import { defaultFieldTypeRegistry, FieldTypeRegistry, } from '../../../schema/field-type-registry' import { readFileRefs, type FileAttachmentRef } from '../../../schema/file-field' import { readSignatureModes, type SignatureMode, type SignatureValue } from '../../../schema/signature-field' import { readGroupSubFields, readGroupMinItems, readGroupMaxItems, type GroupSubFieldDescriptor, } from '../../../schema/group-field' import { resolveLocaleString, type RunnerFieldDescriptor, type RunnerFieldNode, type RunnerFieldRendererProps, type RunnerFileAttachmentRef, type RunnerLocaleMap, type RunnerOption, } from '../types' const labelFor = ( node: RunnerFieldNode | undefined, locale: string, defaultLocale: string, fallback: string, ): string => resolveLocaleString(node?.['x-om-label'], locale, defaultLocale, fallback) const helpFor = ( node: RunnerFieldNode | undefined, locale: string, defaultLocale: string, ): string | undefined => { if (!node?.['x-om-help']) return undefined return resolveLocaleString(node['x-om-help'], locale, defaultLocale, '') } const SensitiveBadge = React.memo(function SensitiveBadge({ label }: { label: string }) { return ( ) }) function FieldShell( props: RunnerFieldRendererProps & { children: React.ReactNode sensitiveLabel?: string }, ) { const { field, fieldNode, locale, defaultLocale, error, sensitiveLabel, children } = props const label = labelFor(fieldNode, locale, defaultLocale, field.key) const help = helpFor(fieldNode, locale, defaultLocale) const description: React.ReactNode = field.sensitive && sensitiveLabel ? ( {help ? {help} : null} ) : help return ( {children} ) } function asString(value: unknown): string { if (value === null || value === undefined) return '' if (typeof value === 'string') return value return String(value) } function asNumberOrEmpty(value: unknown): string { if (typeof value === 'number' && Number.isFinite(value)) return String(value) return '' } function asArrayOfStrings(value: unknown): string[] { if (!Array.isArray(value)) return [] return value.filter((entry): entry is string => typeof entry === 'string') } function optionsOf(node: RunnerFieldNode): RunnerOption[] { const options = node['x-om-options'] return Array.isArray(options) ? (options as RunnerOption[]) : [] } const pad2 = (input: number): string => String(input).padStart(2, '0') // Date pickers exchange `Date` objects, but submissions persist the native // `YYYY-MM-DD` / `YYYY-MM-DDTHH:mm` strings the JSON-Schema validators expect. // These adapters keep the stored bytes identical so schemaHash + validation // parity survive the widget swap (forms AGENTS MUST 6 / MUST 15). function parseDateOnly(value: unknown): Date | null { if (typeof value !== 'string') return null const match = /^(\d{4})-(\d{2})-(\d{2})/.exec(value) if (!match) return null const date = new Date(Number(match[1]), Number(match[2]) - 1, Number(match[3])) return Number.isNaN(date.getTime()) ? null : date } function formatDateOnly(date: Date | null): string | undefined { if (!date) return undefined return `${date.getFullYear()}-${pad2(date.getMonth() + 1)}-${pad2(date.getDate())}` } function parseDateTimeLocal(value: unknown): Date | null { if (typeof value !== 'string') return null const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/.exec(value) if (!match) return null const date = new Date( Number(match[1]), Number(match[2]) - 1, Number(match[3]), Number(match[4]), Number(match[5]), ) return Number.isNaN(date.getTime()) ? null : date } function formatDateTimeLocal(date: Date | null): string | undefined { if (!date) return undefined return ( `${date.getFullYear()}-${pad2(date.getMonth() + 1)}-${pad2(date.getDate())}` + `T${pad2(date.getHours())}:${pad2(date.getMinutes())}` ) } const SENSITIVE_KEY = 'forms.runner.encrypted_label' // ---------- Renderers ---------- export const TextRenderer: ComponentType = (props) => { const { value, onChange, onBlur, disabled, fieldNode } = props const minLength = typeof fieldNode.minLength === 'number' ? fieldNode.minLength : undefined const maxLength = typeof fieldNode.maxLength === 'number' ? fieldNode.maxLength : undefined const pattern = typeof fieldNode.pattern === 'string' ? fieldNode.pattern : undefined return ( onChange(event.target.value)} onBlur={onBlur} disabled={disabled} minLength={minLength} maxLength={maxLength} pattern={pattern} /> ) } export const TextareaRenderer: ComponentType = (props) => { const { value, onChange, onBlur, disabled } = props return (