import { jsx as a } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { useMemo as p, useCallback as y } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { getExpectedRepaymentDate as E, calculateMaximumRepaymentPeriodInMonths as q, getPercentage as P } from "../utils/utils.js";
import i from "../../../../internal/Typography/Typography.js";
import { TypographyVariant as m, TypographyElement as s } from "../../../../internal/Typography/types.js";
/* empty css                         */
import f from "../../../../internal/Button/Button.js";
import { ButtonVariant as R } from "../../../../internal/Button/types.js";
import M from "../../../../../hooks/useMutation/useMutation.js";
import { Tooltip as C } from "../../../../internal/Tooltip/Tooltip.js";
import { AlertTypeOption as B } from "../../../../internal/Alert/types.js";
import { Alert as D } from "../../../../internal/Alert/Alert.js";
import { CapitalErrorMessageDisplay as F } from "../utils/CapitalErrorMessageDisplay.js";
import L from "classnames";
import { sharedCapitalOfferAnalyticsEventProperties as S } from "../CapitalOffer/constants.js";
import { CAPITAL_REPAYMENT_FREQUENCY as N } from "../../../../constants.js";
import { CapitalOfferLegalNotice as W } from "../CapitalOfferLegalNotice/CapitalOfferLegalNotice.js";
import Y from "../../../../../core/Context/preact/useCoreContext.js";
import w from "../../../../../core/Context/analytics/preact/useAnalyticsContext.js";
import V from "../../../../internal/InfoBox/InfoBox.js";
import { Translation as G } from "../../../../internal/Translation/Translation.js";
import H from "../../../../internal/StructuredList/StructuredList.js";
import { Icon as U } from "../../../../internal/Icon/Icon.js";
import { useConfigContext as j } from "../../../../../core/ConfigContext/preact/context.js";
import { EMPTY_OBJECT as J } from "../../../../../utils/value/constants.js";
const K = ["30_013"], _ = {
  ...S,
  subCategory: "Business financing summary"
}, ge = ({
  grantOffer: t,
  onBack: h,
  onFundsRequest: b,
  onContactSupport: d
}) => {
  const { i18n: e } = Y(), l = w(), T = p(() => {
    const o = E(t.expectedRepaymentPeriodDays);
    return o ? e.date(o, { month: "long" }) : null;
  }, [t, e]), { requestFunds: k } = j().endpoints, r = M({
    queryFn: k,
    options: {
      onSuccess: (o) => b?.(o)
    }
  }), v = y(
    (o) => {
      r.mutate(J, { path: { grantOfferId: o } });
    },
    [r]
  ), x = y(() => {
    try {
      t.id && v(t.id);
    } finally {
      l.addEvent?.("Clicked button", { ..._, label: "Request funds" });
    }
  }, [t.id, v, l]), A = y(() => {
    try {
      return h();
    } finally {
      l.addEvent?.("Clicked button", { ..._, label: "Back to slider view" });
    }
  }, [h, l]), c = p(
    () => q(t.maximumRepaymentPeriodDays),
    [t.maximumRepaymentPeriodDays]
  ), n = p(() => {
    const o = r.error ? r.error : null;
    if (o && K.includes(o.errorCode))
      switch (o.errorCode) {
        case "30_013":
          return {
            title: e.get("capital.offer.common.errors.noPrimaryAccount"),
            message: e.get("capital.offer.common.errors.cannotContinueSupport"),
            errorCode: "30_013"
          };
        default:
          return {
            title: e.get("common.errors.somethingWentWrong"),
            message: e.get("capital.offer.common.errors.unavailable")
          };
      }
    return null;
  }, [e, r.error]), I = p(() => {
    const o = [
      {
        key: "capital.common.fields.fees",
        value: e.amount(t.feesAmount.value, t.feesAmount.currency)
      },
      {
        key: "capital.common.fields.totalRepaymentAmount",
        value: e.amount(t.totalAmount.value, t.totalAmount.currency)
      },
      {
        key: "capital.common.fields.repaymentThreshold",
        value: e.amount(t.thresholdAmount.value, t.thresholdAmount.currency)
      },
      {
        key: "capital.common.fields.dailyRepaymentRate",
        value: e.get("capital.common.values.percentage", { values: { percentage: P(t.repaymentRate) } })
      },
      {
        key: "capital.common.fields.expectedRepaymentPeriod",
        value: e.get("capital.common.values.numberOfDays", { values: { days: t.expectedRepaymentPeriodDays } })
      },
      { key: "capital.common.fields.account", value: e.get("capital.common.values.primaryAccount") }
    ];
    return c && o.splice(4, 0, {
      key: "capital.common.fields.maximumRepaymentPeriod",
      value: c === 1 ? e.get("capital.common.values.oneMonth") : e.get("capital.common.values.numberOfMonths", { values: { months: c } })
    }), t.aprBasisPoints && o.splice(1, 0, {
      key: "capital.common.fields.annualPercentageRate",
      value: e.get("capital.common.values.percentage", { values: { percentage: P(t.aprBasisPoints) } })
    }), o;
  }, [t, e, c]);
  return !n && r.error ? /* @__PURE__ */ a(F, { error: r.error, onBack: A, onContactSupport: d }) : /* @__PURE__ */ a("div", { className: "adyen-pe-capital-offer-summary", children: [
    /* @__PURE__ */ a(V, { className: "adyen-pe-capital-offer-summary__grant-summary", children: [
      /* @__PURE__ */ a(i, { el: s.PARAGRAPH, variant: m.BODY, children: /* @__PURE__ */ a(
        G,
        {
          translationKey: "capital.offer.common.fundingRequestInfo",
          fills: {
            amount: /* @__PURE__ */ a("strong", { children: `${e.amount(t.grantAmount.value, t.grantAmount.currency, { minimumFractionDigits: 0 })}` })
          }
        }
      ) }),
      /* @__PURE__ */ a(i, { el: s.PARAGRAPH, variant: m.CAPTION, children: e.get("capital.offer.common.repaymentInfo", {
        values: {
          amount: e.amount(t.thresholdAmount.value, t.thresholdAmount.currency),
          days: N,
          date: T ?? ""
        }
      }) })
    ] }),
    /* @__PURE__ */ a(
      H,
      {
        classNames: "adyen-pe-capital-offer-summary__details",
        renderLabel: (o, u) => u === "capital.common.fields.repaymentThreshold" ? /* @__PURE__ */ a(
          C,
          {
            isUnderlineVisible: !0,
            content: e.get("capital.common.fields.repaymentThreshold.description", {
              values: { days: N }
            }),
            children: /* @__PURE__ */ a("span", { children: /* @__PURE__ */ a(
              i,
              {
                className: "adyen-pe-capital-offer-summary__list-label",
                el: s.SPAN,
                variant: m.CAPTION,
                children: o
              }
            ) })
          }
        ) : u === "capital.common.fields.annualPercentageRate" ? /* @__PURE__ */ a(C, { isUnderlineVisible: !0, content: e.get("capital.common.fields.annualPercentageRate.description"), children: /* @__PURE__ */ a("span", { children: /* @__PURE__ */ a(
          i,
          {
            className: "adyen-pe-capital-offer-summary__list-label",
            el: s.SPAN,
            variant: m.CAPTION,
            children: o
          }
        ) }) }) : /* @__PURE__ */ a(
          i,
          {
            className: "adyen-pe-capital-offer-summary__list-label",
            el: s.SPAN,
            variant: m.CAPTION,
            children: o
          }
        ),
        renderValue: (o, u) => {
          const g = u === "capital.common.fields.account" && r.error && n && n.errorCode === "30_013";
          return /* @__PURE__ */ a(
            i,
            {
              className: L({
                "adyen-pe-capital-offer-summary__details--error": g
              }),
              el: s.SPAN,
              variant: m.CAPTION,
              stronger: !0,
              children: [
                g ? /* @__PURE__ */ a(U, { name: "warning-filled", "data-testid": "primary-account-warning-icon" }) : null,
                o
              ]
            }
          );
        },
        items: I
      }
    ),
    n && /* @__PURE__ */ a(
      D,
      {
        className: "adyen-pe-capital-offer-summary__error-alert",
        type: B.WARNING,
        title: n.title,
        description: n.message,
        children: d ? /* @__PURE__ */ a(f, { className: "adyen-pe-capital-offer-summary__error-alert-button", onClick: d, children: e.get("capital.common.actions.contactSupport") }) : null
      }
    ),
    /* @__PURE__ */ a(W, {}),
    /* @__PURE__ */ a("div", { className: "adyen-pe-capital-offer-summary__buttons", children: [
      r.error && !n ? null : /* @__PURE__ */ a(f, { variant: R.SECONDARY, onClick: A, children: e.get("capital.common.actions.goBack") }),
      /* @__PURE__ */ a(
        f,
        {
          variant: R.PRIMARY,
          state: r.isLoading ? "loading" : void 0,
          onClick: x,
          disabled: r.isLoading || !!r.error || !!r.data,
          "aria-label": e.get("capital.offer.summary.actions.requestFunds"),
          children: e.get(
            r.isLoading ? "capital.offer.summary.actions.requestFunds.states.loading" : "capital.offer.summary.actions.requestFunds"
          )
        }
      )
    ] })
  ] });
};
export {
  ge as CapitalOfferSummary
};
