import { jsx as e } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import h from "classnames";
import { useState as V, useCallback as $, useEffect as q, useMemo as j } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import W from "../../../../../hooks/useTimezoneAwareDateFormatting.js";
import X from "../../../../internal/Button/DownloadButton/DownloadButton.js";
import A from "../../../../internal/CopyText/CopyText.js";
import Y from "../../../../internal/DataGrid/components/Icon.js";
import { isCustomDataObject as g } from "../../../../internal/DataGrid/components/TableCells.js";
import G from "../../../../internal/Link/Link.js";
import { Tag as H } from "../../../../internal/Tag/Tag.js";
import { TypographyElement as Z, TypographyVariant as I } from "../../../../internal/Typography/types.js";
import O from "../../../../internal/Typography/Typography.js";
import { isDisputeActionNeeded as J } from "../../../../utils/disputes/actionNeeded.js";
import { DISPUTE_DETAILS_RESERVED_FIELDS_SET as P, DISPUTE_DATA_LIST_EVIDENCE as Q, DISPUTE_DATA_LIST_EVIDENCE_ERROR_MESSAGE as z, DISPUTE_DATA_LIST as ee, DISPUTE_DATA_LABEL as te } from "./constants.js";
import { getDisputeReason as ne } from "../../../../utils/translation/getters.js";
import { getDefenseReasonContent as se, getDefenseDocumentContent as ae } from "../../utils/index.js";
import { useDisputeFlow as ie } from "../../context/dispute/context.js";
import de from "../../../../../core/Context/preact/useCoreContext.js";
import { DATE_FORMAT_DISPUTE_DETAILS as f } from "../../../../../constants/dateFormats.js";
import { Icon as re } from "../../../../internal/Icon/Icon.js";
import { Fragment as oe } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/dist/preact.module.js";
import le from "../../../../internal/StructuredList/StructuredList.js";
const s = {
  acceptedOn: "disputes.management.details.fields.acceptedOn",
  account: "disputes.management.details.fields.account",
  defendedOn: "disputes.management.details.fields.defendedOn",
  defenseReason: "disputes.management.details.fields.defenseReason",
  disputeEvidence: "disputes.management.details.fields.evidence",
  disputeReason: "disputes.management.details.fields.disputeReason",
  disputeReference: "disputes.management.details.fields.disputeReference",
  expiredOn: "disputes.management.details.fields.expiredOn",
  merchantReference: "disputes.management.details.fields.merchantReference",
  openedOn: "disputes.management.details.fields.openedOn",
  paymentReference: "disputes.management.details.fields.paymentReference",
  reasonCode: "disputes.management.details.fields.reasonCode",
  respondBy: "disputes.management.details.fields.respondBy"
}, me = ["ACCEPTED", "EXPIRED"], Le = ({ dispute: a, dataCustomization: c, defenseReasonConfig: _ }) => {
  const { i18n: o } = de(), { dateFormat: l } = W(a.payment.balanceAccount.timeZone), { defenseDocumentConfig: S } = ie(), [k, b] = V(), v = $(async () => {
    if (a) {
      const u = await c?.details?.onDataRetrieve?.(a);
      b(
        c?.details?.fields.reduce((m, i) => P.has(i.key) || i?.visibility === "hidden" ? m : { ...m, ...u?.[i.key] ? { [i.key]: u[i.key] } : {} }, {})
      );
    }
  }, [a, c]);
  return q(() => {
    v();
  }, [v]), j(() => {
    const { pspReference: u, reason: m, acceptedDate: i, createdAt: C, dueDate: p, status: D, type: L } = a.dispute, { pspReference: F, merchantReference: N, balanceAccount: B } = a.payment, { reason: E, defendedOn: y, suppliedDocuments: R } = a.defense || {}, x = L === "NOTIFICATION_OF_FRAUD", w = D === "EXPIRED" || D === "LOST" && !x && !y, U = J(a.dispute) && a.dispute.defensibility !== "NOT_ACTIONABLE", r = null, M = [
      ...[
        // dispute reason
        {
          key: s.disputeReason,
          value: `${ne(o, m.category)} - ${m.title}`,
          // [NOTE]: Not translated at the moment (maybe in the future)
          id: "disputeReason"
        },
        // reason code
        x ? r : {
          key: s.reasonCode,
          value: m.code,
          id: "reasonCode"
        },
        // created at
        {
          key: s.openedOn,
          value: /* @__PURE__ */ e("time", { dateTime: C, children: l(C, f) }),
          id: "openedOn"
        },
        // respond by
        p && U ? {
          key: s.respondBy,
          value: /* @__PURE__ */ e("time", { dateTime: p, children: l(p, f) }),
          id: "respondBy"
        } : r,
        // dispute reference
        {
          key: s.disputeReference,
          value: /* @__PURE__ */ e(
            A,
            {
              copyButtonAriaLabelKey: "disputes.management.details.actions.copyDisputeReference",
              type: "Default",
              textToCopy: u,
              showCopyTextTooltip: !1
            }
          ),
          id: "disputeId"
        },
        // balance account
        {
          key: s.account,
          value: B.description,
          id: "account"
        },
        // psp reference
        {
          key: s.paymentReference,
          value: /* @__PURE__ */ e(
            A,
            {
              copyButtonAriaLabelKey: "disputes.management.details.actions.copyPaymentReference",
              type: "Default",
              textToCopy: F,
              showCopyTextTooltip: !1
            }
          ),
          id: "paymentPspReference"
        },
        // merchant reference
        N ? {
          key: s.merchantReference,
          value: /* @__PURE__ */ e(
            A,
            {
              copyButtonAriaLabelKey: "disputes.management.details.actions.copyMerchantReference",
              type: "Default",
              textToCopy: N,
              showCopyTextTooltip: !1
            }
          ),
          id: "paymentMerchantReference"
        } : r,
        // defense reason
        E ? {
          key: s.defenseReason,
          value: se(_, o, E)?.title ?? E,
          id: "defenseReason"
        } : r,
        // defended on
        y ? {
          key: s.defendedOn,
          value: /* @__PURE__ */ e("time", { dateTime: y, children: l(y, f) }),
          id: "defendedOn"
        } : r,
        // evidence
        R && R.length > 0 ? {
          key: s.disputeEvidence,
          value: /* @__PURE__ */ e(oe, { children: R.map((t, n) => {
            const T = {
              path: { disputePspReference: u },
              query: { documentType: t }
            };
            return /* @__PURE__ */ e("div", { className: Q, children: [
              /* @__PURE__ */ e(H, { label: ae(S, o, t)?.title ?? t }),
              /* @__PURE__ */ e(
                X,
                {
                  className: "adyen-pe-dispute-document-download",
                  endpointName: "downloadDefenseDocument",
                  disabled: !1,
                  requestParams: T,
                  iconButton: !0,
                  errorMessage: () => /* @__PURE__ */ e("div", { className: z, children: [
                    /* @__PURE__ */ e(re, { name: "info-filled" }),
                    /* @__PURE__ */ e(O, { variant: I.CAPTION, el: Z.SPAN, children: o.get("disputes.management.details.errors.downloadFailure") })
                  ] }),
                  setError: () => console.warn("Download failed for", t),
                  "aria-label": o.get("disputes.management.details.actions.downloadEvidence")
                }
              )
            ] }, `${t}-${n}`);
          }) }),
          id: "disputeEvidence"
        } : r,
        // accepted on
        i && me.includes(D) ? {
          key: s.acceptedOn,
          value: /* @__PURE__ */ e("time", { dateTime: i, children: l(i, f) }),
          id: "acceptedOn"
        } : r,
        // expired on
        p && w ? {
          key: s.expiredOn,
          value: /* @__PURE__ */ e("time", { dateTime: p, children: l(p, f) }),
          id: "expiredOn"
        } : r
      ].filter(Boolean).filter((t) => !c?.details?.fields?.some((n) => n.key === t.id && n.visibility === "hidden")),
      ...Object.entries(k || {}).filter(
        ([t, n]) => !P.has(t) && n.type !== "button" && n.visibility !== "hidden"
      ).map(([t, n]) => ({
        key: t,
        value: g(n) ? n.value : n,
        type: g(n) ? n.type : "text",
        config: g(n) ? n.config : void 0
      })) || {}
    ];
    return /* @__PURE__ */ e(
      le,
      {
        classNames: ee,
        items: M,
        layout: "4-8",
        align: "start",
        renderLabel: (t) => /* @__PURE__ */ e("div", { className: te, children: t }),
        renderValue: (t, n, T, d) => {
          if (T === "link" && d)
            return /* @__PURE__ */ e(G, { classNames: [h(d?.className)], href: d.href, target: d.target || "_blank", children: t });
          if (T === "icon" && d) {
            const K = { url: d?.src, alt: d.alt || t };
            return /* @__PURE__ */ e("div", { className: h("adyen-pe-dispute-data__list-icon-value", d?.className), children: [
              /* @__PURE__ */ e(Y, { ...K }),
              /* @__PURE__ */ e(O, { variant: I.BODY, children: t })
            ] });
          }
          return /* @__PURE__ */ e(O, { className: h(d?.className), variant: I.BODY, children: t });
        }
      }
    );
  }, [
    a.dispute,
    a.payment,
    a.defense,
    o,
    l,
    _,
    k,
    S,
    c?.details?.fields
  ]);
};
export {
  Le as default
};
