import { jsx as t } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { useCallback as v, useMemo as m, useEffect as j } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import k from "../../../../../hooks/useBalanceAccountSelection.js";
import G from "../../../../../hooks/useDefaultOverviewFilterParams.js";
import H from "../../../../internal/FilterBar/filters/DateFilter/DateFilter.js";
import { LIMIT_OPTIONS as Y, DEFAULT_PAGE_LIMIT as J } from "../../../../internal/Pagination/constants.js";
import { FilterParam as s } from "../../../../types.js";
import { ReportsTable as Q, FIELDS as V } from "../ReportsTable/ReportsTable.js";
import { BASE_CLASS as W, EARLIEST_PAYOUT_SINCE_DATE as X } from "./constants.js";
/* empty css                     */
import { useCustomColumnsData as Z } from "../../../../../hooks/useCustomColumnsData.js";
import $ from "../../../../utils/customData/hasCustomField.js";
import z from "../../../../utils/customData/mergeRecords.js";
import ee from "../../../../internal/Pagination/hooks/useCursorPaginatedRecords.js";
import { useFilterBarState as te, FilterBarMobileSwitch as re, FilterBar as oe } from "../../../../internal/FilterBar/FilterBar.js";
import ie from "../../../../internal/FormFields/Select/BalanceAccountSelector/BalanceAccountSelector.js";
import { useConfigContext as se } from "../../../../../core/ConfigContext/preact/context.js";
import { isFunction as ae } from "../../../../../utils/value/is.js";
import { Header as ne } from "../../../../internal/Header/Header.js";
const ge = ({
  onFiltersChanged: a,
  balanceAccounts: p,
  allowLimitSelection: u = !0,
  preferredLimit: C = J,
  isLoadingBalanceAccount: F,
  onContactSupport: S,
  hideTitle: T,
  dataCustomization: r
}) => {
  const { getReports: n } = se().endpoints, { activeBalanceAccount: e, balanceAccountSelectionOptions: D, onBalanceAccountSelection: R } = k({ balanceAccounts: p }), { defaultParams: o, nowTimestamp: I, refreshNowTimestamp: l } = G("reports", e), L = v(
    async (i, c) => n({ signal: c, errorLevel: "error" }, {
      query: {
        ...i,
        type: "payout",
        createdSince: i[s.CREATED_SINCE] ?? o.current.defaultFilterParams[s.CREATED_SINCE],
        createdUntil: i[s.CREATED_UNTIL] ?? o.current.defaultFilterParams[s.CREATED_UNTIL],
        balanceAccountId: e?.id ?? ""
      }
    }),
    [e?.id, o, n]
  ), d = te(), _ = m(() => ae(a) ? a : void 0, [a]), P = m(() => u ? Y : void 0, [u]), { canResetFilters: g, error: h, fetching: O, filters: f, limit: b, limitOptions: B, records: A, resetFilters: le, updateFilters: w, updateLimit: y, ...N } = ee({
    fetchRecords: L,
    dataField: "data",
    filterParams: o.current.defaultFilterParams,
    initialFiltersSameAsDefault: !0,
    onFiltersChanged: _,
    preferredLimit: C,
    preferredLimitOptions: P,
    enabled: !!e?.id && !!n
  }), U = v(
    ({ records: i, retrievedData: c }) => z(i, c, (E, q) => E.createdAt === q.createdAt),
    []
  ), M = m(() => $(r?.list?.fields, V), [r?.list?.fields]), { customRecords: x, loadingCustomRecords: K } = Z({
    records: A,
    hasCustomColumn: M,
    onDataRetrieve: r?.list?.onDataRetrieve,
    mergeCustomData: U
  });
  return j(() => {
    l();
  }, [f, l]), /* @__PURE__ */ t("div", { className: W, children: [
    /* @__PURE__ */ t(ne, { hideTitle: T, titleKey: "reports.overview.title", subtitleKey: "reports.overview.generateInfo", children: /* @__PURE__ */ t(re, { ...d }) }),
    /* @__PURE__ */ t(oe, { ...d, ariaLabelKey: "reports.overview.filters.label", children: [
      /* @__PURE__ */ t(
        ie,
        {
          activeBalanceAccount: e,
          balanceAccountSelectionOptions: D,
          onBalanceAccountSelection: R
        }
      ),
      /* @__PURE__ */ t(
        H,
        {
          canResetFilters: g,
          defaultParams: o,
          filters: f,
          nowTimestamp: I,
          refreshNowTimestamp: l,
          sinceDate: X,
          timezone: "UTC",
          updateFilters: w
        }
      )
    ] }),
    /* @__PURE__ */ t(
      Q,
      {
        balanceAccountId: e?.id,
        loading: O || F || !p || !e || K,
        data: r?.list?.onDataRetrieve ? x : A,
        showPagination: !0,
        limit: b,
        limitOptions: B,
        onContactSupport: S,
        onLimitSelection: y,
        error: h,
        customColumns: r?.list?.fields,
        ...N
      }
    )
  ] });
};
export {
  ge as ReportsOverview
};
