import { jsx as t } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import l from "classnames";
import { useState as G, useMemo as h, useCallback as w, useEffect as $ } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import V from "../../../../../hooks/useTimezoneAwareDateFormatting.js";
import { Alert as H } from "../../../../internal/Alert/Alert.js";
import { AlertTypeOption as j } from "../../../../internal/Alert/types.js";
import { DAY_MS as q } from "../../../../internal/Calendar/calendar/constants.js";
import { getDisputeReason as z } from "../../../../utils/translation/getters.js";
import { isDisputeActionNeededUrgently as Q } from "../../../../utils/disputes/actionNeeded.js";
import W from "../../../../internal/DataOverviewError/DataOverviewError.js";
import { TypographyVariant as v, TypographyElement as A } from "../../../../internal/Typography/types.js";
import y from "../../../../internal/Typography/Typography.js";
import { BASE_CLASS as r } from "./constants.js";
import { useTableColumns as Z } from "../../../../../hooks/useTableColumns.js";
import { useResponsiveContainer as J, containerQueries as X } from "../../../../../hooks/useResponsiveContainer.js";
import f from "../../../TransactionsOverview/components/TransactionsTable/PaymentMethodCell.js";
import ee from "./DisputeStatusTag.js";
import { Tag as te } from "../../../../internal/Tag/Tag.js";
import { Tooltip as oe } from "../../../../internal/Tooltip/Tooltip.js";
/* empty css                   */
import ne from "../../../../../core/Context/preact/useCoreContext.js";
import { DATE_FORMAT_RESPONSE_DEADLINE as se, DATE_FORMAT_DISPUTES as N } from "../../../../../constants/dateFormats.js";
import R from "../../../../internal/DataGrid/DataGrid.js";
import re from "../../../../internal/Pagination/Pagination.js";
import { Icon as ie } from "../../../../internal/Icon/Icon.js";
import { Translation as ae } from "../../../../internal/Translation/Translation.js";
import { useConfigContext as le } from "../../../../../core/ConfigContext/preact/context.js";
const O = {
  status: "disputes.overview.common.fields.status",
  respondBy: "disputes.overview.common.fields.respondBy",
  createdAt: "disputes.overview.common.fields.openedOn",
  paymentMethod: "disputes.overview.common.fields.paymentMethod",
  disputeReason: "disputes.overview.common.fields.disputeReason",
  reason: "disputes.overview.common.fields.reason",
  currency: "disputes.overview.common.fields.currency",
  disputedAmount: "disputes.overview.common.fields.disputedAmount",
  totalPaymentAmount: "disputes.overview.common.fields.totalPaymentAmount"
}, M = {
  CHARGEBACKS: { title: "disputes.overview.chargebacks.errors.listEmpty", message: "disputes.overview.chargebacks.errors.updateFilters" },
  FRAUD_ALERTS: { title: "disputes.overview.fraudAlerts.errors.listEmpty", message: "disputes.overview.fraudAlerts.errors.updateFilters" },
  ONGOING_AND_CLOSED: {
    title: "disputes.overview.ongoingAndClosed.errors.listEmpty",
    message: "disputes.overview.ongoingAndClosed.errors.updateFilters"
  }
}, de = Object.keys(O), n = {
  cellContent: `${r}__cell-content`,
  cellContentVStack: `${r}__cell-content--vstack`,
  cellTextGrey: `${r}__cell-text--grey`,
  dateContentUrgent: `${r}__date-content--urgent`,
  statusContent: `${r}__status-content`,
  statusContentUrgent: `${r}__status-content--urgent`
}, xe = ({
  error: D,
  loading: S,
  balanceAccountId: ce,
  onContactSupport: E,
  showPagination: T,
  onRowClick: L,
  data: F,
  customColumns: k,
  activeBalanceAccount: U,
  statusGroup: s,
  ...B
}) => {
  const { i18n: i } = ne(), { refreshing: _ } = le(), { dateFormat: d } = V(U?.timeZone), [b, P] = G(null), g = h(() => S || _, [S, _]), o = J(X.down.xs);
  let c;
  if (T)
    switch (s) {
      case "CHARGEBACKS":
        c = "disputes.overview.chargebacks.limitSelect.a11y.label";
        break;
      case "FRAUD_ALERTS":
        c = "disputes.overview.fraudAlerts.limitSelect.a11y.label";
        break;
      case "ONGOING_AND_CLOSED":
        c = "disputes.overview.ongoingAndClosed.limitSelect.a11y.label";
        break;
    }
  const x = Z({
    fields: de,
    fieldsKeys: O,
    customColumns: k,
    columnConfig: h(
      () => ({
        status: {
          visible: s === "ONGOING_AND_CLOSED"
        },
        reason: {
          visible: s === "FRAUD_ALERTS" && !o,
          flex: 2
        },
        respondBy: {
          visible: s === "CHARGEBACKS"
        },
        currency: {
          visible: !o,
          flex: 0.5
        },
        disputedAmount: {
          visible: s !== "FRAUD_ALERTS",
          position: "right"
        },
        createdAt: {
          visible: !o || s === "FRAUD_ALERTS"
        },
        paymentMethod: {
          visible: !o
        },
        disputeReason: {
          visible: s !== "FRAUD_ALERTS" && !o
        },
        totalPaymentAmount: {
          visible: s === "FRAUD_ALERTS",
          position: "right"
        }
      }),
      [o, s]
    )
  }), C = w(() => P(null), []), I = w(
    (e) => {
      if (!e) return "";
      const u = new Date(e).getTime() - Date.now(), p = Math.ceil(u / q), a = d(e, { ...se, weekday: void 0 });
      return p <= 1 ? i.get("disputes.overview.common.actionNeeded.respondToday", { values: { date: a } }) : i.get("disputes.overview.common.actionNeeded.respondDays", { values: { days: p, date: a } });
    },
    [d, i]
  ), K = {
    title: M[s].title,
    message: [M[s].message]
  }, Y = h(
    () => () => /* @__PURE__ */ t(W, { error: D, errorMessage: "disputes.overview.common.errors.listUnavailable", onContactSupport: E }),
    [D, E]
  );
  return $(() => {
    g && C();
  }, [g, C]), /* @__PURE__ */ t("div", { className: r, children: [
    b && /* @__PURE__ */ t(H, { onClose: C, type: j.WARNING, className: "adyen-pe-disputes-table-alert", ...b }),
    /* @__PURE__ */ t(
      R,
      {
        autoFitColumns: o,
        errorDisplay: Y,
        error: D,
        columns: x,
        data: F,
        loading: g,
        outline: !1,
        onRowClick: { callback: L },
        emptyTableMessage: K,
        customCells: {
          status: ({ item: e }) => /* @__PURE__ */ t("div", { className: l(n.cellContent, { [n.cellContentVStack]: o }), children: [
            /* @__PURE__ */ t(ee, { dispute: e }),
            o && /* @__PURE__ */ t(f, { paymentMethod: e.paymentMethod })
          ] }),
          reason: ({ item: e }) => e.reason.title,
          respondBy: ({ item: e }) => {
            const m = Q(e), u = d(e.dueDate, N), p = !0, a = () => m && p ? /* @__PURE__ */ t(oe, { content: I(e.dueDate), children: /* @__PURE__ */ t("span", { className: n.dateContentUrgent, children: [
              /* @__PURE__ */ t("time", { dateTime: e.dueDate, children: u }),
              /* @__PURE__ */ t(ie, { name: "warning-filled" })
            ] }) }) : /* @__PURE__ */ t("time", { dateTime: e.dueDate, children: u });
            return /* @__PURE__ */ t("div", { className: l(n.cellContent, { [n.cellContentVStack]: o }), children: [
              e.dueDate ? /* @__PURE__ */ t(
                y,
                {
                  el: A.SPAN,
                  variant: v.BODY,
                  className: l(n.statusContent, {
                    [n.cellTextGrey]: o && !m,
                    [n.statusContentUrgent]: m
                  }),
                  children: o ? /* @__PURE__ */ t(
                    ae,
                    {
                      translationKey: "disputes.overview.common.actionNeeded.dueDate",
                      fills: { dueDate: a }
                    }
                  ) : a()
                }
              ) : null,
              o && /* @__PURE__ */ t(f, { paymentMethod: e.paymentMethod })
            ] });
          },
          currency: ({ item: e }) => /* @__PURE__ */ t(te, { children: e.amount.currency }),
          disputedAmount: ({ item: e }) => e.amount && /* @__PURE__ */ t(y, { el: A.SPAN, variant: v.BODY, stronger: !0, children: i.amount(e.amount.value, e.amount.currency, { hideCurrency: !1 }) }),
          createdAt: ({ item: e }) => /* @__PURE__ */ t("div", { className: l(n.cellContent, { [n.cellContentVStack]: o }), children: [
            /* @__PURE__ */ t(
              "time",
              {
                dateTime: e.createdAt,
                className: l(n.statusContent, {
                  [n.cellTextGrey]: o
                }),
                children: /* @__PURE__ */ t(y, { el: A.SPAN, variant: v.BODY, children: d(e.createdAt, N) })
              }
            ),
            o && /* @__PURE__ */ t(f, { paymentMethod: e.paymentMethod })
          ] }),
          paymentMethod: ({ item: e }) => /* @__PURE__ */ t(f, { paymentMethod: e.paymentMethod }),
          disputeReason: ({ item: e }) => /* @__PURE__ */ t("span", { children: z(i, e.reason.category) }),
          totalPaymentAmount: ({ item: e }) => e && /* @__PURE__ */ t(y, { el: A.SPAN, variant: v.BODY, stronger: !0, children: i.amount(e.amount.value, e.amount.currency, { hideCurrency: !1 }) })
        },
        children: T && /* @__PURE__ */ t(R.Footer, { children: /* @__PURE__ */ t(
          re,
          {
            ...B,
            ariaLabelKey: "disputes.overview.common.pagination.a11y.label",
            limitSelectAriaLabelKey: c
          }
        ) })
      }
    )
  ] });
};
export {
  xe as DisputesTable,
  M as EMPTY_TABLE_MESSAGE_KEYS,
  de as FIELDS,
  O as FIELD_KEYS
};
