{"version":3,"file":"validation.cjs","names":["declarationOf","FileAnnotationDraftSchema","KIND_BY_SUBTYPE","EngineError","EngineErrorCode","fileAnnotationPatchSchemaOf","pdfPrepareAnnotationDraft","pdfDeriveAnnotationDraft","pdfPrepareAnnotationPatch","pdfDeriveAnnotationPatch","bundlePageMapping","fieldMarkersOf","draftOf","annotationKey","encodePageKey","pageRefsIn","declarationOf","AnnotationDraftSchema","EngineError","EngineErrorCode","mapPageRefs","fieldMarkersOf","planAnnotationCopies","draftOf","bundlePageMapping","pageRefsIn","annotationKey","fieldActionsOf","widgetPlacementOf","holdsValue","fieldValueOf","widgetFlagsOf","encodeFieldRefKey","fieldMatches","FormFieldDraftSchema","EngineError","EngineErrorCode","fieldDraftOf","planFormCopies"],"sources":["../src/annotation/checkWrite.ts","../src/transfer/planAnnotationImport.ts","../src/transfer/planAnnotationCopy.ts","../src/transfer/planFormImport.ts","../src/transfer/planFormCopy.ts"],"sourcesContent":["/**\n * The checks every annotation write runs before its first write, on both\n * engines: the prepared values against their kind's schema, not only the\n * names. A value the schema refuses is `InvalidArg` naming the field. A check\n * only accepts or refuses: it never adds, drops or changes a field, so the\n * engine writes what a viewer predicts (prepare, check, derive; the viewer\n * runs the same two steps without schema validation, `resolve/`). Built with\n * zod, so it runs where the engine does (its worker, the server), never on\n * the page: `@embedpdf/engine-core/validation` exports it.\n */\nimport type { z } from 'zod';\n\nimport {\n  declarationOf,\n  FileAnnotationDraftSchema,\n  fileAnnotationPatchSchemaOf,\n  KIND_BY_SUBTYPE,\n  type AnnotationDraft,\n  type Annotation,\n  type AnnotationPatch,\n} from './kinds';\nimport {\n  pdfDeriveAnnotationDraft,\n  pdfPrepareAnnotationDraft,\n  type DraftResolveOptions,\n} from './resolve/resolveAnnotationDraft';\nimport {\n  pdfDeriveAnnotationPatch,\n  pdfPrepareAnnotationPatch,\n  type ResolveOptions,\n} from './resolve/resolveAnnotationPatch';\nimport { EngineError } from '../errors/EngineError';\nimport { EngineErrorCode } from '../errors/EngineErrorCode';\nimport type { PdfCoordinates } from '../pageSpace/coordinates';\n\n/**\n * A prepared create's data checked against its kind's schema. The caller\n * writes its own data, not the schema's output, so nothing is reshaped on its\n * way in.\n */\nexport function assertAnnotationDraft(\n  draft: AnnotationDraft<PdfCoordinates>,\n  options: {\n    /**\n     * Link fields the caller has taken out of the draft and links itself (a\n     * change set's `reply` and popup `parent`); they aren't checked here.\n     */\n    linked?: readonly string[];\n  } = {},\n): void {\n  const declaration = declarationOf(String(draft.subtype));\n  const linked = options.linked ?? [];\n  const schema =\n    declaration && linked.length > 0\n      ? (declaration.fileSchemas.create as unknown as z.AnyZodObject).omit(\n          Object.fromEntries(linked.map((name) => [name, true])),\n        )\n      : FileAnnotationDraftSchema;\n  const checked = schema.safeParse(draft);\n  if (!checked.success) throw invalidWrite(checked.error, `${String(draft.subtype)} create`);\n}\n\n/**\n * A prepared update's data (`pdfPrepareAnnotationPatch`) checked against the\n * kind it leaves the annotation: its own, or the sibling the patch names. A\n * `readBack()` value still in it is a change, as preparation drops one sent\n * back as it reads, so one a write can't make is refused; the rest is checked\n * against the kind's update schema.\n */\nexport function assertAnnotationPatch(\n  current: Annotation<PdfCoordinates>,\n  prepared: AnnotationPatch<PdfCoordinates>,\n): void {\n  const subtype = prepared.subtype ?? current.subtype;\n  if (subtype === 'unsupported') return;\n  const values = prepared as Record<string, unknown>;\n  for (const [name, write] of Object.entries(KIND_BY_SUBTYPE[subtype].readBackWrites)) {\n    const value = values[name];\n    if (value === undefined || write?.safeParse(value).success) continue;\n    throw new EngineError(\n      EngineErrorCode.InvalidArg,\n      write\n        ? `${subtype} update field '${name}': this value can be read but not written; send it back unchanged or replace it`\n        : `${subtype} update field '${name}': the engine works this out; send it back unchanged or leave it out`,\n      { details: { field: name } },\n    );\n  }\n  const parsed = fileAnnotationPatchSchemaOf(subtype).safeParse(prepared);\n  if (!parsed.success) throw invalidWrite(parsed.error, `${subtype} update`);\n}\n\n/**\n * The draft the engine writes for `draft`, in the file's coordinates:\n * prepared (`pdfPrepareAnnotationDraft`), checked against its kind, and\n * every field that follows from it stated (`pdfDeriveAnnotationDraft`). A\n * viewer predicts the same without schema validation\n * (`pdfPredictAnnotationDraft`).\n *\n * Throws `InvalidArg` for a draft the engine refuses. Pure: it reads nothing\n * but its arguments.\n */\nexport function pdfResolveAnnotationDraft(\n  draft: AnnotationDraft<PdfCoordinates>,\n  options: DraftResolveOptions = {},\n): AnnotationDraft<PdfCoordinates> {\n  const prepared = pdfPrepareAnnotationDraft(draft);\n  assertAnnotationDraft(prepared, { linked: options.linked ?? [] });\n  return pdfDeriveAnnotationDraft(prepared, options);\n}\n\n/**\n * The patch the engine writes for `patch` on `current`, in the file's\n * coordinates: prepared (`pdfPrepareAnnotationPatch`), checked against its\n * kind (`assertAnnotationPatch`), and every field that follows from it\n * stated (`pdfDeriveAnnotationPatch`). A viewer predicts the same without\n * schema validation (`pdfPredictAnnotationPatch`).\n *\n * Throws `InvalidArg` for a patch the engine refuses. Pure: it reads nothing\n * but its arguments.\n */\nexport function pdfResolveAnnotationPatch(\n  current: Annotation<PdfCoordinates>,\n  patch: AnnotationPatch<PdfCoordinates>,\n  options: ResolveOptions = {},\n): AnnotationPatch<PdfCoordinates> {\n  const prepared = pdfPrepareAnnotationPatch(current, patch);\n  assertAnnotationPatch(current, prepared);\n  return pdfDeriveAnnotationPatch(current, prepared, options);\n}\n\nfunction invalidWrite(error: z.ZodError, where: string): EngineError {\n  const issue = error.issues[0]!;\n  const field = issue.path.join('.');\n  return new EngineError(\n    EngineErrorCode.InvalidArg,\n    `${where}${field ? ` field '${field}'` : ''}: ${issue.message}`,\n    { details: field ? { field } : {} },\n  );\n}\n","/**\n * What an annotation import creates and leaves out, decided before its first write: each item\n * checked against its kind's create schema. Built with zod, so it runs where the engine does,\n * from `@embedpdf/engine-core/validation`; the facts an import commits are in\n * `annotationImport.ts`.\n */\nimport type { AnnotationBundle } from './AnnotationBundle';\nimport {\n  fieldMarkersOf,\n  type AnnotationDropReason,\n  type AnnotationImportDrop,\n} from './annotationImport';\nimport { bundlePageMapping, type BundleImportPages, type BundleImportTarget } from './bundle';\nimport { mapPageRefs, pageRefsIn } from './pageRefs';\nimport {\n  AnnotationDraftSchema,\n  declarationOf,\n  type AnnotationDraft,\n  type Annotation,\n} from '../annotation/kinds';\nimport type { KindFields } from '../annotation/declaration';\nimport type { AnnotationReplyType } from '../annotation/primitives';\nimport { EngineError } from '../errors/EngineError';\nimport { EngineErrorCode } from '../errors/EngineErrorCode';\nimport { annotationKey } from '../identity/annotationKey';\nimport type { AnnotationRef } from '../identity/AnnotationRef';\nimport { encodePageKey, type PageRef } from '../identity/PageRef';\n\n/** One annotation an import creates. */\nexport interface PlannedAnnotation {\n  /** Its item in the bundle. */\n  readonly item: number;\n  /** The page it goes on. */\n  readonly page: PageRef;\n  /** What `create` writes: the item's data, its pages mapped, without its links and dropped fields. */\n  readonly draft: AnnotationDraft;\n  /** The annotation it replies to, by its place in `creates`. */\n  readonly replyTo?: { readonly planned: number; readonly type: AnnotationReplyType };\n  /** For a popup: the annotation it shows, by its place in `creates`. */\n  readonly parent?: number;\n}\n\nexport interface AnnotationImportPlan {\n  /** In bundle order. */\n  readonly creates: readonly PlannedAnnotation[];\n  readonly dropped: readonly AnnotationImportDrop[];\n}\n\n/**\n * What an import of `bundle` into the `target` pages creates, and what it\n * leaves out. Nothing here touches a document, so an import refuses before\n * its first write:\n *\n * - `InvalidArg` when an item's data isn't valid for its kind, or when a\n *   page an item is on or points at maps to no target page (naming them);\n * - `NotFound` when a page list maps to a page the target doesn't have.\n *\n * Unsupported items and unresolved relationships are left out in this order:\n *\n * 1. an item of an unsupported kind, and a form field's widget; a field\n *    marker leaves out only that field;\n * 2. an item whose `reply.to` or `parent` is no item of the bundle on the\n *    same target page (`parent-missing`);\n * 3. an item whose name is used on its target page, or by an earlier item\n *    going to the same page (`name-conflict`);\n * 4. then, until nothing changes, an item whose `reply.to` or `parent` was\n *    left out (`parent-dropped`), so a whole branch goes together.\n *\n * `nameTaken(page, nm)` answers for the target page as it is before the import.\n */\nexport function planAnnotationImport(input: {\n  readonly bundle: Pick<AnnotationBundle, 'pages' | 'items'>;\n  readonly pages?: BundleImportPages;\n  readonly target: readonly BundleImportTarget[];\n  readonly nameTaken: (page: PageRef, nm: string) => boolean;\n}): AnnotationImportPlan {\n  const { bundle, target, nameTaken } = input;\n  const items = bundle.items.map((item) => item.data);\n  const mapPage = bundlePageMapping({\n    bundlePages: bundle.pages,\n    pages: input.pages ?? 'same',\n    target,\n    named: items.flatMap(pagesNamedBy),\n  });\n\n  const drops = new Map<number, AnnotationDropReason>();\n  const fieldDrops = new Map<number, Array<{ field: string; reason: AnnotationDropReason }>>();\n  const drafts = new Map<number, AnnotationDraft>();\n  items.forEach((data, index) => {\n    if (data.subtype === 'unsupported') {\n      drops.set(index, 'unsupported-kind');\n      return;\n    }\n    if (data.subtype === 'widget') {\n      drops.set(index, 'widget');\n      return;\n    }\n    // A popup shows another annotation; one that names none can't be made.\n    if (data.subtype === 'popup' && data.parent === null) {\n      drops.set(index, 'parent-missing');\n      return;\n    }\n    const unwritable = unwritableValuesOf(data);\n    if (unwritable === 'item') {\n      drops.set(index, 'unsupported-value');\n      return;\n    }\n    const markers = [...fieldMarkersOf(data), ...unwritable];\n    if (markers.length > 0) fieldDrops.set(index, markers);\n    drafts.set(\n      index,\n      draftOf(data, markers, mapPage, { label: `import: item ${index}`, details: { item: index } }),\n    );\n  });\n\n  const byKey = new Map<string, number>();\n  items.forEach((data, index) => {\n    const key = annotationKey(data.ref);\n    if (!byKey.has(key)) byKey.set(key, index);\n  });\n  // A reply and a popup are on their parent's page (ISO 32000-2 §12.5.6.2),\n  // so a link finds its target only there.\n  const pageOf = (index: number) => encodePageKey(mapPage(items[index]!.ref.page));\n  const resolve = (ref: AnnotationRef, from: number) => {\n    const found = byKey.get(annotationKey(ref));\n    return found !== undefined && pageOf(found) === pageOf(from) ? found : undefined;\n  };\n  const linksOf = (data: Annotation) =>\n    [data.reply?.to, data.subtype === 'popup' ? data.parent : null].filter(\n      (ref): ref is AnnotationRef => ref != null,\n    );\n\n  items.forEach((data, index) => {\n    if (drops.has(index)) return;\n    if (linksOf(data).some((ref) => resolve(ref, index) === undefined)) {\n      drops.set(index, 'parent-missing');\n    }\n  });\n\n  const popupOf = new Map<number, number>();\n  items.forEach((data, index) => {\n    if (drops.has(index) || data.subtype !== 'popup' || !data.parent) return;\n    const parent = resolve(data.parent, index)!;\n    if (popupOf.has(parent)) drops.set(index, 'popup-taken');\n    else popupOf.set(parent, index);\n  });\n\n  const claimed = new Set<string>();\n  items.forEach((data, index) => {\n    if (drops.has(index) || !data.nm) return;\n    const page = mapPage(data.ref.page);\n    const claim = `${encodePageKey(page)}\\u0000${data.nm}`;\n    if (claimed.has(claim) || nameTaken(page, data.nm)) drops.set(index, 'name-conflict');\n    else claimed.add(claim);\n  });\n\n  for (let changed = true; changed; ) {\n    changed = false;\n    items.forEach((data, index) => {\n      if (drops.has(index)) return;\n      if (linksOf(data).some((ref) => drops.has(resolve(ref, index)!))) {\n        drops.set(index, 'parent-dropped');\n        changed = true;\n      }\n    });\n  }\n\n  const planned = new Map<number, number>();\n  items.forEach((_data, index) => {\n    if (!drops.has(index)) planned.set(index, planned.size);\n  });\n  const creates: PlannedAnnotation[] = [...planned.keys()].map((index) => {\n    const data = items[index]!;\n    const replyTo = data.reply ? planned.get(resolve(data.reply.to, index)!) : undefined;\n    const parent =\n      data.subtype === 'popup' && data.parent\n        ? planned.get(resolve(data.parent, index)!)\n        : undefined;\n    return {\n      item: index,\n      page: mapPage(data.ref.page),\n      draft: drafts.get(index)!,\n      ...(replyTo !== undefined ? { replyTo: { planned: replyTo, type: data.reply!.type } } : {}),\n      ...(parent !== undefined ? { parent } : {}),\n    };\n  });\n\n  const dropped: AnnotationImportDrop[] = [];\n  items.forEach((data, index) => {\n    const reason = drops.get(index);\n    if (reason) dropped.push({ ref: data.ref, reason });\n    else for (const drop of fieldDrops.get(index) ?? []) dropped.push({ ref: data.ref, ...drop });\n  });\n  return { creates, dropped };\n}\n\n/**\n * The pages an item is on or points at, apart from its links to other\n * annotations (`reply.to`, `parent`, `popup`), which resolve in the bundle.\n */\nfunction pagesNamedBy(data: Annotation): PageRef[] {\n  const { ref, reply: _reply, popup: _popup, ...rest } = data;\n  const fields = { ...rest } as Record<string, unknown>;\n  if (data.subtype === 'popup') delete fields.parent;\n  return [ref.page, ...pageRefsIn(fields)];\n}\n\n/**\n * The values `data` reads that a write can't make (a `readBack()` field's\n * read-only values), each left out; `'item'` when one is a field every\n * create needs. A link's target is reported by {@link fieldMarkersOf}.\n */\nexport function unwritableValuesOf(\n  data: Annotation,\n): Array<{ field: string; reason: AnnotationDropReason }> | 'item' {\n  const unwritable: Array<{ field: string; reason: AnnotationDropReason }> = [];\n  const declaration = declarationOf(data.subtype);\n  for (const [name, spec] of Object.entries(declaration?.fields ?? {})) {\n    const { owner, readBack, required } = spec.traits;\n    if (owner !== 'data' || !readBack || name === 'target') continue;\n    const value = (data as unknown as Record<string, unknown>)[name];\n    if (value == null || spec.write.safeParse(value).success) continue;\n    if (required) return 'item';\n    unwritable.push({ field: name, reason: 'unsupported-value' });\n  }\n  return unwritable;\n}\n\n/**\n * The create `data` becomes, checked against its kind's create schema.\n * `where` names it in the refusal: its label, and what the error's details\n * say of it.\n */\nexport function draftOf(\n  data: Annotation,\n  markers: ReadonlyArray<{ field: string }>,\n  mapPage: (page: PageRef) => PageRef,\n  where: { readonly label: string; readonly details: Readonly<Record<string, unknown>> },\n): AnnotationDraft {\n  const fields = { ...data } as Record<string, unknown>;\n  delete fields.reply;\n  // Left out, the field is not written: `null` where a write takes it,\n  // else the kind's default.\n  const declared: KindFields = declarationOf(data.subtype)?.fields ?? {};\n  for (const { field } of markers) {\n    if (declared[field]?.traits.writeNullable) fields[field] = null;\n    else delete fields[field];\n  }\n  // Checked with the popup's `parent`, which the import then links itself.\n  const checked = AnnotationDraftSchema.safeParse(fields);\n  if (data.subtype === 'popup') delete fields.parent;\n  if (!checked.success) {\n    const issue = checked.error.issues[0]!;\n    const path = issue.path.join('.');\n    throw new EngineError(\n      EngineErrorCode.InvalidArg,\n      `${where.label}${path ? ` field '${path}'` : ''}: ${issue.message}`,\n      { details: { ...where.details, ...(path ? { field: path } : {}) } },\n    );\n  }\n  // The item's own data, not the schema's output, so nothing a read carries\n  // is reshaped on its way back in.\n  return mapPageRefs(fields, mapPage) as AnnotationDraft;\n}\n","/**\n * What a copy of annotations creates and leaves out, decided before its\n * first write: the copies as {@link planAnnotationCopies} makes them, each\n * checked against its kind's create schema as an import's items are. Built\n * with zod, so it runs where the engine does, from\n * `@embedpdf/engine-core/validation`.\n */\nimport {\n  planAnnotationCopies,\n  type AnnotationCopyPlacement,\n  type PlannedAnnotationCopy,\n} from './annotationCopy';\nimport {\n  fieldMarkersOf,\n  type AnnotationDropReason,\n  type AnnotationImportDrop,\n} from './annotationImport';\nimport { draftOf, unwritableValuesOf } from './planAnnotationImport';\nimport type { Annotation, AnnotationDraft } from '../annotation/kinds';\n\n/** One annotation a copy creates. */\nexport interface PlannedCopyCreate extends Omit<PlannedAnnotationCopy, 'data'> {\n  /** What `create` writes: the source's data, moved, without its links and dropped fields. */\n  readonly draft: AnnotationDraft;\n}\n\nexport interface AnnotationCopyCreates {\n  /** In the order of the annotations given. */\n  readonly creates: readonly PlannedCopyCreate[];\n  /** What was left out, whole or one field of it, in the order given. */\n  readonly dropped: readonly AnnotationImportDrop[];\n}\n\n/**\n * What a copy of `sources` (in page space) creates, and what it leaves out.\n * Nothing here touches a document, so a copy refuses before its first write:\n * `InvalidArg` when a source's data isn't valid for its kind.\n *\n * {@link planAnnotationCopies} leaves a field that marks something the engine\n * can't write out of its copy, and each one is reported here. Beyond that, a\n * source with a value every create of its kind needs and no write can make\n * is left out, and any other field with such a value is left out of its copy.\n */\nexport function planAnnotationCopy(\n  sources: readonly Annotation[],\n  placement: AnnotationCopyPlacement,\n): AnnotationCopyCreates {\n  type Marker = { field: string; reason: AnnotationDropReason };\n  const markers = new Map<number, Marker[] | 'item'>();\n  sources.forEach((data, index) => {\n    if (data.subtype === 'unsupported' || data.subtype === 'widget') return;\n    const unwritable = unwritableValuesOf(data);\n    markers.set(index, unwritable === 'item' ? 'item' : [...fieldMarkersOf(data), ...unwritable]);\n  });\n  const fieldsOf = (index: number): Marker[] => {\n    const found = markers.get(index);\n    return found === undefined || found === 'item' ? [] : found;\n  };\n\n  const plan = planAnnotationCopies(sources, placement, (index) => markers.get(index) === 'item');\n  const creates = plan.copies.map(\n    ({ data, ...copy }): PlannedCopyCreate => ({\n      ...copy,\n      draft: draftOf(data, fieldsOf(copy.source), (page) => page, {\n        label: `copy: annotation ${copy.source}`,\n        details: { annotation: copy.source },\n      }),\n    }),\n  );\n\n  // `plan.dropped` lists each source left out whole, in order: every other one is a create.\n  const kept = new Set(creates.map((create) => create.source));\n  const whole = plan.dropped[Symbol.iterator]();\n  const dropped: AnnotationImportDrop[] = [];\n  sources.forEach((data, index) => {\n    if (!kept.has(index)) dropped.push(whole.next().value!);\n    else for (const marker of fieldsOf(index)) dropped.push({ ref: data.ref, ...marker });\n  });\n  return { creates, dropped };\n}\n","/**\n * What a form import writes and leaves out, decided before its first write: a design import's\n * fields checked against the create schema, a values import's values matched to the form's\n * fields. Built with zod, so it runs where the engine does, from\n * `@embedpdf/engine-core/validation`; the facts an import commits are in `formImport.ts`.\n */\nimport { bundlePageMapping, type BundleImportPages, type BundleImportTarget } from './bundle';\nimport type { FormBundle } from './FormBundle';\nimport { fieldMatches } from './formExport';\nimport {\n  fieldActionsOf,\n  fieldDraftOf,\n  holdsValue,\n  widgetFlagsOf,\n  widgetPlacementOf,\n} from './formFieldCreates';\nimport type {\n  FormImportDrop,\n  FormValuesImportDrop,\n  FormValuesImportDropReason,\n  WidgetFlags,\n} from './formImport';\nimport { pageRefsIn } from './pageRefs';\nimport type { FieldActionsPatch } from '../dto/PdfAction';\nimport { EngineError } from '../errors/EngineError';\nimport { EngineErrorCode } from '../errors/EngineErrorCode';\nimport type { FormFieldDraft, WidgetPlacement } from '../forms/draft';\nimport type { FormField, FormFieldKind } from '../forms/field';\nimport { FormFieldDraftSchema } from '../forms/schema';\nimport { fieldValueOf, type FormFieldValue } from '../forms/value';\nimport { annotationKey } from '../identity/annotationKey';\nimport { encodeFieldRefKey, type FormFieldRef } from '../identity/FormFieldRef';\n\n/** One field a design import creates. */\nexport interface PlannedFormField {\n  /** Its row in the bundle. */\n  readonly field: number;\n  /** What `create` writes: the field's settings, scripts and widgets, their pages mapped. */\n  readonly draft: FormFieldDraft;\n  /** Each widget's row in the bundle, in `draft.widgets` order. */\n  readonly widgets: readonly number[];\n  /** Each widget's flags, in `draft.widgets` order. */\n  readonly flags: readonly WidgetFlags[];\n  /** The value it gets; absent when the import writes none. */\n  readonly value?: FormFieldValue;\n}\n\nexport interface FormImportPlan {\n  /** In bundle order. */\n  readonly creates: readonly PlannedFormField[];\n  /** The calculation order among the creates, by their place in `creates`, as the bundle has it. */\n  readonly calculationOrder: readonly number[];\n  readonly dropped: readonly FormImportDrop[];\n}\n\n/**\n * What a design import of `bundle` into the `target` pages creates, and what\n * it leaves out. Nothing here touches a document, so an import refuses\n * before its first write:\n *\n * - `InvalidArg` when a field's data isn't valid for its kind, or when a\n *   page a widget is on or points at maps to no target page;\n * - `NotFound` when a page list maps to a page the target doesn't have.\n *\n * Left out, in bundle order: a field of an unknown kind, a field whose\n * name is taken (`nameTaken`, for the document as it is before the\n * import), a widget whose field isn't in the bundle, and each action that\n * is a script, a submit or a link when `mayScript` is false\n * (`doc.forms.script`), or that a write can't make. Each field keeps every\n * widget it has. With `values`, each field gets the value the bundle holds.\n */\nexport function planFormImport(input: {\n  readonly bundle: Pick<FormBundle, 'pages' | 'fields' | 'widgets' | 'calculationOrder'>;\n  readonly pages?: BundleImportPages;\n  readonly target: readonly BundleImportTarget[];\n  readonly nameTaken: (name: string) => boolean;\n  readonly mayScript: boolean;\n  readonly values: boolean;\n}): FormImportPlan {\n  const { bundle, mayScript } = input;\n  const mapPage = bundlePageMapping({\n    bundlePages: bundle.pages,\n    pages: input.pages ?? 'same',\n    target: input.target,\n    named: bundle.widgets.flatMap((row) => pageRefsIn(row.data)),\n  });\n\n  const rowByKey = new Map(\n    bundle.widgets.map((row, index) => [annotationKey(row.data.ref), index]),\n  );\n  const owned = new Set<number>();\n  const dropped: FormImportDrop[] = [];\n  const claimed = new Set<string>();\n  const creates: PlannedFormField[] = [];\n\n  bundle.fields.forEach(({ data: field }, index) => {\n    const rows = field.widgets.flatMap((widget) => {\n      const row = widget.ref ? rowByKey.get(annotationKey(widget.ref)) : undefined;\n      return row === undefined ? [] : [row];\n    });\n    for (const row of rows) owned.add(row);\n    if (field.kind === 'unknown') {\n      dropped.push({ ref: field.ref, reason: 'unsupported-kind' });\n      return;\n    }\n    if (claimed.has(field.name) || input.nameTaken(field.name)) {\n      dropped.push({ ref: field.ref, reason: 'name-conflict' });\n      return;\n    }\n    claimed.add(field.name);\n    const actions = fieldActionsOf(field, mayScript, dropped);\n    const placements = rows.map((row) =>\n      widgetPlacementOf(field, bundle.widgets[row]!.data, mapPage, mayScript, dropped),\n    );\n    const draft = draftOf(field, actions, placements, index);\n    const value = input.values && holdsValue(field) ? fieldValueOf(field) : null;\n    creates.push({\n      field: index,\n      draft,\n      widgets: rows,\n      flags: rows.map((row) => widgetFlagsOf(bundle.widgets[row]!.data)),\n      ...(value ? { value } : {}),\n    });\n  });\n\n  bundle.widgets.forEach((row, index) => {\n    if (!owned.has(index)) {\n      dropped.push({ ref: row.data.field, widget: row.data.ref, reason: 'no-field' });\n    }\n  });\n\n  // The order the bundle gives the fields that kept their calculate script.\n  const calculationOrder = bundle.calculationOrder.flatMap((ref) => {\n    const at = creates.findIndex(\n      (create) =>\n        encodeFieldRefKey(bundle.fields[create.field]!.data.ref) === encodeFieldRefKey(ref) &&\n        create.draft.actions?.calculate,\n    );\n    return at < 0 ? [] : [at];\n  });\n  return { creates, calculationOrder, dropped };\n}\n\n/** A field of the document a values import fills. */\nexport interface FormValuesImportTarget {\n  readonly ref: FormFieldRef;\n  readonly name: string;\n  readonly kind: FormFieldKind;\n}\n\n/** One value a values import writes. */\nexport interface PlannedValueWrite<T extends FormValuesImportTarget = FormValuesImportTarget> {\n  /** Its field's row in the bundle. */\n  readonly field: number;\n  /** The document's field of that name. */\n  readonly target: T;\n  readonly value: FormFieldValue;\n}\n\n/**\n * What a values import of `bundle` writes into a form whose fields are\n * `target`: each field the bundle holds a value for gets it, matched by full\n * name. Left out, in bundle order: a field the document doesn't have\n * (`no-field`), one of another kind (`wrong-kind`), and one `refusal`\n * names a reason for: a value the field can't take, a field a signature\n * locked, a field the session may not fill in. So everything is decided\n * before the first write.\n */\nexport function planFormValuesImport<T extends FormValuesImportTarget>(input: {\n  readonly bundle: Pick<FormBundle, 'fields'>;\n  readonly target: readonly T[];\n  readonly refusal?: (\n    target: T,\n    value: FormFieldValue,\n  ) => Extract<\n    FormValuesImportDropReason,\n    'value-not-allowed' | 'locked' | 'fill-not-allowed'\n  > | null;\n}): {\n  readonly writes: readonly PlannedValueWrite<T>[];\n  readonly dropped: readonly FormValuesImportDrop[];\n} {\n  const writes: PlannedValueWrite<T>[] = [];\n  const dropped: FormValuesImportDrop[] = [];\n  input.bundle.fields.forEach(({ data: field }, index) => {\n    const value = holdsValue(field) ? fieldValueOf(field) : null;\n    if (!value) return;\n    const target = input.target.find((candidate) =>\n      fieldMatches(candidate, { kind: 'fqn', name: field.name }),\n    );\n    if (!target) {\n      dropped.push({ ref: field.ref, reason: 'no-field' });\n    } else if (target.kind !== field.kind) {\n      dropped.push({ ref: field.ref, reason: 'wrong-kind' });\n    } else {\n      const refused = input.refusal?.(target, value);\n      if (refused) dropped.push({ ref: field.ref, reason: refused });\n      else writes.push({ field: index, target, value });\n    }\n  });\n  return { writes, dropped };\n}\n\n/**\n * `draft`, checked against the create schema. `label` names it in the\n * refusal (`import: field 2`), and `details` is what the error says of it.\n */\nexport function checkedFieldDraft(\n  draft: FormFieldDraft,\n  label: string,\n  details: Readonly<Record<string, unknown>>,\n): FormFieldDraft {\n  const checked = FormFieldDraftSchema.safeParse(draft);\n  if (!checked.success) {\n    const issue = checked.error.issues[0]!;\n    const path = issue.path.join('.');\n    throw new EngineError(\n      EngineErrorCode.InvalidArg,\n      `${label}${path ? ` member '${path}'` : ''}: ${issue.message}`,\n      { details: { ...details, ...(path ? { field: path } : {}) } },\n    );\n  }\n  return draft;\n}\n\n/** The create a field row becomes, checked against the create schema. */\nfunction draftOf(\n  field: FormField,\n  actions: FieldActionsPatch,\n  widgets: WidgetPlacement[],\n  index: number,\n): FormFieldDraft {\n  const label = `import: field ${index}`;\n  return checkedFieldDraft(fieldDraftOf(field, field.name, actions, widgets, label), label, {\n    item: index,\n  });\n}\n","/**\n * What a copy of form fields creates and leaves out, decided before its\n * first write: the copies as {@link planFormCopies} makes them, each checked\n * against the create schema as an import's fields are. Built with zod, so it\n * runs where the engine does, from `@embedpdf/engine-core/validation`.\n */\nimport { planFormCopies, type FormCopyPlan } from './formCopy';\nimport { checkedFieldDraft } from './planFormImport';\n\n/**\n * What a copy of fields of `form` (in page space) creates, and what it\n * leaves out. Nothing here touches a document, so a copy refuses before its\n * first write: as {@link planFormCopies} refuses, and with `InvalidArg` when\n * a source's data isn't valid for its kind.\n */\nexport function planFormCopy(input: Parameters<typeof planFormCopies>[0]): FormCopyPlan {\n  const plan = planFormCopies(input);\n  for (const create of plan.creates) {\n    checkedFieldDraft(create.draft, `copy: field ${create.source}`, { item: create.source });\n  }\n  return plan;\n}\n"],"mappings":";;;;;;;;;;;;AAwCA,SAAgB,sBACd,OACA,UAMI,CAAC,GACC;CACN,MAAM,cAAcA,eAAAA,cAAc,OAAO,MAAM,OAAO,CAAC;CACvD,MAAM,SAAS,QAAQ,UAAU,CAAC;CAOlC,MAAM,WALJ,eAAe,OAAO,SAAS,IAC1B,YAAY,YAAY,OAAqC,KAC5D,OAAO,YAAY,OAAO,KAAK,SAAS,CAAC,MAAM,IAAI,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