| 1 |
// Utility helpers for field operations used by the builder |
| 2 |
import type { Field, NameSubField, AddressSubField } from '@/types/field' |
| 3 |
|
| 4 |
export function duplicateFieldForStore( |
| 5 |
original: Field, |
| 6 |
newIndex: number, |
| 7 |
getLabel: (k: string) => string, |
| 8 |
): Field { |
| 9 |
const origClone = JSON.parse(JSON.stringify(original)) as Field |
| 10 |
const copyBase: Field = { |
| 11 |
...origClone, |
| 12 |
id: 0, |
| 13 |
fieldIndex: newIndex, |
| 14 |
label: `${origClone.label}${getLabel('copy_label')}`, |
| 15 |
limitMaxLength: origClone.limitMaxLength ?? false, |
| 16 |
maxLength: origClone.maxLength ?? 255, |
| 17 |
labelPosition: origClone.labelPosition ?? 'default', |
| 18 |
noDuplicates: origClone.noDuplicates ?? false, |
| 19 |
inputPrefix: origClone.inputPrefix || '', |
| 20 |
inputSuffix: origClone.inputSuffix || '', |
| 21 |
} |
| 22 |
|
| 23 |
// If it's name/address parent-child, ensure subfields are duplicated and ids reset |
| 24 |
if (origClone.type === 'name') { |
| 25 |
const sub = (origClone.nameFields ?? []) as NameSubField[] |
| 26 |
copyBase.nameFields = sub.map((nf) => ({ ...nf, id: '' })) |
| 27 |
copyBase.nameFieldTypes = (copyBase.nameFields || []).map((nf) => nf.type) |
| 28 |
} else if (origClone.type === 'address') { |
| 29 |
const sub = (origClone.addressFields ?? []) as AddressSubField[] |
| 30 |
copyBase.addressFields = sub.map((af) => ({ ...af, id: '' })) |
| 31 |
// recompute addressFieldTypes from addressFields |
| 32 |
copyBase.addressFieldTypes = (copyBase.addressFields || []).map((af) => af.type) |
| 33 |
} |
| 34 |
|
| 35 |
return copyBase |
| 36 |
} |
| 37 |
|