{"version":3,"file":"utils.mjs","names":[],"sources":["../../src/tanstack-form/utils.ts"],"sourcesContent":["import React from \"react\";\nimport { replaceEmptyValue } from \"@autoform/core\";\nimport type { SchemaProvider } from \"@autoform/core\";\n\nexport function createSchemaValidator<T extends Record<string, any>>(\n  schema: SchemaProvider<T>,\n  removeEmptyValue = true,\n) {\n  return ({ value }: { value: T }) => {\n    const validation = schema.validateSchema(\n      removeEmptyValue ? replaceEmptyValue(value) : value,\n    );\n    if (validation.success) return;\n\n    return {\n      fields: Object.fromEntries(\n        validation.errors.map((error) => [\n          formatTanStackPath(error.path.map(String)),\n          error,\n        ]),\n      ),\n    };\n  };\n}\n\nexport function focusFirstFieldInPath(path: string): void {\n  const exact = CSS.escape(path);\n  const prefix = CSS.escape(`${path}.`);\n  document\n    .querySelector<HTMLElement>(\n      `:is(input:not([type=\"hidden\"]),select,textarea,button,[tabindex]):not(:disabled):is([id=\"${exact}\"],[name=\"${exact}\"],[id^=\"${prefix}\"],[name^=\"${prefix}\"])`,\n    )\n    ?.focus();\n}\n\nexport function getAppForm(form: {\n  AppForm?: React.ComponentType<React.PropsWithChildren>;\n}): React.ComponentType<React.PropsWithChildren> {\n  const AppForm = form.AppForm;\n  if (!AppForm) {\n    throw new Error(\n      \"TanStack AutoForm formControl must be created with useAppForm \" +\n        \"exported from @autoform/react/tanstack-form.\",\n    );\n  }\n  return AppForm;\n}\n\n/**\n * Formats an array of path segments into a TanStack Form compatible string path.\n * e.g., [\"guests\", \"0\", \"name\"] -> \"guests[0].name\"\n */\nexport function formatTanStackPath(path: string[]): string {\n  return path.reduce((acc, part) => {\n    if (!isNaN(Number(part))) {\n      return `${acc}[${part}]`;\n    }\n    return acc ? `${acc}.${part}` : part;\n  }, \"\");\n}\n\nexport function getErrorMessage(fieldApi: any): string | undefined {\n  const error = fieldApi.state.meta.errors?.[0];\n  if (!error) return undefined;\n  if (typeof error === \"string\") return error;\n  if (typeof error.message === \"string\") return error.message;\n  return String(error);\n}\n"],"mappings":";;AAIA,SAAgB,sBACd,QACA,mBAAmB,MACnB;CACA,QAAQ,EAAE,YAA0B;EAClC,MAAM,aAAa,OAAO,eACxB,mBAAmB,kBAAkB,KAAK,IAAI,KAChD;EACA,IAAI,WAAW,SAAS;EAExB,OAAO,EACL,QAAQ,OAAO,YACb,WAAW,OAAO,KAAK,UAAU,CAC/B,mBAAmB,MAAM,KAAK,IAAI,MAAM,CAAC,GACzC,KACF,CAAC,CACH,EACF;CACF;AACF;AAEA,SAAgB,sBAAsB,MAAoB;CACxD,MAAM,QAAQ,IAAI,OAAO,IAAI;CAC7B,MAAM,SAAS,IAAI,OAAO,GAAG,KAAK,EAAE;CACpC,SACG,cACC,4FAA4F,MAAM,YAAY,MAAM,WAAW,OAAO,aAAa,OAAO,IAC5J,CAAC,EACC,MAAM;AACZ;AAEA,SAAgB,WAAW,MAEsB;CAC/C,MAAM,UAAU,KAAK;CACrB,IAAI,CAAC,SACH,MAAM,IAAI,MACR,4GAEF;CAEF,OAAO;AACT;;;;;AAMA,SAAgB,mBAAmB,MAAwB;CACzD,OAAO,KAAK,QAAQ,KAAK,SAAS;EAChC,IAAI,CAAC,MAAM,OAAO,IAAI,CAAC,GACrB,OAAO,GAAG,IAAI,GAAG,KAAK;EAExB,OAAO,MAAM,GAAG,IAAI,GAAG,SAAS;CAClC,GAAG,EAAE;AACP;AAEA,SAAgB,gBAAgB,UAAmC;CACjE,MAAM,QAAQ,SAAS,MAAM,KAAK,SAAS;CAC3C,IAAI,CAAC,OAAO,OAAO,KAAA;CACnB,IAAI,OAAO,UAAU,UAAU,OAAO;CACtC,IAAI,OAAO,MAAM,YAAY,UAAU,OAAO,MAAM;CACpD,OAAO,OAAO,KAAK;AACrB"}