{"version":3,"file":"ngx-t-forms-payload-template-editor.component-CVzOTLYE.mjs","sources":["../../../projects/ngx-t-forms/src/lib/components/t-dynamic-data-edit/elements/payload-template-editor/payload-template-editor.component.ts","../../../projects/ngx-t-forms/src/lib/components/t-dynamic-data-edit/elements/payload-template-editor/payload-template-editor.component.html"],"sourcesContent":["import {\r\n  ChangeDetectionStrategy,\r\n  Component,\r\n  ViewEncapsulation,\r\n  computed,\r\n  input,\r\n  output,\r\n} from '@angular/core';\r\nimport { MatIconModule } from '@angular/material/icon';\r\n\r\nimport type { FormColumnInputs, PayloadTemplate } from 'ngx-t-forms-types';\r\n\r\nimport type { IConfigElementError } from '../../t-dynamic-data-edit.component';\r\nimport { bindableFields, fieldEditorBindings } from '../_shared/field-bindings';\r\nimport {\r\n  JsonEditorComponent,\r\n  type EditorBinding,\r\n  type JsonEditorChange,\r\n} from '../json-editor/json-editor.component';\r\nimport { buildTokenMaps, mapTemplateTokens } from './token-transform';\r\n\r\n/**\r\n * Editor for a POST fetch's `backEndConfig.payloadTemplate`.\r\n *\r\n * Reuses the shared {@link JsonEditorComponent} (syntax highlight, validation,\r\n * Format, `$`-trigger binding menu). Each form input is offered as an insertable\r\n * `{{inputId}}` binding — typing `$` opens the menu. The parsed JSON is emitted\r\n * as the template; `{{inputId}}` tokens map to live field values at runtime, and\r\n * every other literal is a fixed default.\r\n *\r\n * Which mechanism is active (template vs mapped `minimumInputRequired`) is chosen\r\n * upstream by the mode selector in the element-editor config, so this editor only\r\n * renders when template mode is selected — it owns no toggle.\r\n *\r\n * @example\r\n *   <lib-payload-template-editor [value]=\"tpl()\" [formInputs]=\"inputs()\"\r\n *     (valueChanged)=\"onTemplate($event)\" />\r\n */\r\n@Component({\r\n  selector: 'lib-payload-template-editor',\r\n  templateUrl: './payload-template-editor.component.html',\r\n  styleUrl: './payload-template-editor.component.scss',\r\n  changeDetection: ChangeDetectionStrategy.OnPush,\r\n  encapsulation: ViewEncapsulation.Emulated,\r\n  host: { class: 'lib-payload-template-editor' },\r\n  imports: [MatIconModule, JsonEditorComponent],\r\n})\r\nexport class PayloadTemplateEditorComponent {\r\n  /** Current template (`undefined` when nothing has been authored yet). */\r\n  readonly value = input<PayloadTemplate | undefined>(undefined);\r\n\r\n  /**\r\n   * Default template shown when nothing has been authored yet — the endpoint's\r\n   * captured request body, or a skeleton derived from the fetch's required inputs.\r\n   * The builder also persists this via the descriptor's `defaultValue`; this input\r\n   * only makes it appear immediately instead of after the persist round-trip.\r\n   */\r\n  readonly defaultTemplate = input<PayloadTemplate | undefined>(undefined);\r\n\r\n  /** All form inputs, offered as insertable `{{inputId}}` bindings. */\r\n  readonly formInputs = input<readonly FormColumnInputs[]>([]);\r\n\r\n  /** Disables the editor. */\r\n  readonly disabled = input<boolean>(false);\r\n\r\n  /**\r\n   * Shows the built-in instruction line. A host that provides its own copy for\r\n   * this editor (the endpoint dialog's per-tab hint) passes `false` so the\r\n   * author reads ONE instruction, not two stacked ones.\r\n   */\r\n  readonly showHint = input<boolean>(true);\r\n\r\n  /** Validation errors surfaced by the parent editor. */\r\n  readonly errors = input<IConfigElementError[] | undefined>([]);\r\n\r\n  /** Optional id supplied by the parent editor. */\r\n  readonly id = input<string | undefined>(undefined);\r\n\r\n  /** Emits the parsed template on every valid edit. */\r\n  readonly valueChanged = output<PayloadTemplate>();\r\n\r\n  /** id⇄label token maps (safe + unique labels only; everything else stays a raw id). */\r\n  readonly #tokenMaps = computed(() => buildTokenMaps(bindableFields(this.formInputs())));\r\n\r\n  /** Field bindings for the JSON editor's `$`-trigger picker. */\r\n  protected readonly editorBindings = computed<readonly EditorBinding[]>(() =>\r\n    fieldEditorBindings(this.formInputs(), this.#tokenMaps().idToLabel),\r\n  );\r\n\r\n  /**\r\n   * The value the JSON editor renders — the authored template, or the derived\r\n   * default when none exists yet — with stored `{{id}}` tokens rewritten to the\r\n   * friendly `{{Label}}` form for display.\r\n   */\r\n  protected readonly editorValue = computed<object | undefined>(() =>\r\n    mapTemplateTokens(this.value() ?? this.defaultTemplate(), this.#tokenMaps().idToLabel) as object | undefined,\r\n  );\r\n\r\n  /**\r\n   * Commit a valid edit: rewrite the friendly `{{Label}}` tokens back to the\r\n   * stored `{{id}}` form before emitting. Invalid/empty edits are ignored (last\r\n   * valid stays in effect).\r\n   */\r\n  protected onEditorChange(change: JsonEditorChange): void {\r\n    if (!change.valid) return;\r\n    const stored = mapTemplateTokens(change.value, this.#tokenMaps().labelToId);\r\n    this.valueChanged.emit(stored as PayloadTemplate);\r\n  }\r\n}\r\n","<div class=\"template-editor\">\r\n  @if (showHint()) {\r\n  <p class=\"hint-line\">\r\n    <mat-icon class=\"info-icon\">info</mat-icon>\r\n    <span>Author the request body as JSON. Type <code>$</code> to insert a field by name;\r\n      every other literal is a fixed default. This supersedes the required-input mapping.</span>\r\n  </p>\r\n  }\r\n\r\n  <lib-json-editor\r\n    height=\"360px\"\r\n    [value]=\"editorValue()\"\r\n    [bindings]=\"editorBindings()\"\r\n    [disabled]=\"disabled()\"\r\n    (valueChange)=\"onEditorChange($event)\" />\r\n\r\n  @for (error of errors(); track $index) {\r\n    <p class=\"error-line\"><span>{{ error.message }}</span></p>\r\n  }\r\n</div>\r\n"],"names":[],"mappings":";;;;;;;AAqBA;;;;;;;;;;;;;;;;AAgBG;MAUU,8BAA8B,CAAA;AAT3C,IAAA,WAAA,GAAA;;AAWW,QAAA,IAAA,CAAA,KAAK,GAAG,KAAK,CAA8B,SAAS,4EAAC;AAE9D;;;;;AAKG;AACM,QAAA,IAAA,CAAA,eAAe,GAAG,KAAK,CAA8B,SAAS,sFAAC;;AAG/D,QAAA,IAAA,CAAA,UAAU,GAAG,KAAK,CAA8B,EAAE,iFAAC;;AAGnD,QAAA,IAAA,CAAA,QAAQ,GAAG,KAAK,CAAU,KAAK,+EAAC;AAEzC;;;;AAIG;AACM,QAAA,IAAA,CAAA,QAAQ,GAAG,KAAK,CAAU,IAAI,+EAAC;;AAG/B,QAAA,IAAA,CAAA,MAAM,GAAG,KAAK,CA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