import { jsx as e } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { useMemo as u, useCallback as d, useState as R } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { isCapitalRegionSupported as b } from "../../../../internal/CapitalHeader/helpers.js";
import { CapitalErrorMessageDisplay as F } from "../../../CapitalOffer/components/utils/CapitalErrorMessageDisplay.js";
import { CAPITAL_OVERVIEW_CLASS_NAMES as i } from "../../constants.js";
import { useFetch as v } from "../../../../../hooks/useFetch.js";
/* empty css                     */
import M from "../Unqualified.js";
import { PreQualified as Q } from "../PreQualified/PreQualified.js";
import { GrantList as S } from "../GrantList/GrantList.js";
import { ErrorMessageDisplay as U } from "../../../../internal/ErrorMessageDisplay/ErrorMessageDisplay.js";
import { getCapitalErrorMessage as w } from "../../../../utils/capital/getCapitalErrorMessage.js";
import { useConfigContext as A } from "../../../../../core/ConfigContext/preact/context.js";
import { CapitalHeader as L } from "../../../../internal/CapitalHeader/CapitalHeader.js";
import { EMPTY_OBJECT as O } from "../../../../../utils/value/constants.js";
const Z = ({
  hideTitle: o,
  onContactSupport: p,
  onFundsRequest: m,
  onOfferDismiss: g,
  onOfferOptionsRequest: q,
  skipPreQualifiedIntro: G
}) => {
  const C = A()?.extraConfig?.legalEntity, c = u(() => b(C), [C]), { getGrants: s, getDynamicGrantOffersConfiguration: l } = A().endpoints, t = v({
    fetchOptions: { enabled: !!s && c },
    queryFn: d(async () => s?.(O), [s])
  }), n = v({
    fetchOptions: { enabled: !!l && c },
    queryFn: d(async () => l?.(O), [l])
  }), r = n.data, [f, y] = R(), a = u(
    () => f ? [f, ...t.data?.data || []] : t.data?.data,
    [t.data?.data, f]
  ), x = d(
    (E) => {
      m ? m(E) : y(E);
    },
    [m]
  ), h = u(() => !!(n.error && t.error || n.error && !a?.length), [n.error, a?.length, t.error]), N = u(() => {
    if (c) {
      if (h)
        return "Error";
      if (!s && !l || !r && !a || t.isFetching || n.isFetching)
        return "Loading";
      if (a?.length)
        return "GrantList";
      if (r?.maxAmount && r?.minAmount)
        return "PreQualified";
    } else return "UnsupportedRegion";
    return "Unqualified";
  }, [
    l,
    r,
    n.isFetching,
    a,
    s,
    t.isFetching,
    h,
    c
  ]), P = u(() => !!(r && r.minAmount && r.maxAmount), [r]);
  return /* @__PURE__ */ e("div", { className: i.base, children: (() => {
    switch (N) {
      case "Loading":
        return /* @__PURE__ */ e("div", { className: i.skeletonContainer, children: [
          /* @__PURE__ */ e("div", { className: i.headerSkeleton }),
          /* @__PURE__ */ e("div", { className: i.skeleton })
        ] });
      case "Error":
        return /* @__PURE__ */ e("div", { className: i.errorContainer, children: [
          /* @__PURE__ */ e(L, { hideTitle: o, titleKey: "capital.common.title" }),
          /* @__PURE__ */ e(
            U,
            {
              absolutePosition: !1,
              outlined: !1,
              withImage: !0,
              onContactSupport: p,
              ...w(n.error, p)
            }
          )
        ] });
      case "GrantList":
        return a && /* @__PURE__ */ e(
          S,
          {
            externalDynamicOffersConfig: r,
            grantList: a,
            hideTitle: o,
            newOfferAvailable: P,
            onFundsRequest: m,
            onGrantListUpdateRequest: y,
            onOfferDismiss: g
          }
        );
      case "PreQualified":
        return /* @__PURE__ */ e(
          Q,
          {
            onOfferDismiss: g,
            onOfferOptionsRequest: q,
            skipPreQualifiedIntro: G,
            hideTitle: o,
            dynamicOffer: r,
            onFundsRequest: x
          }
        );
      case "Unqualified":
        return /* @__PURE__ */ e(M, { hideTitle: o });
      case "UnsupportedRegion":
        return /* @__PURE__ */ e("div", { className: i.errorContainer, children: [
          /* @__PURE__ */ e(L, { hideTitle: o, titleKey: "capital.common.title" }),
          /* @__PURE__ */ e(F, { unsupportedRegion: !0 })
        ] });
      default:
        return null;
    }
  })() });
};
export {
  Z as CapitalOverview
};
