{"version":3,"file":"errors.mjs","sources":["../../../../src/lib/form/errors.mts"],"sourcesContent":["import type { FormElement } from '../shared/fields.mjs';\nimport type { FieldValidity, ValidatorFn, ValidationMessages } from '../shared/types.mjs';\n\ninterface ValidationCheckOptions {\n  messages?: ValidationMessages;\n  validators?: Record<string, Record<string, ValidatorFn>>;\n}\n\n/**\n * Extracts validity errors from the element and merges with custom errors.\n */\nfunction extractErrors(el: FormElement, custom?: Record<string, string>): Record<string, string> {\n  const output: Record<string, string> = {};\n  for (const key in el.validity) {\n    if (key !== 'valid' && el.validity[key as keyof ValidityState]) {\n      output[key] = el.validationMessage || key;\n    }\n  }\n  return { ...output, ...custom };\n}\n\n/**\n * Gets the result of any custom validations available on the fields.\n */\nfunction getCustomValidations(value: unknown, values: Record<string, unknown>, validations: Record<string, ValidatorFn> = {}): Record<string, string> {\n  const errors: Record<string, string> = {};\n\n  Object.entries(validations).forEach(([key, validation]) => {\n    const message = validation(value, values);\n    if (message !== null) {\n      errors[key] = message;\n    }\n  });\n\n  return errors;\n}\n\n/**\n * Creates a validation checker for an element group\n */\nexport function createValidationChecker(\n  inputGroup: string,\n  elementGroup: FormElement[],\n  values: Record<string, unknown>,\n  options?: ValidationCheckOptions,\n): (el: FormElement, elValue: unknown) => FieldValidity {\n  return (el: FormElement, elValue: unknown): FieldValidity => {\n    // Reset the validity\n    elementGroup.forEach((groupEl) => {\n      groupEl.setCustomValidity('');\n      groupEl.removeAttribute('data-formula-invalid');\n    });\n\n    // If there are no options, just return the current error\n    if (!options) {\n      const valid = el.checkValidity();\n      if (!valid) {\n        el.setAttribute('data-formula-invalid', 'true');\n      }\n      return {\n        valid,\n        invalid: !valid,\n        message: el.validationMessage,\n        errors: extractErrors(el),\n      };\n    }\n\n    // Check for any custom messages in the options or dataset\n    const customMessages: Record<string, string> = {\n      ...options?.messages?.[inputGroup],\n      ...(el.dataset as Record<string, string>),\n    };\n\n    // Check for any custom validations\n    const customErrors = getCustomValidations(elValue, values, options?.validators?.[inputGroup]);\n\n    const errors = extractErrors(el, customErrors);\n    const errorKeys = Object.keys(errors);\n\n    // If there are errors, check if we have a custom message for the first error\n    if (errorKeys.length > 0) {\n      const firstErrorKey = errorKeys[0];\n      // Check if there's a custom message for this error type\n      if (customMessages[firstErrorKey]) {\n        el.setCustomValidity(customMessages[firstErrorKey]);\n      } else if (customErrors[firstErrorKey]) {\n        // Use the custom validator's message\n        el.setCustomValidity(customErrors[firstErrorKey]);\n      }\n    }\n\n    const valid = el.checkValidity();\n    if (!valid) {\n      el.setAttribute('data-formula-invalid', 'true');\n    }\n\n    return {\n      valid,\n      invalid: !valid,\n      message: el.validationMessage,\n      errors,\n    };\n  };\n}\n"],"names":["extractErrors","el","custom","output","key","getCustomValidations","value","values","validations","errors","validation","message","createValidationChecker","inputGroup","elementGroup","options","elValue","groupEl","valid","customMessages","customErrors","errorKeys","firstErrorKey"],"mappings":"AAWA,SAASA,EAAcC,GAAiBC,GAAyD;AAC/F,QAAMC,IAAiC,CAAA;AACvC,aAAWC,KAAOH,EAAG;AACnB,IAAIG,MAAQ,WAAWH,EAAG,SAASG,CAA0B,MAC3DD,EAAOC,CAAG,IAAIH,EAAG,qBAAqBG;AAG1C,SAAO,EAAE,GAAGD,GAAQ,GAAGD,EAAA;AACzB;AAKA,SAASG,EAAqBC,GAAgBC,GAAiCC,IAA2C,CAAA,GAA4B;AACpJ,QAAMC,IAAiC,CAAA;AAEvC,gBAAO,QAAQD,CAAW,EAAE,QAAQ,CAAC,CAACJ,GAAKM,CAAU,MAAM;AACzD,UAAMC,IAAUD,EAAWJ,GAAOC,CAAM;AACxC,IAAII,MAAY,SACdF,EAAOL,CAAG,IAAIO;AAAA,EAElB,CAAC,GAEMF;AACT;AAKO,SAASG,EACdC,GACAC,GACAP,GACAQ,GACsD;AACtD,SAAO,CAACd,GAAiBe,MAAoC;AAQ3D,QANAF,EAAa,QAAQ,CAACG,MAAY;AAChC,MAAAA,EAAQ,kBAAkB,EAAE,GAC5BA,EAAQ,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