import { jsx as e } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { BaseList as _ } from "../../../../internal/BaseList/BaseList.js";
import { GrantItem as A } from "../GrantItem/GrantItem.js";
import { getGrantConfig as P } from "../GrantItem/utils.js";
import { GRANT_ADJUSTMENT_DETAILS as T } from "../GrantAdjustmentDetails/constants.js";
import { GrantRepaymentDetails as k } from "../GrantRepaymentDetails/GrantRepaymentDetails.js";
import { sharedCapitalOverviewAnalyticsEventProperties as R } from "../../constants.js";
import { useLandedPageEvent as x } from "../../../../../hooks/useAnalytics/useLandedPageEvent.js";
import B from "../../../../internal/SegmentedControl/SegmentedControl.js";
import { useState as y, useMemo as l, useCallback as p } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import U from "../../../../internal/Button/Button.js";
import { ButtonVariant as I } from "../../../../internal/Button/types.js";
import M from "../../../../../core/Context/preact/useCoreContext.js";
import O from "../../../../../core/Context/analytics/preact/useAnalyticsContext.js";
import { CapitalHeader as j } from "../../../../internal/CapitalHeader/CapitalHeader.js";
const b = {
  ...R,
  subCategory: "Grants overview"
}, u = ({ grants: t, showDetails: c }) => /* @__PURE__ */ e(_, { classNames: "adyen-pe-grant-list__items", children: t.map((a) => /* @__PURE__ */ e("li", { children: /* @__PURE__ */ e(A, { grant: a, showDetails: c.bind(null, a) }) }, a.id)) }), te = ({ grantList: t, hideTitle: c, newOfferAvailable: a, onNewOfferRequest: f }) => {
  const [w, v] = y(), [n, C] = y(), { i18n: h } = M(), g = O(), [i, m] = l(() => {
    const s = [], r = [];
    return t?.forEach((o) => {
      o.status === "Active" || o.status === "Pending" ? s.push(o) : r.push(o);
    }), [s, r];
  }, [t]), G = l(() => t.length > 1 && i.length && m.length ? "SegmentedGrants" : "UnifiedGrants", [i.length, t.length, m.length]), D = l(() => a && !i.some((s) => s.status === "Pending"), [i, a]), S = p(() => {
    try {
      return f();
    } finally {
      g.addEvent?.("Clicked button", { ...b, label: "See new offer" });
    }
  }, [f, g]), E = l(() => n && P(n), [n]), N = p(() => v(void 0), []), d = p((s, r) => {
    v(r), C(s);
  }, []);
  if (x({ ...b, label: "Capital overview" }), n)
    switch (w) {
      case T.unscheduledRepayment: {
        if (E?.hasUnscheduledRepaymentDetails)
          return /* @__PURE__ */ e(k, { grant: n, onDetailsClose: N });
        break;
      }
    }
  return /* @__PURE__ */ e("div", { className: "adyen-pe-grant-list", children: [
    /* @__PURE__ */ e("div", { className: "adyen-pe-grant-list__header-container", children: [
      /* @__PURE__ */ e(j, { hideTitle: c, titleKey: "capital.common.title" }),
      D ? /* @__PURE__ */ e(U, { onClick: S, className: "adyen-pe-grant-list__offer-button", variant: I.SECONDARY, children: h.get("capital.overview.grants.list.actions.newOffer") }) : null
    ] }),
    G === "UnifiedGrants" && /* @__PURE__ */ e(u, { grants: t, showDetails: d }),
    G === "SegmentedGrants" && /* @__PURE__ */ e(
      B,
      {
        items: [
          {
            label: "capital.overview.grants.list.tabs.labels.inProgress",
            content: /* @__PURE__ */ e(u, { grants: i, showDetails: d }),
            id: "active"
          },
          {
            label: "capital.overview.grants.list.tabs.labels.closed",
            content: /* @__PURE__ */ e(u, { grants: m, showDetails: d }),
            id: "inactive"
          }
        ],
        activeItem: "active",
        "aria-label": h.get("capital.overview.grants.list.tabs.a11y.label")
      }
    )
  ] });
};
export {
  te as GrantsDisplay
};
