import { jsx as t } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import R from "../../../../internal/Typography/Typography.js";
import { TypographyVariant as N, TypographyElement as P } from "../../../../internal/Typography/types.js";
import E from "../../../../internal/Button/Button.js";
import { ButtonVariant as _ } from "../../../../internal/Button/types.js";
import { useMemo as m, useState as k, useCallback as c, useEffect as Y } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import L from "../../../../../hooks/useMutation/useMutation.js";
/* empty css                           */
import { sharedCapitalOfferAnalyticsEventProperties as q } from "../CapitalOffer/constants.js";
import { getExpectedRepaymentDate as F, getPercentage as j } from "../utils/utils.js";
import { CapitalErrorMessageDisplay as G } from "../utils/CapitalErrorMessageDisplay.js";
import { calculateSliderAdjustedMidValue as H } from "../../../../internal/Slider/Slider.js";
import { CAPITAL_REPAYMENT_FREQUENCY as Q } from "../../../../constants.js";
import { debounce as U } from "../../../../utils/utils.js";
import C from "../../../../../core/Context/preact/useCoreContext.js";
import $ from "../../../../../core/Context/analytics/preact/useAnalyticsContext.js";
import { Fragment as z } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/dist/preact.module.js";
import J from "../../../../internal/CapitalSlider/CapitalSlider.js";
import K from "../../../../internal/InfoBox/InfoBox.js";
import W from "../../../../internal/StructuredList/StructuredList.js";
import { useConfigContext as X } from "../../../../../core/ConfigContext/preact/context.js";
const D = {
  ...q,
  subCategory: "Business financing offer"
}, Z = () => /* @__PURE__ */ t("div", { className: "adyen-pe-capital-offer-selection__loading-container", children: [...Array(4)].map((e, n) => /* @__PURE__ */ t("div", { className: "adyen-pe-capital-offer-selection__loading-skeleton" }, n)) }), O = ({ data: e }) => {
  const { i18n: n } = C(), u = m(() => {
    const r = e.expectedRepaymentPeriodDays && F(e.expectedRepaymentPeriodDays);
    return r ? n.date(r, { month: "long" }) : null;
  }, [e, n]);
  return /* @__PURE__ */ t("div", { className: "adyen-pe-capital-offer-selection__information", children: [
    /* @__PURE__ */ t(R, { el: P.SPAN, variant: N.BODY, wide: !0, children: u && n.get("capital.offer.common.repaymentInfo", {
      values: {
        amount: n.amount(e.thresholdAmount.value, e.thresholdAmount.currency),
        days: Q,
        date: u
      }
    }) }),
    /* @__PURE__ */ t(
      W,
      {
        renderValue: (r) => /* @__PURE__ */ t(R, { el: P.SPAN, stronger: !0, variant: N.CAPTION, children: r }),
        renderLabel: (r) => /* @__PURE__ */ t(R, { el: P.SPAN, variant: N.CAPTION, children: r }),
        items: [
          { key: "capital.common.fields.fees", value: n.amount(e.feesAmount.value, e.feesAmount.currency) },
          {
            key: "capital.common.fields.dailyRepaymentRate",
            value: `${n.get("capital.common.values.percentage", {
              values: { percentage: j(e.repaymentRate) }
            })}`
          },
          {
            key: "capital.common.fields.expectedRepaymentPeriod",
            value: n.get("capital.common.values.numberOfDays", { values: { days: e.expectedRepaymentPeriodDays } })
          }
        ]
      }
    )
  ] });
}, Ne = ({
  dynamicOffersConfig: e,
  dynamicOffersConfigError: n,
  emptyGrantOffer: u,
  onContactSupport: r,
  onOfferDismiss: f,
  onOfferSelect: T,
  requestedAmount: v
}) => {
  const { i18n: g } = C(), d = $(), h = m(() => {
    if (e)
      return H(
        e.minAmount.value,
        e.maxAmount.value,
        e.step
      );
  }, [e]), [l, S] = k(void 0), s = m(() => e?.minAmount.currency, [e?.minAmount.currency]), { createGrantOffer: B, getDynamicGrantOffer: I } = X().endpoints, a = L({
    queryFn: I,
    options: {
      retry: 1,
      shouldRetry: c((o) => o.status === 500, []),
      onSettled: c(() => {
        b(!1);
      }, [])
    }
  }), i = L({
    queryFn: B,
    options: {
      onSuccess: (o) => T(o)
    }
  }), M = c(() => {
    try {
      i.mutate({
        body: {
          amount: a.data?.grantAmount.value || l,
          currency: a.data?.grantAmount.currency || s
        },
        contentType: "application/json"
      });
    } finally {
      d.addEvent?.("Clicked button", { ...D, label: "Review offer" });
    }
  }, [s, a.data, l, i, d]), p = c(
    (o) => a.mutate({}, { query: { amount: o, currency: s } }),
    [s, a]
  ), [A, b] = k(!1), y = m(() => U(p, 300), [p]), V = c(
    (o) => {
      y.cancel(), b(!0), S(o);
    },
    [y]
  ), w = c(
    (o) => {
      try {
        return y(o);
      } finally {
        d.addEvent?.("Changed capital offer slider", {
          ...D,
          label: "Slider changed",
          currency: s,
          value: o
        });
      }
    },
    [y, d, s]
  );
  Y(() => {
    e && !a.data && !l && (S(
      (o) => o || (v ? Number(v) : h || e.minAmount.value)
    ), p(l || h || e.minAmount.value));
  }, [e, a.data, p, h, v, l]);
  const x = m(
    () => i.isLoading || a.isLoading || A,
    [a.isLoading, A, i.isLoading]
  );
  return /* @__PURE__ */ t("div", { className: "adyen-pe-capital-offer-selection", children: i.error || a.error || u || n ? /* @__PURE__ */ t(
    G,
    {
      error: i.error || a.error || n,
      onBack: f,
      onContactSupport: r,
      emptyGrantOffer: u
    }
  ) : /* @__PURE__ */ t(z, { children: [
    e && /* @__PURE__ */ t(
      J,
      {
        value: l,
        dynamicOffersConfig: e,
        onValueChange: V,
        onRelease: w
      }
    ),
    /* @__PURE__ */ t(K, { className: "adyen-pe-capital-offer-selection__details", children: !a.data || a.isLoading || A ? /* @__PURE__ */ t(Z, {}) : a.data ? /* @__PURE__ */ t(O, { data: a.data }) : null }),
    /* @__PURE__ */ t("div", { className: "adyen-pe-capital-offer-selection__buttons", children: [
      f && /* @__PURE__ */ t(E, { variant: _.SECONDARY, onClick: f, children: g.get("capital.common.actions.goBack") }),
      /* @__PURE__ */ t(
        E,
        {
          variant: _.PRIMARY,
          state: x ? "loading" : void 0,
          onClick: M,
          disabled: i.isLoading || !e?.minAmount,
          "aria-label": g.get("capital.offer.selection.actions.reviewOffer"),
          children: g.get(
            x ? "capital.offer.selection.actions.reviewOffer.states.loading" : "capital.offer.selection.actions.reviewOffer"
          )
        }
      )
    ] })
  ] }) });
};
export {
  Ne as CapitalOfferSelection
};
