import { jsx as n } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import j from "classnames";
import { useMemo as T, useCallback as r, useState as b, useEffect as J } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import F from "../../../../internal/Button/ButtonActions/ButtonActions.js";
import { ButtonVariant as p } from "../../../../internal/Button/types.js";
import { useFetch as P } from "../../../../../hooks/useFetch.js";
import { useModalContext as K } from "../../../../internal/Modal/Modal.js";
import { PaymentLinkSummary as $ } from "../PaymentLinkSummary/PaymentLinkSummary.js";
import { PaymentLinkTabs as z } from "../PaymentLinkTabs/PaymentLinkTabs.js";
import { PaymentLinkExpiration as G } from "../PaymentLinkExpiration/PaymentLinkExpiration.js";
/* empty css                        */
import { PaymentLinkSkeleton as H } from "../PaymentLinkSkeleton/PaymentLinkSkeleton.js";
import { PaymentLinkError as Q } from "../PaymentLinkError/PaymentLinkError.js";
import w from "../../../../internal/Typography/Typography.js";
import { TypographyVariant as O } from "../../../../internal/Typography/types.js";
import U from "../../../../../core/Context/preact/useCoreContext.js";
import { Header as W } from "../../../../internal/Header/Header.js";
import { Icon as R } from "../../../../internal/Icon/Icon.js";
import { Fragment as X } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/dist/preact.module.js";
import { useConfigContext as Z } from "../../../../../core/ConfigContext/preact/context.js";
import { EMPTY_OBJECT as tt } from "../../../../../utils/value/constants.js";
const d = {
  root: "adyen-pe-payment-link-details",
  content: "adyen-pe-payment-link-details__content",
  expirationSuccessContainer: "adyen-pe-payment-link-details__expiration-success-container",
  expirationSuccessIcon: "adyen-pe-payment-link-details__expiration-success-icon"
}, St = ({ id: y, onUpdate: k, hideTitle: Y, onContactSupport: g, onDismiss: a, isDismissButtonHidden: x }) => {
  const { i18n: i, getCdnDataset: f } = U(), { getPayByLinkPaymentLinkById: u } = Z().endpoints, {
    data: t,
    isFetching: _,
    error: h,
    refetch: L
  } = P(
    T(
      () => ({
        fetchOptions: {
          enabled: !!y && !!u
        },
        queryFn: async () => u(tt, {
          path: {
            paymentLinkId: y
          }
        })
      }),
      [u, y]
    )
  ), { data: I, isFetching: M } = P({
    queryFn: r(async () => {
      const e = `${i.locale ?? "en-US"}`;
      return f ? await f({
        name: e,
        extension: "json",
        subFolder: "countries",
        fallback: []
      }) ?? [] : [];
    }, [f, i.locale])
  }), l = r(
    (e) => I?.find((q) => q.id === e)?.name || e,
    [I]
  ), C = _ || M, o = T(
    () => t && {
      ...t,
      ...t.shopperInformation ? {
        shopperInformation: {
          ...t?.shopperInformation,
          ...t?.shopperInformation?.shopperCountry ? {
            shopperCountry: t?.shopperInformation?.shopperCountry && l(t?.shopperInformation.shopperCountry)
          } : {},
          ...t?.shopperInformation?.billingAddress ? {
            billingAddress: t?.shopperInformation?.billingAddress && {
              ...t?.shopperInformation.billingAddress,
              country: t?.shopperInformation?.billingAddress?.country && l(t?.shopperInformation.billingAddress.country)
            }
          } : {},
          ...t?.shopperInformation?.shippingAddress ? {
            shippingAddress: t?.shopperInformation?.shippingAddress && {
              ...t?.shopperInformation.shippingAddress,
              country: t?.shopperInformation?.shippingAddress?.country && l(t?.shopperInformation.shippingAddress.country)
            }
          } : {}
        }
      } : {}
    },
    [t, l]
  ), [s, m] = b("details"), [c, S] = b(!1);
  J(() => {
    let e;
    return c && (e = setTimeout(() => S(!1), 3e3)), () => clearTimeout(e);
  }, [c]);
  const A = r(async () => {
    if (o)
      try {
        await navigator.clipboard.writeText(o.linkInformation.paymentLink), S(!0);
      } catch (e) {
        console.error("Failed to copy link:", e);
      }
  }, [o]), v = r(() => {
    m("expirationConfirmation");
  }, []), B = r(() => {
    m("expirationSuccess"), k?.();
  }, [k]), E = r(() => {
    m("details"), L();
  }, [L]), N = r(() => {
    a?.();
  }, [a]), { withinModal: D } = K(), V = r(() => {
    if (C)
      return /* @__PURE__ */ n(H, {});
    if (!o || h)
      return /* @__PURE__ */ n(
        Q,
        {
          error: h,
          onDismiss: a,
          onContactSupport: g
        }
      );
    if (s === "expirationConfirmation")
      return /* @__PURE__ */ n(
        G,
        {
          paymentLink: o,
          onCancel: () => m("details"),
          onExpirationSuccess: B
        }
      );
    if (s === "expirationSuccess") {
      const e = [
        ...a ? [
          {
            title: i.get("payByLink.details.expirationSuccess.actions.goBackToList"),
            event: N,
            variant: p.SECONDARY
          }
        ] : [],
        {
          title: i.get("payByLink.details.expirationSuccess.actions.showDetails"),
          event: E,
          variant: p.SECONDARY
        }
      ];
      return /* @__PURE__ */ n("div", { className: d.expirationSuccessContainer, children: [
        /* @__PURE__ */ n(R, { name: "checkmark-circle-fill", className: d.expirationSuccessIcon }),
        /* @__PURE__ */ n(w, { variant: O.TITLE, children: i.get("payByLink.details.expirationSuccess.title") }),
        /* @__PURE__ */ n(w, { variant: O.BODY, children: i.get("payByLink.details.expirationSuccess.description") }),
        /* @__PURE__ */ n(F, { actions: e })
      ] });
    }
    if (s === "details") {
      const e = [
        {
          title: i.get(c ? "payByLink.details.actions.copied" : "payByLink.details.actions.copyLink"),
          event: A,
          variant: p.PRIMARY,
          iconLeft: /* @__PURE__ */ n(R, { name: c ? "checkmark" : "copy" })
        },
        ...o.linkInformation.status !== "expired" && o.linkInformation.status !== "completed" ? [
          {
            title: i.get("payByLink.details.actions.expire"),
            event: v,
            variant: p.SECONDARY
          }
        ] : [],
        ...!x && a ? [
          {
            title: i.get("payByLink.common.actions.goBack"),
            event: a,
            variant: p.SECONDARY
          }
        ] : []
      ];
      return /* @__PURE__ */ n(X, { children: [
        /* @__PURE__ */ n($, { paymentLink: o }),
        /* @__PURE__ */ n(z, { paymentLink: o }),
        /* @__PURE__ */ n(F, { actions: e })
      ] });
    }
  }, [
    C,
    o,
    h,
    s,
    a,
    g,
    B,
    i,
    N,
    E,
    c,
    A,
    v,
    x
  ]);
  return /* @__PURE__ */ n("div", { className: d.root, children: [
    /* @__PURE__ */ n("div", { className: j({ "adyen-pe-visually-hidden": s !== "details" }), children: /* @__PURE__ */ n(W, { hideTitle: Y, forwardedToRoot: !D, titleKey: "payByLink.details.title" }) }),
    /* @__PURE__ */ n("div", { className: d.content, children: V() })
  ] });
};
export {
  St as PaymentLinkDetails
};
