;
{
  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] = "91ceb501-82f2-4c77-8542-fd71d8769a11", e._sentryDebugIdIdentifier = "sentry-dbid-91ceb501-82f2-4c77-8542-fd71d8769a11");
  } catch (e) {
  }
}
;
import { b as useApiContext, $ as httpPost, a3 as createLogger, u as useTranslation, a as addResourceBundles, _ as __variableDynamicImportRuntimeHelper, L as Loader, V as Button } from "./StoreProvider-CqQrndhS.js";
import { jsx, jsxs, Fragment } from "preact/jsx-runtime";
import { useState, useMemo, useCallback, useEffect } from "preact/hooks";
import { useMutation } from "@tanstack/preact-query";
import { u as useOnboardingStatus } from "./useOnboardingStatus-Sxg0PEE-.js";
import { O as OnboardingStatuses } from "./view-verification-status-VFcBydkR.js";
import { a as Alert } from "./Alert-CpDg47bv.js";
const generateOnboardingLink = async (legalEntityId, baseUrl, data) => {
  return httpPost(
    {
      baseUrl,
      path: `legalEntities/${legalEntityId}/onboardingStatus`
    },
    data
  );
};
const useGenerateOnboardingLink = (options) => {
  const { rootLegalEntityId, baseUrl } = useApiContext();
  return useMutation({
    mutationFn: (data) => generateOnboardingLink(rootLegalEntityId.value, baseUrl.value, data),
    ...options
  });
};
const coerceError = (err) => {
  if (err instanceof Error) return err;
  if (typeof err === "object") {
    if (err !== null && "message" in err && typeof err.message === "string") {
      return new Error(err.message);
    }
  }
  return new Error("Unknown error");
};
const contentMap = {
  [OnboardingStatuses.AWAITING_DATA]: {
    description: ["onboardingRedirectAwaitingDescriptionP1"],
    ctaButton: "onboardingRedirectAwaitingCtaButton"
  },
  [OnboardingStatuses.IN_REVIEW]: {
    description: ["onboardingRedirectInReviewDescriptionP1"],
    ctaButton: "onboardingRedirectInReviewCtaButton"
  },
  [OnboardingStatuses.UNSUCCESSFUL]: {
    description: ["onboardingRedirectUnsuccessfulDescriptionP1"],
    ctaButton: "onboardingRedirectUnsuccessfulCtaButton"
  },
  [OnboardingStatuses.REJECTED]: {
    description: ["onboardingRedirectRejectedDescriptionP1"]
  },
  [OnboardingStatuses.VERIFIED]: {
    description: ["onboardingRedirectVerifiedDescriptionP1"]
  }
};
function getContentByStatus(status) {
  return contentMap[status];
}
const verification = "adyen-kyc-verification";
const verificationDescription = "adyen-kyc-verification-description";
const styles = {
  verification,
  verificationDescription
};
const FINAL_STATUSES = [
  OnboardingStatuses.VERIFIED,
  OnboardingStatuses.REJECTED
];
const logger = createLogger();
function ViewVerificationStatusComponent({
  legalEntityId,
  hideExplanation = false,
  returnUrl,
  redirectTarget = "_blank",
  themeId,
  onStatusChange,
  onError,
  onRedirect
}) {
  const { i18n, t } = useTranslation("verificationStatus");
  addResourceBundles(i18n, [
    { ns: "verificationStatus", importFn: (lang) => __variableDynamicImportRuntimeHelper(/* @__PURE__ */ Object.assign({ "./language/bg-BG.json": () => import("./bg-BG-Coja327N.js"), "./language/cs-CZ.json": () => import("./cs-CZ-DXtJsBPz.js"), "./language/da-DK.json": () => import("./da-DK-DWA8uOd8.js"), "./language/de-DE.json": () => import("./de-DE-D50lGOJE.js"), "./language/el-GR.json": () => import("./el-GR-CKHMsdx2.js"), "./language/en-US.json": () => import("./en-US-D2yrwtzT.js"), "./language/es-ES.json": () => import("./es-ES-DF_L5EDd.js"), "./language/et-EE.json": () => import("./et-EE-BuBiNLCi.js"), "./language/fi-FI.json": () => import("./fi-FI-DH6jpo9K.js"), "./language/fr-FR.json": () => import("./fr-FR-BnblnBsx.js"), "./language/hr-HR.json": () => import("./hr-HR-De5kHU8U.js"), "./language/hu-HU.json": () => import("./hu-HU-BcogZby3.js"), "./language/it-IT.json": () => import("./it-IT-Ckv8Ce4e.js"), "./language/ja-JP.json": () => import("./ja-JP-FWHXsXlW.js"), "./language/lt-LT.json": () => import("./lt-LT-DDdq07sX.js"), "./language/lv-LV.json": () => import("./lv-LV-CkyuY2ji.js"), "./language/nl-NL.json": () => import("./nl-NL-Bx5p0nXL.js"), "./language/no-NO.json": () => import("./no-NO-Bs3-6MDM.js"), "./language/pl-PL.json": () => import("./pl-PL-BzhEoDb5.js"), "./language/pt-BR.json": () => import("./pt-BR-DSENwwGu.js"), "./language/pt-PT.json": () => import("./pt-PT-CIo3mlXL.js"), "./language/ro-RO.json": () => import("./ro-RO-ILTvlS9i.js"), "./language/sk-SK.json": () => import("./sk-SK-Cv23cDQO.js"), "./language/sl-SI.json": () => import("./sl-SI-DEaeEVKB.js"), "./language/sv-SE.json": () => import("./sv-SE-CXaMlJCI.js") }), `./language/${lang}.json`, 3) }
  ]);
  const { rootLegalEntityId: currentLegalEntityId } = useApiContext();
  currentLegalEntityId.value = legalEntityId;
  const config = {
    locale: i18n.language,
    returnUrl,
    themeId
  };
  const [onboardingStatus, setOnboardingStatus] = useState();
  const [alertContent, setAlertContent] = useState();
  const content = useMemo(() => {
    if (!onboardingStatus) return;
    const statusContent = getContentByStatus(onboardingStatus);
    if (statusContent) return statusContent;
    logger.error(`there is no content for status "${onboardingStatus}"`);
  }, [onboardingStatus]);
  const handleError = useCallback(
    (rawError, userMessage) => {
      const error = coerceError(rawError);
      logger.error(error);
      setAlertContent(userMessage);
      onError?.({
        error,
        userMessage: t(($) => $[userMessage])
      });
    },
    [t, onError]
  );
  const updateOnboardingStatus = useCallback(
    (newStatus) => {
      if (!newStatus) return;
      setOnboardingStatus(newStatus);
      if (newStatus !== onboardingStatus) onStatusChange?.(newStatus);
    },
    [onStatusChange, onboardingStatus]
  );
  const { isLoading, data, isError } = useOnboardingStatus({
    refetchInterval: (res) => {
      const status = res.state.data?.status;
      setOnboardingStatus(status);
      return status === void 0 || !FINAL_STATUSES.includes(status) ? 3e3 : false;
    }
  });
  useEffect(() => {
    setAlertContent(void 0);
    updateOnboardingStatus(data?.status);
  }, [data, updateOnboardingStatus]);
  const generateOnboardingLink2 = useGenerateOnboardingLink({
    onSuccess: ({ redirectUrl }) => {
      if (!redirectUrl) {
        handleError(new Error("No redirectUrl returned"), "onboardingRedirectLinkError");
        return;
      }
      openUrl(redirectUrl, config);
    },
    onError: (error) => {
      handleError(error, "onboardingRedirectLinkError");
    }
  });
  const openUrl = (url, config2) => {
    const newTab = window.open(url, redirectTarget);
    if (!newTab) {
      handleError(new Error("Could not open redirect in new tab"), "onboardingRedirectOpenError");
      return;
    }
    onRedirect?.({
      ...config2,
      onboardingLink: url
    });
  };
  const showAlert = alertContent || isError;
  const hideButton = onboardingStatus === OnboardingStatuses.REJECTED || onboardingStatus === OnboardingStatuses.VERIFIED || !content?.ctaButton;
  if (!showAlert && (isLoading || !content)) {
    return /* @__PURE__ */ jsx(Loader, {});
  }
  return /* @__PURE__ */ jsx("section", { className: styles.verification, children: showAlert ? /* @__PURE__ */ jsx(Alert, { title: t(($) => $[alertContent ?? "onboardingRedirectLinkError"]), variant: "error" }) : /* @__PURE__ */ jsxs(Fragment, { children: [
    !hideExplanation && content?.description.map((key) => /* @__PURE__ */ jsx("p", { className: styles.verificationDescription, children: t(($) => $[key]) }, key)),
    !hideButton && /* @__PURE__ */ jsx(
      Button,
      {
        onClick: () => {
          generateOnboardingLink2.mutate(config);
        },
        variant: "link",
        loading: generateOnboardingLink2.isPending,
        children: t(($) => $[content.ctaButton])
      }
    )
  ] }) });
}
export {
  ViewVerificationStatusComponent as V
};
