{"version":3,"file":"useFormEngine.mjs","names":[],"sources":["../../../../../../../react-form/src/components/useFormEngine.ts"],"sourcesContent":["\"use client\";\n\nimport { useEffect, useId, useRef } from \"react\";\nimport { FormEngine, FormEngineOptions } from \"../engine/FormEngine\";\nimport { FormProps } from \"../types\";\n\nfunction buildOptions(props: FormProps<any>, id: string): FormEngineOptions {\n  return {\n    id,\n    defaultValue: props.defaultValue,\n    values: props.values,\n    schema: props.schema,\n    validateOn: props.validateOn,\n    ignoreEmptyValues: props.ignoreEmptyValues,\n    focusFirstError: props.focusFirstError,\n    onSubmit: props.onSubmit,\n    onError: props.onError,\n  };\n}\n\n/**\n * Shared wiring for the `Form` / `NativeForm` function components:\n *\n * - Lazily instantiates a single {@link FormEngine} held in a ref (stable\n *   identity for context + the event bus).\n * - Derives an SSR-safe id from React's `useId()` when none is provided.\n * - Syncs the latest scalar options/handlers into the engine each render.\n * - Re-hydrates controls when the reactive `values` prop identity changes, and\n *   updates the reset baseline when `defaultValue` changes.\n * - Activates/destroys the active-form registry on mount/unmount.\n *\n * @param submitHandler platform-specific submit trigger invoked by `form.submit()`.\n */\nexport function useFormEngine(\n  props: FormProps<any>,\n  submitHandler: (engine: FormEngine) => void\n) {\n  const reactId = useId();\n  const id = props.id ? String(props.id) : `form-${reactId}`;\n\n  const engineRef = useRef<FormEngine>();\n\n  if (!engineRef.current) {\n    engineRef.current = new FormEngine(buildOptions(props, id));\n  }\n\n  const engine = engineRef.current;\n\n  // Sync scalar options/handlers every render. This only mutates the engine's\n  // option bag (no React state), so it is safe during render.\n  engine.setOptions(buildOptions(props, id));\n  engine.submitHandler = () => submitHandler(engine);\n\n  // Reactive value hydration. Gate on the last-applied identity (not a\n  // first-run boolean) so React StrictMode's double-mount does not fire a\n  // spurious `fill()` with the unchanged initial `values`. The constructor\n  // already seeded `hydrationValues` from the initial `props.values`.\n  const lastValues = useRef(props.values);\n  useEffect(() => {\n    if (props.values === undefined || props.values === lastValues.current) {\n      return;\n    }\n\n    lastValues.current = props.values;\n    engine.fill(props.values, { dirty: false, validate: false });\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [props.values]);\n\n  // Reactive reset baseline — same identity-gated pattern. The constructor\n  // already applied the initial `props.defaultValue`.\n  const lastDefaultValue = useRef(props.defaultValue);\n  useEffect(() => {\n    if (props.defaultValue === lastDefaultValue.current) {\n      return;\n    }\n\n    lastDefaultValue.current = props.defaultValue;\n    engine.setDefaultValue(props.defaultValue);\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [props.defaultValue]);\n\n  useEffect(() => {\n    engine.activate();\n\n    return () => {\n      engine.destroy();\n    };\n  }, [engine]);\n\n  return { engine, id };\n}\n"],"mappings":";;;;;;AAMA,SAAS,aAAa,OAAuB,IAA+B;CAC1E,OAAO;EACL;EACA,cAAc,MAAM;EACpB,QAAQ,MAAM;EACd,QAAQ,MAAM;EACd,YAAY,MAAM;EAClB,mBAAmB,MAAM;EACzB,iBAAiB,MAAM;EACvB,UAAU,MAAM;EAChB,SAAS,MAAM;CACjB;AACF;;;;;;;;;;;;;;AAeA,SAAgB,cACd,OACA,eACA;CACA,MAAM,UAAU,MAAM;CACtB,MAAM,KAAK,MAAM,KAAK,OAAO,MAAM,EAAE,IAAI,QAAQ;CAEjD,MAAM,YAAY,OAAmB;CAErC,IAAI,CAAC,UAAU,SACb,UAAU,UAAU,IAAI,WAAW,aAAa,OAAO,EAAE,CAAC;CAG5D,MAAM,SAAS,UAAU;CAIzB,OAAO,WAAW,aAAa,OAAO,EAAE,CAAC;CACzC,OAAO,sBAAsB,cAAc,MAAM;CAMjD,MAAM,aAAa,OAAO,MAAM,MAAM;CACtC,gBAAgB;EACd,IAAI,MAAM,WAAW,UAAa,MAAM,WAAW,WAAW,SAC5D;EAGF,WAAW,UAAU,MAAM;EAC3B,OAAO,KAAK,MAAM,QAAQ;GAAE,OAAO;GAAO,UAAU;EAAM,CAAC;CAE7D,GAAG,CAAC,MAAM,MAAM,CAAC;CAIjB,MAAM,mBAAmB,OAAO,MAAM,YAAY;CAClD,gBAAgB;EACd,IAAI,MAAM,iBAAiB,iBAAiB,SAC1C;EAGF,iBAAiB,UAAU,MAAM;EACjC,OAAO,gBAAgB,MAAM,YAAY;CAE3C,GAAG,CAAC,MAAM,YAAY,CAAC;CAEvB,gBAAgB;EACd,OAAO,SAAS;EAEhB,aAAa;GACX,OAAO,QAAQ;EACjB;CACF,GAAG,CAAC,MAAM,CAAC;CAEX,OAAO;EAAE;EAAQ;CAAG;AACtB"}