import { jsx as e } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { useMemo as u, useState as l, useCallback as m } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { isCapitalRegionSupported as P } from "../../../../internal/CapitalHeader/helpers.js";
import { CapitalErrorMessageDisplay as q } from "../utils/CapitalErrorMessageDisplay.js";
import { CAPITAL_OFFER_CLASS_NAMES as d, sharedCapitalOfferAnalyticsEventProperties as D } from "./constants.js";
import { CapitalOfferSelection as F } from "../CapitalOfferSelection/CapitalOfferSelection.js";
import { useFetch as M } from "../../../../../hooks/useFetch.js";
import { CapitalOfferSummary as _ } from "../CapitalOfferSummary/CapitalOfferSummary.js";
import { useLandedPageEvent as x } from "../../../../../hooks/useAnalytics/useLandedPageEvent.js";
/* empty css                  */
import { CapitalHeader as O } from "../../../../internal/CapitalHeader/CapitalHeader.js";
import { useConfigContext as y } from "../../../../../core/ConfigContext/preact/context.js";
import { EMPTY_OBJECT as G } from "../../../../../utils/value/constants.js";
const L = {
  ...D,
  subCategory: "Capital offer"
}, N = ({
  externalDynamicOffersConfig: t,
  hideTitle: i,
  onContactSupport: o,
  onFundsRequest: C,
  onOfferDismiss: g,
  onOfferSelect: a
}) => {
  const [S, c] = l(!1), [E, h] = l(), [f, p] = l(), { getDynamicGrantOffersConfiguration: n } = y().endpoints, A = m((r) => {
    c(!r);
  }, []), { data: v, error: R } = M({
    fetchOptions: {
      enabled: !t && !!n,
      onSuccess: A
    },
    queryFn: m(async () => n?.(G), [n])
  }), T = t || v, b = m(
    (r) => {
      a ? a(r) : (h(r?.grantAmount.value), p(r));
    },
    [a]
  ), s = u(() => f ? "OfferSummary" : "OfferSelection", [f]);
  return x({ ...L, label: "Capital offer" }), /* @__PURE__ */ e("div", { className: d.base, children: [
    /* @__PURE__ */ e(
      O,
      {
        hasDivider: !0,
        hideTitle: i,
        titleKey: s === "OfferSummary" ? "capital.offer.summary.title" : "capital.offer.selection.title"
      }
    ),
    s === "OfferSelection" && /* @__PURE__ */ e(
      F,
      {
        requestedAmount: E,
        dynamicOffersConfig: T,
        dynamicOffersConfigError: R,
        onOfferDismiss: g,
        onOfferSelect: b,
        emptyGrantOffer: S,
        onContactSupport: o
      }
    ),
    s === "OfferSummary" && /* @__PURE__ */ e(
      _,
      {
        grantOffer: f,
        onBack: () => p(void 0),
        onFundsRequest: C,
        onContactSupport: o
      }
    )
  ] });
}, W = (t) => {
  const i = y()?.extraConfig?.legalEntity;
  return u(() => P(i), [i]) ? /* @__PURE__ */ e(N, { ...t }) : /* @__PURE__ */ e("div", { className: d.errorContainer, children: [
    /* @__PURE__ */ e(O, { hideTitle: t.hideTitle, titleKey: "capital.common.title" }),
    /* @__PURE__ */ e(q, { unsupportedRegion: !0 })
  ] });
};
export {
  W as CapitalOffer
};
