{"version":3,"file":"form-D2-dJPN2.cjs","names":[],"sources":["../src/utils/form.ts"],"sourcesContent":["/**\r\n * Form participation utility for custom elements.\r\n *\r\n * Since @bquery/bquery does not support `static formAssociated = true`,\r\n * this module provides a hidden-input proxy pattern that allows custom\r\n * elements to participate in native `<form>` submission.\r\n */\r\n\r\nexport type FormProxyOptions = {\r\n  /**\r\n   * Optional callback invoked just before native form submission to\r\n   * return the most-current value. Lets components avoid stale state\r\n   * caused by missed `setValue` calls during fast user interaction\r\n   * (e.g. slider drag-end racing with submit).\r\n   */\r\n  getLiveValue?: () => string;\r\n};\r\n\r\nexport type FormProxy = {\r\n  /** Update the proxied form value */\r\n  setValue(value: string): void;\r\n  /** Update the proxied name attribute */\r\n  setName(name: string): void;\r\n  /** Enable or disable the proxy */\r\n  setDisabled(disabled: boolean): void;\r\n  /** Remove the proxy from the DOM */\r\n  cleanup(): void;\r\n};\r\n\r\n/**\r\n * Creates a hidden `<input type=\"hidden\">` in the light DOM immediately\r\n * after the host element. The hidden input mirrors name/value/disabled\r\n * so that the custom element data is included in native form submission.\r\n *\r\n * Pass `options.getLiveValue` to refresh the value lazily on the\r\n * `formdata` event of the closest ancestor `<form>`.\r\n */\r\nexport function createFormProxy(\r\n  host: HTMLElement,\r\n  name: string,\r\n  value: string,\r\n  disabled = false,\r\n  options: FormProxyOptions = {}\r\n): FormProxy {\r\n  const input = document.createElement('input');\r\n  input.type = 'hidden';\r\n  input.name = name;\r\n  input.value = value;\r\n  input.disabled = disabled;\r\n  input.setAttribute('data-bq-form-proxy', '');\r\n\r\n  host.insertAdjacentElement('afterend', input);\r\n\r\n  let form: HTMLFormElement | null = null;\r\n  let onFormData: ((event: FormDataEvent) => void) | null = null;\r\n\r\n  if (options.getLiveValue) {\r\n    form = host.closest('form');\r\n    if (form) {\r\n      const liveValue = options.getLiveValue;\r\n      onFormData = () => {\r\n        try {\r\n          input.value = liveValue();\r\n        } catch {\r\n          /* best-effort sync — ignore */\r\n        }\r\n      };\r\n      form.addEventListener('formdata', onFormData);\r\n    }\r\n  }\r\n\r\n  return {\r\n    setValue(v: string) {\r\n      input.value = v;\r\n    },\r\n    setName(n: string) {\r\n      input.name = n;\r\n    },\r\n    setDisabled(d: boolean) {\r\n      input.disabled = d;\r\n    },\r\n    cleanup() {\r\n      if (form && onFormData) {\r\n        form.removeEventListener('formdata', onFormData);\r\n        form = null;\r\n        onFormData = null;\r\n      }\r\n      // Defer the sibling removal: removing a sibling synchronously inside\r\n      // the host's `disconnectedCallback` re-enters DOM mutation while the\r\n      // parent is still iterating, which trips happy-dom's child-tracking\r\n      // and is at best wasteful in real browsers. Microtask is enough to\r\n      // let the current removal settle.\r\n      const drop = () => {\r\n        if (input.isConnected) input.remove();\r\n      };\r\n      if (typeof queueMicrotask === 'function') queueMicrotask(drop);\r\n      else Promise.resolve().then(drop);\r\n    },\r\n  };\r\n}\r\n"],"mappings":";;;;;;;;;AAqCA,SAAgB,gBACd,MACA,MACA,OACA,WAAW,OACX,UAA4B,EAAE,EACnB;CACX,MAAM,QAAQ,SAAS,cAAc,QAAQ;AAC7C,OAAM,OAAO;AACb,OAAM,OAAO;AACb,OAAM,QAAQ;AACd,OAAM,WAAW;AACjB,OAAM,aAAa,sBAAsB,GAAG;AAE5C,MAAK,sBAAsB,YAAY,MAAM;CAE7C,IAAI,OAA+B;CACnC,IAAI,aAAsD;AAE1D,KAAI,QAAQ,cAAc;AACxB,SAAO,KAAK,QAAQ,OAAO;AAC3B,MAAI,MAAM;GACR,MAAM,YAAY,QAAQ;AAC1B,sBAAmB;AACjB,QAAI;AACF,WAAM,QAAQ,WAAW;YACnB;;AAIV,QAAK,iBAAiB,YAAY,WAAW;;;AAIjD,QAAO;EACL,SAAS,GAAW;AAClB,SAAM,QAAQ;;EAEhB,QAAQ,GAAW;AACjB,SAAM,OAAO;;EAEf,YAAY,GAAY;AACtB,SAAM,WAAW;;EAEnB,UAAU;AACR,OAAI,QAAQ,YAAY;AACtB,SAAK,oBAAoB,YAAY,WAAW;AAChD,WAAO;AACP,iBAAa;;GAOf,MAAM,aAAa;AACjB,QAAI,MAAM,YAAa,OAAM,QAAQ;;AAEvC,OAAI,OAAO,mBAAmB,WAAY,gBAAe,KAAK;OACzD,SAAQ,SAAS,CAAC,KAAK,KAAK;;EAEpC"}