import { errorCodeDictionary } from "./errorCodeDictionary.js";
import { useCallback, useMemo } from "react";
import { useMessageFormatter } from "../../../i18n/useMessageFormatter.js";
import { assertNever } from "../../../utils/assertNever.js";
const useAccessorErrorFormatter = () => {
  const formatMessage = useMessageFormatter(errorCodeDictionary);
  return useCallback((errors) => {
    return errors.map((error) => {
      if (error.type === "validation") {
        switch (error.code) {
          case "fieldRequired":
            return { message: formatMessage("errorCodes.fieldRequired") };
          default:
            return error;
        }
      } else if (error.type === "execution") {
        switch (error.code) {
          case "UniqueConstraintViolation":
            return { message: formatMessage("errorCodes.notUnique") };
          default:
            return { message: formatMessage("errorCodes.unknownExecutionError") };
        }
      }
      assertNever();
    });
  }, [formatMessage]);
};
const useAccessorErrors = (accessor) => {
  const errorFormatter = useAccessorErrorFormatter();
  const errors = useMemo(() => errorFormatter(accessor.errors?.errors ?? []), [accessor, errorFormatter]);
  return errors.length > 0 ? errors : void 0;
};
export { useAccessorErrorFormatter, useAccessorErrors };
//# sourceMappingURL=useAccessorErrors.js.map
