import { jsx as r } from "../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { useCallback as G, useMemo as T } from "../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import V from "../../../../hooks/useTimezoneAwareDateFormatting.js";
import { useResponsiveContainer as g, containerQueries as k } from "../../../../hooks/useResponsiveContainer.js";
import { useTableColumns as W } from "../../../../hooks/useTableColumns.js";
import { BASE_TABLE_GRID_CLASS as q, PAYMENT_LINK_STATUSES as N, MOBILE_TABLE_CELL_CLASS as S, MOBILE_EXPIRE_DATE_CELL_CLASS as X, MOBILE_AMOUNT_CELL_CLASS as $ } from "./constants.js";
import { Tag as v } from "../../../internal/Tag/Tag.js";
import { TagVariant as m } from "../../../internal/Tag/types.js";
import o from "../../../internal/Typography/Typography.js";
import { TypographyVariant as a, TypographyElement as s } from "../../../internal/Typography/types.js";
import { DAY_MS as j } from "../../../internal/Calendar/calendar/constants.js";
import { Tooltip as z } from "../../../internal/Tooltip/Tooltip.js";
import { isActionNeededUrgently as Q } from "../../../utils/payByLink/actionLevel.js";
import H from "classnames";
import { PaymentLinksErrors as J } from "./PaymentLinksErrors.js";
import { FRONTEND_REDACTED_DATA_MARKER as Z, BACKEND_REDACTED_DATA_MARKER as ee } from "../../../constants.js";
import { ACCOUNT_MISCONFIGURATION as re, WRONG_STORE_IDS as ie } from "../utils/getPaymentLinksErrorMessage.js";
import te from "../../../../core/Context/preact/useCoreContext.js";
import B from "../../../internal/DataGrid/DataGrid.js";
import ne from "../../../internal/Pagination/Pagination.js";
import { Fragment as C } from "../../../../external/.pnpm/preact@10.28.2/node_modules/preact/dist/preact.module.js";
import { DATE_FORMAT_RESPONSE_DEADLINE as oe, DATE_FORMAT_PAYMENT_LINKS_OVERVIEW_EXPIRATION_DATE as A, DATE_FORMAT_PAYMENT_LINKS_OVERVIEW as ae } from "../../../../constants/dateFormats.js";
const I = (c) => {
  switch (c) {
    case "completed":
      return m.SUCCESS;
    case "expired":
      return m.DEFAULT;
    case "paymentPending":
      return m.WARNING;
    case "active":
      return m.BLUE;
    default:
      return m.DEFAULT;
  }
}, se = [
  "paymentLinkId",
  "merchantReference",
  "storeCode",
  "currency",
  "amount",
  "status",
  "expirationDate",
  "creationDate",
  "linkType",
  "shopperEmail"
], R = {
  paymentLinkId: "payByLink.overview.list.fields.id",
  amount: "payByLink.overview.list.fields.amount",
  currency: "payByLink.overview.list.fields.currency",
  status: "payByLink.overview.list.fields.status",
  expirationDate: "payByLink.overview.list.fields.expirationDate",
  creationDate: "payByLink.overview.list.fields.createdAt",
  linkType: "payByLink.overview.list.fields.linkType",
  merchantReference: "payByLink.overview.list.fields.merchantReference",
  shopperEmail: "payByLink.overview.list.fields.shopperEmail",
  storeCode: "payByLink.overview.list.fields.store"
}, le = "payByLink.overview.errors.couldNotLoadLinks", we = ({
  error: c,
  loading: w,
  onContactSupport: L,
  onRowClick: O,
  showDetails: me,
  showPagination: M,
  paymentLinks: b,
  stores: d,
  allStores: p,
  storeError: f,
  ...P
}) => {
  const { i18n: t, getImageAsset: D } = te(), { dateFormat: l } = V(), x = g(k.up.sm), F = G(
    (e) => {
      if (!e) return "";
      const y = new Date(e).getTime() - Date.now(), _ = Math.ceil(y / j), h = l(e, { ...oe, weekday: void 0 });
      return _ <= 1 ? t.get("payByLink.overview.common.actionNeeded.expiresToday", { values: { date: h } }) : t.get("payByLink.overview.common.actionNeeded.expiresDays", { values: { days: _, date: h } });
    },
    [l, t]
  ), i = g(k.down.xs), U = W({
    fields: se,
    fieldsKeys: R,
    columnConfig: {
      amount: {
        position: "right",
        flex: x ? 1.5 : void 0
      },
      linkType: {
        label: t.get(R.linkType),
        visible: !i
      },
      storeCode: {
        visible: d && d?.length > 1 && !i
      },
      merchantReference: {
        visible: !i
      },
      currency: {
        visible: !i
      },
      status: {
        visible: !i
      },
      expirationDate: {
        visible: !i
      },
      creationDate: {
        visible: !i
      },
      shopperEmail: {
        visible: !i
      }
    }
  }), Y = {
    title: "payByLink.overview.errors.listEmpty",
    message: ["payByLink.overview.errors.listEmpty.message"]
  }, u = T(() => {
    if (!(p?.length !== 0 || f))
      return {
        message: "No stores configured",
        name: "Account misconfiguration",
        errorCode: re,
        type: "error",
        requestId: ""
      };
  }, [p, f]), E = T(() => {
    if (!(p && p?.length > 0 && d?.length !== 0))
      return {
        errorCode: ie,
        type: "error",
        requestId: ""
      };
  }, [p, d]), K = T(
    () => () => /* @__PURE__ */ r(
      J,
      {
        getImageAsset: D,
        error: u || c || E,
        onContactSupport: L,
        errorMessage: le
      }
    ),
    [c, D, L, u, E]
  );
  return /* @__PURE__ */ r("div", { className: q, children: /* @__PURE__ */ r(
    B,
    {
      narrowColumns: i,
      errorDisplay: K,
      error: u || c || E,
      columns: U,
      data: b,
      loading: w,
      outline: !1,
      onRowClick: { callback: O },
      emptyTableMessage: Y,
      customCells: {
        currency: ({ item: e }) => {
          if (e?.amount?.currency)
            return /* @__PURE__ */ r(v, { label: `${e.amount.currency}`, variant: m.DEFAULT });
        },
        amount: ({ value: e, item: n }) => {
          const y = t.amount(e.value, e.currency, { hideCurrency: !0 });
          return i ? /* @__PURE__ */ r("div", { className: H(S, $), children: [
            /* @__PURE__ */ r(o, { el: s.SPAN, variant: a.BODY, strongest: !0, children: y }),
            /* @__PURE__ */ r("span", { children: n.status && /* @__PURE__ */ r(v, { label: t.get(N[n.status]), variant: I(n.status) }) })
          ] }) : /* @__PURE__ */ r(o, { el: s.SPAN, variant: a.BODY, children: y });
        },
        status: ({ value: e }) => {
          if (e)
            return /* @__PURE__ */ r(v, { label: t.get(`${N[e]}`), variant: I(e) });
        },
        linkType: ({ item: e }) => {
          if (!e?.linkType) return null;
          const n = e?.linkType === "open" ? "payByLink.common.linkType.open" : "payByLink.common.linkType.singleUse";
          return /* @__PURE__ */ r(o, { el: s.SPAN, variant: a.BODY, children: t.get(n) });
        },
        creationDate: ({ value: e }) => /* @__PURE__ */ r("time", { dateTime: e, children: /* @__PURE__ */ r(o, { el: s.SPAN, variant: a.BODY, children: l(e, ae) }) }),
        expirationDate: ({ value: e }) => Q(e) ? /* @__PURE__ */ r(z, { content: F(e), children: /* @__PURE__ */ r("span", { children: /* @__PURE__ */ r("time", { dateTime: e, children: l(e, A) }) }) }) : /* @__PURE__ */ r("time", { dateTime: e, children: /* @__PURE__ */ r(o, { el: s.SPAN, variant: a.BODY, children: l(e, A) }) }),
        paymentLinkId: ({ item: e }) => i ? /* @__PURE__ */ r("div", { className: S, children: [
          /* @__PURE__ */ r(o, { strongest: !0, el: s.SPAN, variant: a.BODY, children: e.paymentLinkId }),
          /* @__PURE__ */ r("time", { dateTime: e.expirationDate, children: /* @__PURE__ */ r(
            o,
            {
              className: X,
              el: s.SPAN,
              variant: a.CAPTION,
              children: t.get("payByLink.overview.common.actionNeeded.expiresAt", {
                values: { date: l(e.expirationDate, A) }
              })
            }
          ) })
        ] }) : /* @__PURE__ */ r(C, { children: e.paymentLinkId }),
        shopperEmail: ({ item: e }) => /* @__PURE__ */ r(C, { children: e.shopperEmail === ee ? Z : e.shopperEmail })
      },
      children: M && /* @__PURE__ */ r(B.Footer, { children: /* @__PURE__ */ r(
        ne,
        {
          ...P,
          ariaLabelKey: "payByLink.overview.pagination.label",
          limitSelectAriaLabelKey: "payByLink.overview.pagination.controls.limitSelect.label"
        }
      ) })
    }
  ) });
};
export {
  se as PAYMENT_LINKS_TABLE_FIELDS,
  we as PaymentLinksTable
};
