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