;
{
  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] = "6e61fe93-ca7d-4775-82bd-78e13c53ff35", e._sentryDebugIdIdentifier = "sentry-dbid-6e61fe93-ca7d-4775-82bd-78e13c53ff35");
  } catch (e) {
  }
}
;
import { b as useApiContext, k as httpGet, aa as useI18nContext, u as useTranslation, a9 as getCapabilityProblems, V as Button, L as Loader, ab as cloneObject, ac as entriesOf, q as TaskStatuses, a3 as createLogger, a as addResourceBundles, _ as __variableDynamicImportRuntimeHelper, z as Typography } from "./StoreProvider-CqQrndhS.js";
import { jsxs, Fragment, jsx } from "preact/jsx-runtime";
import { u as useDeleteTransferInstrument } from "./useDeleteTransferInstrument-Dwy8pw7H.js";
import { useQuery } from "@tanstack/preact-query";
import { u as useToastContext } from "./useToastContext-CPvuqSMm.js";
import { u as useDataset } from "./useDataset-DgIHjoGg.js";
import { a as Alert } from "./Alert-CpDg47bv.js";
import { s as statusToTag, E as EmbeddedStatus } from "./EmbeddedStatus-DOl241T2.js";
import { a as datasetUtilities, d as datasetIdentifier } from "./datasetUtil-BHQp0-xL.js";
import { useState } from "preact/hooks";
import { m as mapTransferInstrumentToPayoutAccount, u as useTransferInstrument } from "./mapTransferInstrumentToPayoutAccount-12gPvtW8.js";
import { C as Card } from "./Card-OQuFzmxm.js";
import { M as Modal } from "./Modal-1lLoobr9.js";
import { S as StructuredList } from "./StructuredList-BNLhk_zV.js";
import { T as Tag } from "./Tag-DdkVScyr.js";
import { D as DetailViewLayout } from "./DetailViewLayout-Z9cuUp4X.js";
import { V as VerificationErrorAlert } from "./VerificationErrorAlert-CHLilqXN.js";
import { C as Confirm } from "./Confirm-BeimUgOe.js";
import { c as currencyByCountry } from "./types-BQgBwM6A.js";
const getTransferInstruments = async (legalEntityId, baseUrl) => {
  return httpGet({
    baseUrl,
    path: `legalEntities/${legalEntityId}/transferInstruments`
  });
};
const useTransferInstruments = (options) => {
  const { rootLegalEntityId, baseUrl } = useApiContext();
  return useQuery({
    queryKey: ["transferInstruments"],
    queryFn: () => getTransferInstruments(rootLegalEntityId.value, baseUrl.value),
    ...options
  });
};
function ManageTransferInstrumentOverviewItemModal({
  status,
  transferInstrument,
  onEdit,
  onClose,
  onRemove,
  onInitiateRemove
}) {
  const { i18n } = useI18nContext();
  const { t } = useTranslation("ui");
  const [confirmOpen, setConfirmOpen] = useState(false);
  const datasetUtils = datasetUtilities(i18n.locale);
  const isReviewInProgress = status === "PROCESSING";
  const showEditButton = !transferInstrument?.bankAccount.trustedSource;
  const problems = transferInstrument ? getCapabilityProblems(transferInstrument, transferInstrument.bankAccount.countryCode) : {};
  const formatDataForStructuredList = (data) => {
    const summaryData = cloneObject(data);
    if (!summaryData.payoutAccountDetails) return [];
    if (summaryData.payoutVerificationMethod?.bankCountry) {
      summaryData.payoutAccountDetails.bankCountry = datasetUtils.getCountryName(
        summaryData.payoutVerificationMethod.bankCountry
      );
      summaryData.payoutAccountDetails.currency = currencyByCountry[summaryData.payoutVerificationMethod.bankCountry]?.[0];
    }
    delete summaryData.payoutAccountDetails.transferInstrumentId;
    return entriesOf(summaryData.payoutAccountDetails).map(([key, value]) => ({
      term: i18n.get(key),
      details: value
    }));
  };
  const getStatusTag = () => {
    const statusTagProps = statusToTag(TaskStatuses[status], t);
    if (!statusTagProps) return;
    return /* @__PURE__ */ jsx(Tag, { variant: statusTagProps.variant, children: statusTagProps.text });
  };
  const getPayoutDetailSummary = (ti) => {
    const mappedTI = formatDataForStructuredList(mapTransferInstrumentToPayoutAccount(ti));
    const statusTag = getStatusTag();
    mappedTI.push({ term: i18n.get("status"), details: statusTag });
    return mappedTI;
  };
  const getStructuredLists = () => {
    if (!transferInstrument) return [];
    const summaries = [];
    summaries.push(getPayoutDetailSummary(transferInstrument));
    if (transferInstrument.documentDetails) {
      summaries.push([
        { term: i18n.get("verificationMethod"), details: i18n.get("documentUpload") },
        {
          term: i18n.get("documentType"),
          details: i18n.get(transferInstrument.documentDetails[0]?.type)
        },
        {
          term: i18n.get("documentName"),
          details: transferInstrument.documentDetails[0]?.fileName
        }
      ]);
    }
    return summaries;
  };
  const handleRemove = async () => {
    await onRemove(transferInstrument.id);
    onClose();
  };
  const handleDeleteClick = async () => {
    if (onInitiateRemove) {
      const confirmDeletion = onInitiateRemove();
      if (confirmDeletion) await handleRemove();
    } else {
      setConfirmOpen(true);
    }
  };
  const actions = /* @__PURE__ */ jsxs(Fragment, { children: [
    transferInstrument && showEditButton && /* @__PURE__ */ jsx(Button, { onClick: () => onEdit(transferInstrument.id), disabled: isReviewInProgress, children: i18n.get("edit") }),
    /* @__PURE__ */ jsx(Button, { variant: "secondary", onClick: handleDeleteClick, disabled: isReviewInProgress, children: i18n.get("delete") })
  ] });
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(Modal, { onClose, ariaLabel: i18n.get("bankAccount"), inset: true, children: !transferInstrument ? /* @__PURE__ */ jsx(Loader, {}) : /* @__PURE__ */ jsxs(DetailViewLayout, { title: i18n.get("bankAccount"), actions, compact: true, children: [
      !!problems.BankAccount?.[transferInstrument.id]?.verificationErrors && /* @__PURE__ */ jsx(VerificationErrorAlert, { problems: problems.BankAccount?.[transferInstrument.id], children: /* @__PURE__ */ jsx(Button, { variant: "link", onClick: () => onEdit(transferInstrument.id), children: i18n.get("editDetails") }) }),
      getStructuredLists().map((listItems) => /* @__PURE__ */ jsx(Card, { variant: "secondary", children: /* @__PURE__ */ jsx(StructuredList, { items: listItems }) }, listItems))
    ] }) }),
    confirmOpen && /* @__PURE__ */ jsx(
      Confirm,
      {
        title: i18n.get("areYouSureYouWantToDeleteThisBankAccount"),
        onConfirm: handleRemove,
        confirmText: i18n.get("delete"),
        onCancel: () => setConfirmOpen(false),
        critical: true
      }
    )
  ] });
}
function ManageTransferInstrumentOverviewItem({
  transferInstrumentReference,
  onEdit,
  onRemove,
  onInitiateRemove
}) {
  const { i18n } = useI18nContext();
  const { status, transferInstrument: transferInstrumentRef } = transferInstrumentReference;
  const [fetchedTi, setFetchedTi] = useState(false);
  const [modalOpen, setModalOpen] = useState(false);
  const { data: transferInstrument } = useTransferInstrument(transferInstrumentRef.id, {
    enabled: fetchedTi
  });
  const isDetailsRequired = status === "DETAILS_REQUIRED";
  const accountIdentifier = transferInstrumentRef.accountIdentifier?.replaceAll("*", "•");
  const handleClick = () => {
    if (!fetchedTi) setFetchedTi(true);
    if (isDetailsRequired) {
      onEdit(transferInstrumentRef.id);
      return;
    }
    setModalOpen(true);
  };
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(
      EmbeddedStatus,
      {
        status: TaskStatuses[status],
        className: "adyen-kyc-manage-ti-overview-item",
        iconName: "bank",
        title: accountIdentifier,
        subtitle: i18n.get("bankAccount"),
        onClick: handleClick,
        actionIndicator: isDetailsRequired ? { text: i18n.get("continue") } : void 0,
        "aria-label": `${i18n.get("bankAccount")} ${accountIdentifier}`
      }
    ),
    modalOpen && /* @__PURE__ */ jsx(
      ManageTransferInstrumentOverviewItemModal,
      {
        transferInstrument,
        onEdit,
        onRemove,
        onInitiateRemove,
        onClose: () => setModalOpen(false),
        status
      }
    )
  ] });
}
function ManageTransferInstrumentOverview({
  transferInstrumentReferences,
  legalEntityId,
  onEdit,
  onAdd,
  onRemove,
  onInitiateRemove
}) {
  const { i18n } = useI18nContext();
  const transferInstrumentListEle = transferInstrumentReferences?.map((transferInstrumentRef) => {
    if (!transferInstrumentRef?.transferInstrument?.id || !transferInstrumentRef.status) return;
    return /* @__PURE__ */ jsx(
      ManageTransferInstrumentOverviewItem,
      {
        transferInstrumentReference: transferInstrumentRef,
        onEdit,
        onRemove,
        onInitiateRemove
      },
      transferInstrumentRef.transferInstrument?.id
    );
  });
  return /* @__PURE__ */ jsxs("section", { className: "adyen-kyc-manage-ti-overview", children: [
    transferInstrumentListEle,
    /* @__PURE__ */ jsx(
      EmbeddedStatus,
      {
        title: i18n.get("addBankAccount"),
        iconName: "plus",
        light: true,
        onClick: () => onAdd(legalEntityId),
        "aria-label": i18n.get("addBankAccount")
      }
    )
  ] });
}
const REFETCH_INTERVAL = 3e3;
const logger = createLogger();
function ManageTransferInstrumentComponent({
  legalEntityId,
  onAdd,
  onEdit,
  onInitiateRemove,
  onRemoveSuccess
}) {
  const { rootLegalEntityId: currentLegalEntityId } = useApiContext();
  currentLegalEntityId.value = legalEntityId;
  const { i18n } = useI18nContext();
  const { i18n: i18next } = useTranslation(["common", "banking"]);
  const { showToast } = useToastContext();
  const { mutateAsync: deleteTransferInstrument } = useDeleteTransferInstrument();
  addResourceBundles(i18next, [
    { ns: "common", importFn: (lang) => __variableDynamicImportRuntimeHelper(/* @__PURE__ */ Object.assign({ "../../../language/locales/bg-BG.json": () => import("./bg-BG-6XzbmxcW.js"), "../../../language/locales/cs-CZ.json": () => import("./cs-CZ-C6vEQ8P9.js"), "../../../language/locales/da-DK.json": () => import("./da-DK-DzcTI92X.js"), "../../../language/locales/de-DE.json": () => import("./de-DE-dPfrbCii.js"), "../../../language/locales/el-GR.json": () => import("./el-GR-Dazirlmt.js"), "../../../language/locales/en-US.instructions.json": () => import("./en-US.instructions-Ddr7AO05.js"), "../../../language/locales/en-US.json": () => import("./StoreProvider-CqQrndhS.js").then((n) => n.bF), "../../../language/locales/es-ES.json": () => import("./es-ES--IvNed_Q.js"), "../../../language/locales/et-EE.json": () => import("./et-EE-CsTXSlnY.js"), "../../../language/locales/fi-FI.json": () => import("./fi-FI-BUixKfXH.js"), "../../../language/locales/fr-FR.json": () => import("./fr-FR-BM-qNLci.js"), "../../../language/locales/hr-HR.json": () => import("./hr-HR-BF74vdPD.js"), "../../../language/locales/hu-HU.json": () => import("./hu-HU-DYhMVR_c.js"), "../../../language/locales/it-IT.json": () => import("./it-IT-D63gRTDc.js"), "../../../language/locales/ja-JP.json": () => import("./ja-JP-DL0IjTzn.js"), "../../../language/locales/lt-LT.json": () => import("./lt-LT-BX8dOAzK.js"), "../../../language/locales/lv-LV.json": () => import("./lv-LV-dA8yYdKe.js"), "../../../language/locales/nl-NL.json": () => import("./nl-NL-KQ6bciOb.js"), "../../../language/locales/no-NO.json": () => import("./no-NO-Ds3VioxT.js"), "../../../language/locales/pl-PL.json": () => import("./pl-PL-BgbxWHGE.js"), "../../../language/locales/pt-BR.json": () => import("./pt-BR-Ct4s4jXV.js"), "../../../language/locales/pt-PT.json": () => import("./pt-PT-DBEsmXQX.js"), "../../../language/locales/ro-RO.json": () => import("./ro-RO-C00Ig8CL.js"), "../../../language/locales/sk-SK.json": () => import("./sk-SK-BbJWayDh.js"), "../../../language/locales/sl-SI.json": () => import("./sl-SI-hxwQIw_i.js"), "../../../language/locales/sv-SE.json": () => import("./sv-SE-0PQ-U1u6.js") }), `../../../language/locales/${lang}.json`, 6) },
    { ns: "ui", importFn: (lang) => __variableDynamicImportRuntimeHelper(/* @__PURE__ */ Object.assign({ "../../ui/language/bg-BG.json": () => import("./bg-BG-zZhDshGi.js"), "../../ui/language/cs-CZ.json": () => import("./cs-CZ-Ck22NTT9.js"), "../../ui/language/da-DK.json": () => import("./da-DK-DQ4ZYSea.js"), "../../ui/language/de-DE.json": () => import("./de-DE-CkbOZ6S_.js"), "../../ui/language/el-GR.json": () => import("./el-GR-DudORw4U.js"), "../../ui/language/en-US.json": () => import("./en-US-CjjPhkB5.js"), "../../ui/language/es-ES.json": () => import("./es-ES-BAolMrZ0.js"), "../../ui/language/et-EE.json": () => import("./et-EE-BOIrYrz9.js"), "../../ui/language/fi-FI.json": () => import("./fi-FI-D8WrLxxq.js"), "../../ui/language/fr-FR.json": () => import("./fr-FR-Beljeilc.js"), "../../ui/language/hr-HR.json": () => import("./hr-HR-lIHN6Yl2.js"), "../../ui/language/hu-HU.json": () => import("./hu-HU-CWdwNCev.js"), "../../ui/language/it-IT.json": () => import("./it-IT-5a7NHVFo.js"), "../../ui/language/ja-JP.json": () => import("./ja-JP-DSvZhFNY.js"), "../../ui/language/lt-LT.json": () => import("./lt-LT-CgalYpUh.js"), "../../ui/language/lv-LV.json": () => import("./lv-LV-aAd9SEqx.js"), "../../ui/language/nl-NL.json": () => import("./nl-NL-CmURERx_.js"), "../../ui/language/no-NO.json": () => import("./no-NO-qyqWODMp.js"), "../../ui/language/pl-PL.json": () => import("./pl-PL-q9azN24o.js"), "../../ui/language/pt-BR.json": () => import("./pt-BR-BxoOF8jB.js"), "../../ui/language/pt-PT.json": () => import("./pt-PT-pIChDwiI.js"), "../../ui/language/ro-RO.json": () => import("./ro-RO-DRsaNX9j.js"), "../../ui/language/sk-SK.json": () => import("./sk-SK-7-enbO1N.js"), "../../ui/language/sl-SI.json": () => import("./sl-SI-DTvJT7fC.js"), "../../ui/language/sv-SE.json": () => import("./sv-SE-N-Hqwcjq.js") }), `../../ui/language/${lang}.json`, 5) },
    { ns: "banking", importFn: (lang) => __variableDynamicImportRuntimeHelper(/* @__PURE__ */ Object.assign({ "../../BankAccount/language/bg-BG.json": () => import("./bg-BG-DyX-yYYI.js"), "../../BankAccount/language/cs-CZ.json": () => import("./cs-CZ-BD5bYeuJ.js"), "../../BankAccount/language/da-DK.json": () => import("./da-DK-h8DYQ_Iq.js"), "../../BankAccount/language/de-DE.json": () => import("./de-DE-LQE_LFFL.js"), "../../BankAccount/language/el-GR.json": () => import("./el-GR-DGbzdIL0.js"), "../../BankAccount/language/en-US.json": () => import("./en-US-h0JneJJl.js"), "../../BankAccount/language/es-ES.json": () => import("./es-ES-DdGmXdxO.js"), "../../BankAccount/language/et-EE.json": () => import("./et-EE-D5SNtVsM.js"), "../../BankAccount/language/fi-FI.json": () => import("./fi-FI-CIYa9SyS.js"), "../../BankAccount/language/fr-FR.json": () => import("./fr-FR-l5z31r7i.js"), "../../BankAccount/language/hr-HR.json": () => import("./hr-HR-BtowKWUi.js"), "../../BankAccount/language/hu-HU.json": () => import("./hu-HU-MHIPehPS.js"), "../../BankAccount/language/it-IT.json": () => import("./it-IT-BAvmkEF8.js"), "../../BankAccount/language/ja-JP.json": () => import("./ja-JP-g6VOUGXZ.js"), "../../BankAccount/language/lt-LT.json": () => import("./lt-LT-DDs3ngzd.js"), "../../BankAccount/language/lv-LV.json": () => import("./lv-LV-CmB2-IO5.js"), "../../BankAccount/language/nl-NL.json": () => import("./nl-NL-DV4NqPVW.js"), "../../BankAccount/language/no-NO.json": () => import("./no-NO-B9618F4_.js"), "../../BankAccount/language/pl-PL.json": () => import("./pl-PL-DqzUSnFK.js"), "../../BankAccount/language/pt-BR.json": () => import("./pt-BR-zuqB_CBf.js"), "../../BankAccount/language/pt-PT.json": () => import("./pt-PT-CFY1O_K0.js"), "../../BankAccount/language/ro-RO.json": () => import("./ro-RO-7CJ4F5iB.js"), "../../BankAccount/language/sk-SK.json": () => import("./sk-SK-DubwJifw.js"), "../../BankAccount/language/sl-SI.json": () => import("./sl-SI-59OQIZ3f.js"), "../../BankAccount/language/sv-SE.json": () => import("./sv-SE-CQWBP7n1.js") }), `../../BankAccount/language/${lang}.json`, 5) }
  ]);
  const {
    data: transferInstrumentReferences,
    isLoading,
    isError
  } = useTransferInstruments({
    refetchOnMount: "always",
    refetchInterval: (query) => (
      // if all TIs are verified, we can stop automatic refetching
      query.state.data?.every(({ status }) => status === "FINISHED") ? 0 : REFETCH_INTERVAL
    )
  });
  useDataset(datasetIdentifier.country);
  const onRemove = async (transferInstrumentId) => {
    try {
      await deleteTransferInstrument(transferInstrumentId);
      onRemoveSuccess?.(transferInstrumentId, legalEntityId);
    } catch (e) {
      showToast({ label: i18n.get("thereWasAnErrorTryAgain"), variant: "error" });
      logger.error(e);
    }
  };
  const render = () => {
    if (isLoading) {
      return /* @__PURE__ */ jsx(EmbeddedStatus, { loading: true });
    }
    if (isError) {
      return /* @__PURE__ */ jsx(Alert, { title: i18n.get("sorryAnErrorOccurred"), variant: "error" });
    }
    return /* @__PURE__ */ jsxs(Fragment, { children: [
      !transferInstrumentReferences?.length && /* @__PURE__ */ jsxs("div", { className: "adyen-kyc-u-margin-bottom-12", children: [
        /* @__PURE__ */ jsx(Typography, { el: "h1", variant: "title-l", children: i18n.get("bankDetails") }),
        /* @__PURE__ */ jsx(Typography, { children: i18n.get("addMissingBankDetails") })
      ] }),
      /* @__PURE__ */ jsx(
        ManageTransferInstrumentOverview,
        {
          transferInstrumentReferences,
          onEdit: (transferInstrumentId) => onEdit(transferInstrumentId, legalEntityId),
          onRemove,
          onInitiateRemove,
          onAdd,
          legalEntityId
        }
      )
    ] });
  };
  return /* @__PURE__ */ jsx("div", { className: "adyen-kyc-manage-transfer-instrument", children: render() });
}
export {
  ManageTransferInstrumentComponent as M
};
