import { jsx as r } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { useState as M, useMemo as i, useCallback as Y } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import q from "../../../../../hooks/useFreezePeriod.js";
import x from "../../../../../hooks/useTimezoneAwareDateFormatting.js";
import { Alert as U } from "../../../../internal/Alert/Alert.js";
import { AlertTypeOption as z } from "../../../../internal/Alert/types.js";
import K from "../../../../internal/Button/DownloadButton/DownloadButton.js";
import k from "../../../../internal/DataOverviewError/DataOverviewError.js";
import { TypographyVariant as s, TypographyElement as a } from "../../../../internal/Typography/types.js";
import n from "../../../../internal/Typography/Typography.js";
import { useResponsiveContainer as f, containerQueries as u } from "../../../../../hooks/useResponsiveContainer.js";
import { BASE_CLASS as G, DATE_TYPE_CLASS as j, DATE_TYPE_DATE_SECTION_CLASS as Q, DISABLED_BUTTONS_TIMEOUT as V } from "./constants.js";
/* empty css                  */
import { useTableColumns as W } from "../../../../../hooks/useTableColumns.js";
import { getReportType as w } from "../../../../utils/translation/getters.js";
import X from "../../../../../core/Context/preact/useCoreContext.js";
import { Icon as H } from "../../../../internal/Icon/Icon.js";
import D from "../../../../internal/DataGrid/DataGrid.js";
import J from "../../../../internal/Pagination/Pagination.js";
import { DATE_FORMAT_REPORTS as E } from "../../../../../constants/dateFormats.js";
import { useConfigContext as Z } from "../../../../../core/ConfigContext/preact/context.js";
const $ = ["createdAt", "dateAndReportType", "reportType", "reportFile"], ee = {
  createdAt: "reports.overview.list.fields.createdAt",
  reportFile: "reports.overview.list.fields.reportFile",
  reportType: "reports.overview.list.fields.reportType"
}, Se = ({
  error: l,
  loading: p,
  balanceAccountId: S,
  onContactSupport: A,
  showPagination: g,
  data: b,
  customColumns: C,
  ..._
}) => {
  const { i18n: o } = X(), { dateFormat: v } = x("UTC"), { freeze: h, frozen: N } = q(V), [y, d] = M(null), { refreshing: T } = Z(), F = i(() => p || T, [p, T]), m = f(u.up.sm), c = f(u.down.xs), R = W({
    customColumns: C,
    fields: $,
    fieldsKeys: ee,
    columnConfig: i(
      () => ({
        dateAndReportType: { visible: c },
        createdAt: { visible: m },
        reportType: { visible: m },
        reportFile: { visible: !0, position: c ? "right" : void 0 }
      }),
      [m, c]
    )
  }), P = Y(() => {
    d(null);
  }, []), O = {
    title: "reports.overview.errors.listEmpty",
    message: ["common.errors.updateFilters"]
  }, L = i(
    () => () => /* @__PURE__ */ r(k, { error: l, errorMessage: "reports.overview.errors.listUnavailable", onContactSupport: A }),
    [l, A]
  ), B = i(() => /* @__PURE__ */ r(H, { name: "warning" }), []), I = i(
    () => (e) => {
      const t = {};
      switch (e?.errorCode) {
        case "999_429_001":
          t.title = o.get("reports.overview.errors.download"), t.description = o.get("reports.overview.errors.tooManyDownloads");
          break;
        case "00_500":
        default:
          t.title = o.get("reports.overview.errors.download"), t.description = o.get("reports.overview.errors.retryDownload");
          break;
      }
      d(t);
    },
    [o]
  );
  return p && d(null), /* @__PURE__ */ r("div", { className: G, children: [
    y && /* @__PURE__ */ r(U, { onClose: P, type: z.WARNING, className: "adyen-pe-reports-table-alert", ...y }),
    /* @__PURE__ */ r(
      D,
      {
        errorDisplay: L,
        error: l,
        columns: R,
        data: b,
        loading: F,
        outline: !1,
        emptyTableMessage: O,
        customCells: {
          createdAt: ({ value: e }) => e ? e && /* @__PURE__ */ r("time", { dateTime: e, children: /* @__PURE__ */ r(n, { el: a.SPAN, variant: s.BODY, children: v(e, E) }) }) : null,
          dateAndReportType: ({ item: e }) => /* @__PURE__ */ r("div", { className: j, children: [
            e?.type && /* @__PURE__ */ r(n, { el: a.SPAN, variant: s.BODY, stronger: !0, children: w(o, e.type) }),
            /* @__PURE__ */ r("time", { dateTime: e.createdAt, children: /* @__PURE__ */ r(n, { className: Q, el: a.SPAN, variant: s.BODY, children: v(e.createdAt, E) }) })
          ] }),
          reportType: ({ item: e }) => e?.type && /* @__PURE__ */ r(n, { el: a.SPAN, variant: s.BODY, children: w(o, e.type) }),
          reportFile: ({ item: e }) => {
            const t = {
              query: { balanceAccountId: S, createdAt: e.createdAt, type: e.type }
            };
            return /* @__PURE__ */ r(
              K,
              {
                className: "adyen-pe-reports-table--download",
                endpointName: "downloadReport",
                disabled: N,
                requestParams: t,
                onDownloadRequested: h,
                setError: I,
                errorDisplay: B,
                "aria-label": o.get("reports.overview.list.controls.downloadReport.label")
              }
            );
          }
        },
        children: g && /* @__PURE__ */ r(D.Footer, { children: /* @__PURE__ */ r(
          J,
          {
            ..._,
            ariaLabelKey: "reports.overview.pagination.label",
            limitSelectAriaLabelKey: "reports.overview.pagination.controls.limitSelect.label"
          }
        ) })
      }
    )
  ] });
};
export {
  $ as FIELDS,
  Se as ReportsTable
};
