import { jsx as e } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import Y from "classnames";
import { useMemo as T, useCallback as _, useState as re, useEffect as ce } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { useFetch as pe } from "../../../../../hooks/useFetch.js";
import { useResponsiveContainer as de, containerQueries as me } from "../../../../../hooks/useResponsiveContainer.js";
/* empty css                 */
import { Alert as N } from "../../../../internal/Alert/Alert.js";
import { AlertVariantOption as S, AlertTypeOption as O } from "../../../../internal/Alert/types.js";
import ue from "../../../../internal/Button/ButtonActions/ButtonActions.js";
import { ButtonVariant as I } from "../../../../internal/Button/types.js";
import le from "../../../../internal/StatusBox/StatusBox.js";
import Te from "../../../../internal/StatusBox/useStatusBox.js";
import { Tag as Ae } from "../../../../internal/Tag/Tag.js";
import fe from "../../../DisputesOverview/components/DisputesTable/DisputeStatusTag.js";
import { useDisputeFlow as Ee } from "../../context/dispute/context.js";
import { isDisputeActionNeeded as De } from "../../../../utils/disputes/actionNeeded.js";
import { DisputeIssuerComments as _e } from "./DisputeIssuerComments.js";
import Ne from "./DisputeDataProperties.js";
import { DISPUTE_DATA_SKELETON_CONTAINER as Se, DISPUTE_DATA_STATUS_BOX_SKELETON as Oe, DISPUTE_DATA_STATUS_BOX_STATUS_CONTAINER as Ie, DISPUTE_DATA_STATUS_BOX_STATUS as K, DISPUTE_DATA_STATUS_BOX_AMOUNT as ge, DISPUTE_DATA_STATUS_BOX_PAYMENT_METHOD_CONTAINER as he, DISPUTE_DATA_STATUS_BOX_PAYMENT_METHOD as G, DISPUTE_DATA_PROPERTIES_SKELETON_CONTAINER as Re, DISPUTE_DATA_PROPERTIES_SKELETON as Pe, DISPUTE_DATA_PROPERTIES_SKELETON_ELEMENT as V, DISPUTE_DATA_CLASS as j, DISPUTE_DATA_MOBILE_CLASS as q, DISPUTE_DATA_ERROR_CONTAINER as ye, DISPUTE_STATUS_BOX as Be, DISPUTE_DATA_ACTION_BAR as Ce } from "./constants.js";
import Fe from "../../../../internal/Typography/Typography.js";
import { TypographyElement as Ue, TypographyVariant as ve } from "../../../../internal/Typography/types.js";
import Le from "../../../../../hooks/useTimezoneAwareDateFormatting.js";
import { getDisputeType as be } from "../../../../utils/translation/getters.js";
import { ErrorMessageDisplay as Me } from "../../../../internal/ErrorMessageDisplay/ErrorMessageDisplay.js";
import { getDisputesErrorMessage as ke } from "../../../../utils/disputes/getDisputesErrorMessage.js";
import Q from "../../../../../core/Context/preact/useCoreContext.js";
import we from "../../../../internal/Button/Button.js";
import { Fragment as g } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/dist/preact.module.js";
import { Translation as xe } from "../../../../internal/Translation/Translation.js";
import { useConfigContext as He } from "../../../../../core/ConfigContext/preact/context.js";
import { EMPTY_OBJECT as Xe } from "../../../../../utils/value/constants.js";
import { isFunction as y } from "../../../../../utils/value/is.js";
import { DATE_FORMAT_RESPONSE_DEADLINE as Ye } from "../../../../../constants/dateFormats.js";
const Ke = (o) => !!o && typeof o == "object" && o.type === "button", Ge = ({
  alertMode: o,
  dispute: d,
  timeZone: m
}) => {
  const { i18n: r } = Q(), { dateFormat: n } = Le(m);
  switch (o) {
    case "contactSupport": {
      const { dueDate: a, type: u } = d, l = u === "REQUEST_FOR_INFORMATION" ? "disputes.management.details.alerts.contactSupport.requestForInformation" : u === "NOTIFICATION_OF_FRAUD" ? "disputes.management.details.alerts.contactSupport.notificationOfFraud" : "disputes.management.details.alerts.contactSupport.chargeback";
      return /* @__PURE__ */ e(
        N,
        {
          type: O.WARNING,
          variant: S.TIP,
          description: /* @__PURE__ */ e(g, { children: [
            r.get(l),
            u !== "NOTIFICATION_OF_FRAUD" && !!a && /* @__PURE__ */ e(g, { children: [
              " ",
              /* @__PURE__ */ e(
                xe,
                {
                  translationKey: "disputes.management.details.alerts.responseDeadline",
                  fills: {
                    date: /* @__PURE__ */ e("time", { dateTime: a, children: /* @__PURE__ */ e(Fe, { variant: ve.CAPTION, el: Ue.SPAN, stronger: !0, children: n(a, Ye) }) })
                  }
                }
              )
            ] })
          ] })
        }
      );
    }
    case "autoDefended":
      return /* @__PURE__ */ e(
        N,
        {
          type: O.HIGHLIGHT,
          variant: S.TIP,
          description: r.get("disputes.management.details.alerts.autoDefended")
        }
      );
    case "notDefended": {
      const a = d.status === "EXPIRED" ? "disputes.management.details.alerts.notDefendedExpired" : "disputes.management.details.alerts.notDefendedLost";
      return /* @__PURE__ */ e(N, { type: O.HIGHLIGHT, variant: S.TIP, description: r.get(a) });
    }
    case "notDefendable":
      return /* @__PURE__ */ e(
        N,
        {
          type: O.HIGHLIGHT,
          variant: S.TIP,
          description: r.get("disputes.management.details.alerts.notDefendable")
        }
      );
  }
  return null;
}, ht = ({
  disputeId: o,
  dataCustomization: d,
  onContactSupport: m,
  onDismiss: r
}) => {
  const { i18n: n } = Q(), { dispute: a, setDispute: u, setFlowState: l, defenseReasonConfig: Z } = Ee(), { getDisputeDetail: h, getApplicableDefenseDocuments: z, acceptDispute: J } = He().endpoints, W = y(J), $ = y(z), B = de(me.up.sm), { data: A, isFetching: ee, error: R } = pe(
    T(
      () => ({
        fetchOptions: {
          enabled: !!o && !!h && !a,
          onSuccess: ((s) => {
            u(s);
          })
        },
        queryFn: async () => h(Xe, {
          path: {
            disputePspReference: o
          }
        })
      }),
      [a, o, h, u]
    )
  ), t = a || A, c = t?.dispute?.defensibility, te = Te({
    amountData: t?.dispute.amount,
    paymentMethodData: t?.payment.paymentMethod
  }), C = T(() => {
    const { chargeback: s, preArbitration: i } = t?.dispute.issuerExtraData ?? {}, D = [];
    return [i, s].forEach((H) => {
      H && ["LIABILITY_NOT_ACCEPTED_FULLY", "PRE_ARB_REASON", "NOTE"].forEach((ie) => {
        const X = H[ie]?.trim();
        X && D.push(X);
      });
    }), D.filter(Boolean);
  }, [t]), F = T(() => be(n, t?.dispute.type), [n, t]), f = !!c && ["ACCEPTABLE", "DEFENDABLE_EXTERNALLY"].includes(c) || t?.dispute.type === "NOTIFICATION_OF_FRAUD", U = !!c && c === "DEFENDABLE" && $, v = !!c && ["ACCEPTABLE", "DEFENDABLE"].includes(c) && W, L = _(() => {
    l("accept");
  }, [l]), b = _(() => {
    l("defendReasonSelectionView");
  }, [l]), [E, se] = re([]), M = _(async () => {
    const s = A && await d?.details?.onDataRetrieve?.(A);
    if (s)
      return se(
        Object.values(s).filter((i) => Ke(i))
      );
  }, [A, d?.details]);
  ce(() => {
    M();
  }, [M]);
  const k = T(() => a?.dispute.type === "REQUEST_FOR_INFORMATION" ? n.get("disputes.management.details.actions.submitInformation") : n.get("disputes.management.details.actions.defendChargeback"), [n, a?.dispute.type]), w = T(() => {
    const s = [];
    return U && s.push({
      title: k,
      event: b
    }), v && s.push({
      title: n.get("disputes.management.details.actions.accept"),
      event: L,
      variant: I.SECONDARY
    }), f && y(m) && s.push({
      title: n.get("disputes.management.details.actions.contactSupport"),
      event: m,
      variant: I.SECONDARY
    }), E && E.length && E.forEach((i) => {
      s.push({
        title: i?.value,
        event: i?.config?.action,
        variant: I.SECONDARY,
        classNames: i?.config?.className ? [i?.config?.className] : []
      });
    }), s;
  }, [U, k, b, v, f, m, E, n, L]), x = T(() => !!t && De(t.dispute), [t]), ne = _(() => /* @__PURE__ */ e(we, { variant: I.SECONDARY, onClick: r, children: n.get("disputes.management.common.actions.goBack") }), [n, r]);
  if (!t && !R || ee) {
    const s = Array.from({ length: 5 });
    return /* @__PURE__ */ e("div", { className: Y(j, { [q]: !B }), children: /* @__PURE__ */ e("div", { className: Se, children: [
      /* @__PURE__ */ e("div", { className: Oe, children: [
        /* @__PURE__ */ e("div", { className: Ie, children: [
          /* @__PURE__ */ e("span", { className: K }),
          /* @__PURE__ */ e("span", { className: K })
        ] }),
        /* @__PURE__ */ e("span", { className: ge }),
        /* @__PURE__ */ e("div", { className: he, children: [
          /* @__PURE__ */ e("span", { className: G }),
          /* @__PURE__ */ e("span", { className: G })
        ] })
      ] }),
      /* @__PURE__ */ e("div", { className: Re, children: s.map((i, D) => /* @__PURE__ */ e("div", { className: Pe, children: [
        /* @__PURE__ */ e("span", { className: V }),
        /* @__PURE__ */ e("span", { className: V })
      ] }, `skeleton-${D}`)) })
    ] }) });
  }
  const P = t?.dispute.type === "NOTIFICATION_OF_FRAUD", ae = !!(t?.defense && t.defense.defendedOn);
  let p;
  t?.defense?.autodefended === !0 ? p = "autoDefended" : x && c === "NOT_ACTIONABLE" ? p = "notDefendable" : x && f || f && P ? p = "contactSupport" : (t?.dispute.status === "EXPIRED" || t?.dispute.status === "LOST" && !(P || ae)) && (p = "notDefended");
  const oe = ke(
    R,
    "disputes.management.common.errors.unavailable",
    m
  );
  return /* @__PURE__ */ e("div", { className: Y(j, { [q]: !B }), children: R ? /* @__PURE__ */ e("div", { className: ye, children: /* @__PURE__ */ e(
    Me,
    {
      renderSecondaryButton: r ? ne : void 0,
      withImage: !0,
      outlined: !1,
      absolutePosition: !1,
      withBackground: !1,
      ...oe
    }
  ) }) : t ? /* @__PURE__ */ e(g, { children: [
    /* @__PURE__ */ e("div", { className: Be, children: /* @__PURE__ */ e(
      le,
      {
        ...te,
        tag: /* @__PURE__ */ e(g, { children: [
          F && /* @__PURE__ */ e(Ae, { label: F }),
          !P && /* @__PURE__ */ e(fe, { dispute: t.dispute })
        ] })
      }
    ) }),
    C.length > 0 && /* @__PURE__ */ e(_e, { issuerComments: C }),
    p && /* @__PURE__ */ e(
      Ge,
      {
        alertMode: p,
        dispute: t.dispute,
        timeZone: t.payment.balanceAccount?.timeZone
      }
    ),
    /* @__PURE__ */ e(Ne, { dispute: t, dataCustomization: d, defenseReasonConfig: Z }),
    w.length > 0 ? /* @__PURE__ */ e("div", { className: Ce, children: /* @__PURE__ */ e(ue, { actions: w }) }) : null
  ] }) : null });
};
export {
  ht as DisputeData,
  ht as default
};
