;
{
  try {
    let e = "undefined" != typeof window ? window : "undefined" != typeof global ? global : "undefined" != typeof globalThis ? globalThis : "undefined" != typeof self ? self : {}, n = new e.Error().stack;
    n && (e._sentryDebugIds = e._sentryDebugIds || {}, e._sentryDebugIds[n] = "c7f6b062-f65f-46f1-b6cc-e1c13914b267", e._sentryDebugIdIdentifier = "sentry-dbid-c7f6b062-f65f-46f1-b6cc-e1c13914b267");
  } catch (e) {
  }
}
;
import { jsx, jsxs, Fragment } from "preact/jsx-runtime";
import { useContext, useCallback } from "preact/hooks";
import { V as Button, u as useTranslation, b5 as remediationHasFileUploadField, aD as keysOf } from "./StoreProvider-CqQrndhS.js";
import { a as Alert } from "./Alert-CpDg47bv.js";
import { createContext } from "preact";
const errorPanel = "adyen-kyc-error-panel";
const styles = {
  errorPanel
};
const FormRouterContext = createContext(null);
function useFormRouterContext() {
  const context = useContext(FormRouterContext);
  return context;
}
const NavigateToFieldLink = ({ fieldName, label }) => {
  const { goToFormByFieldName } = useFormRouterContext();
  return /* @__PURE__ */ jsx(
    Button,
    {
      onClick: () => {
        goToFormByFieldName(fieldName);
      },
      variant: "link",
      children: label
    }
  );
};
function ServerValidationErrors({
  validationErrors,
  formUtils
}) {
  const { t } = useTranslation("ui");
  const validationErrorFieldNames = Object.keys(validationErrors || {});
  const getValidationErrorMessage = useCallback(
    () => t(($) => $["fieldHasUnsupportedFormats"], {
      fieldName: formUtils.getLabel(validationErrorFieldNames[0])
    }),
    [t, formUtils, validationErrorFieldNames]
  );
  return /* @__PURE__ */ jsx(Alert, { title: t(($) => $["informationCouldNotBeSubmitted"]), variant: "error", children: validationErrorFieldNames?.length === 1 ? /* @__PURE__ */ jsxs("div", { "data-testid": "validation-error--case-1", children: [
    getValidationErrorMessage(),
    " ",
    /* @__PURE__ */ jsx(
      NavigateToFieldLink,
      {
        fieldName: validationErrorFieldNames[0],
        label: t(($) => $["updateTheInformation"])
      }
    ),
    " ",
    t(($) => $["andResubmit"])
  ] }) : /* @__PURE__ */ jsxs("div", { "data-testid": "validation-error--case-2", children: [
    t(($) => $["someFieldsHaveUnsupportedFormatedUpdateInfoAndResubmit"]),
    /* @__PURE__ */ jsx("ul", { children: validationErrorFieldNames.map((fieldName) => /* @__PURE__ */ jsx(
      "li",
      {
        className: "adyen-kyc-error-panel__validation-error",
        "data-testid": "adyen-kyc-error-panel__validation-error",
        children: /* @__PURE__ */ jsx(NavigateToFieldLink, { fieldName, label: formUtils.getLabel(fieldName) })
      },
      fieldName
    )) })
  ] }) });
}
function VerificationErrors({
  verificationErrors,
  formUtils
}) {
  const { t } = useTranslation("common");
  const getErrorMessage = (errorCode) => t(($) => $[`errorMessage_${errorCode}`]);
  const getRemediationMessage = (remediationActions, uploadFieldCollection) => {
    const uploadFields = uploadFieldCollection ? Object.keys(uploadFieldCollection) : [];
    if (remediationActions?.length === 1 && uploadFields.length === 1) {
      const firstUploadField = uploadFields[0];
      const label = t(
        ($) => $[`remediationMessage_${uploadFieldCollection[firstUploadField]?.remediation.code}`]
      );
      return /* @__PURE__ */ jsx(NavigateToFieldLink, { fieldName: firstUploadField, label });
    }
    if (remediationActions?.length === 1) {
      return t(($) => $[`remediationMessage_${remediationActions[0].code}`]);
    }
    let uploadFilesCopy;
    if (uploadFields.length > 0) {
      if (uploadFields.length > 1) {
        const uploadFieldListItems = uploadFields.map((uploadField) => /* @__PURE__ */ jsx(
          NavigateToFieldLink,
          {
            fieldName: uploadField,
            label: t(
              ($) => $[`remediationMessage_${uploadFieldCollection[uploadField]?.remediation.code}`]
            )
          },
          uploadField
        ));
        uploadFilesCopy = /* @__PURE__ */ jsxs(Fragment, { children: [
          formUtils.getLabel("uploadTheFollowingDocuments"),
          /* @__PURE__ */ jsx("ul", { children: uploadFieldListItems.map((item, i) => /* @__PURE__ */ jsx("li", { children: item }, i)) })
        ] });
      } else {
        const firstUploadField = uploadFields[0];
        const label = ` ${t(
          ($) => $[`remediationMessage_${uploadFieldCollection[firstUploadField]?.remediation.code}`]
        )?.toLowerCase()}`;
        uploadFilesCopy = /* @__PURE__ */ jsx(NavigateToFieldLink, { fieldName: firstUploadField, label });
      }
    }
    return /* @__PURE__ */ jsxs(Fragment, { children: [
      t(($) => $["reviewTheInformationBelowAndCorrectMistakesOr"]),
      " ",
      uploadFilesCopy,
      " ",
      t(($) => $["thenResubmit"])
    ] });
  };
  let verificationErrorMsg;
  let parentCode = void 0;
  const parentCodes = keysOf(verificationErrors);
  if (parentCodes.length === 1) {
    parentCode = parentCodes[0];
  }
  const childErrors = Object.values(verificationErrors).reduce(
    (acc, parentChildErrors) => ({
      ...acc,
      ...parentChildErrors
    }),
    {}
  );
  const childErrorCodes = Object.keys(childErrors);
  const fileUploadRemediationFields = Object.values(childErrors).flat().reduce((acc, rem) => {
    const isRemUploadOne = remediationHasFileUploadField(rem);
    const firstRemediationField = rem.fields?.[0];
    if (isRemUploadOne && firstRemediationField) {
      acc[firstRemediationField] = {
        formName: rem?.forms[0],
        remediation: rem
      };
    }
    return acc;
  }, {});
  const onlyChildRemediationActions = Object.values(childErrors).flat();
  if (childErrorCodes.length === 1) {
    verificationErrorMsg = /* @__PURE__ */ jsxs(
      "div",
      {
        "data-testid": `verification-error--case-${onlyChildRemediationActions.length === 1 ? "1" : "2-1"}`,
        children: [
          /* @__PURE__ */ jsx("span", { children: `${getErrorMessage(childErrorCodes[0])} ` }),
          /* @__PURE__ */ jsx("span", { children: getRemediationMessage(onlyChildRemediationActions, fileUploadRemediationFields) })
        ]
      }
    );
  } else if (childErrorCodes.length === 2) {
    verificationErrorMsg = /* @__PURE__ */ jsxs("div", { "data-testid": "verification-error--case-2-2", children: [
      t(($) => $["weFoundTheFollowingIssues"]),
      /* @__PURE__ */ jsx("ul", { children: childErrorCodes.map((subErrorCode) => /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsx("span", { children: getErrorMessage(subErrorCode) }) }, subErrorCode)) }),
      /* @__PURE__ */ jsx("span", { children: getRemediationMessage(onlyChildRemediationActions, fileUploadRemediationFields) })
    ] });
  } else {
    verificationErrorMsg = /* @__PURE__ */ jsx("div", { "data-testid": "verification-error--case-2-3", children: getRemediationMessage(onlyChildRemediationActions, fileUploadRemediationFields) });
  }
  const defaultHeading = parentCode ? getErrorMessage(parentCode) : t(($) => $["correctErrorsResubmit"]);
  return /* @__PURE__ */ jsx(Alert, { title: defaultHeading, variant: "error", children: verificationErrorMsg });
}
function ErrorPanel({
  id = "ariaErrorField",
  verificationErrors,
  validationErrors,
  formUtils
}) {
  const isVerificationError = verificationErrors && Object.keys(verificationErrors).length > 0;
  const isValidationError = validationErrors && Object.keys(validationErrors).length > 0;
  if (!isVerificationError && !isValidationError) {
    return null;
  }
  return /* @__PURE__ */ jsxs("div", { className: styles.errorPanel, id, "aria-live": "polite", children: [
    isVerificationError && !isValidationError && /* @__PURE__ */ jsx(VerificationErrors, { formUtils, verificationErrors }),
    isValidationError && /* @__PURE__ */ jsx(ServerValidationErrors, { formUtils, validationErrors })
  ] });
}
export {
  ErrorPanel as E,
  FormRouterContext as F,
  useFormRouterContext as u
};
