import { jsx as t } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { useState as E, useMemo as m, useCallback as v } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import y from "classnames";
import I from "../../../../../hooks/useTimezoneAwareDateFormatting.js";
import n from "../../../../internal/Typography/Typography.js";
import { TypographyVariant as s, TypographyElement as A } from "../../../../internal/Typography/types.js";
import { Tag as f } from "../../../../internal/Tag/Tag.js";
import { GRANT_ITEM_CLASS_NAMES as i } from "./constants.js";
import { getGrantConfig as N } from "./utils.js";
/* empty css               */
import { GrantDetails as x } from "../GrantDetails/GrantDetails.js";
import S from "../../../../internal/CopyText/CopyText.js";
import { Tooltip as w } from "../../../../internal/Tooltip/Tooltip.js";
import { Alert as L } from "../../../../internal/Alert/Alert.js";
import { AlertTypeOption as P } from "../../../../internal/Alert/types.js";
import { ButtonVariant as B } from "../../../../internal/Button/types.js";
import D from "../../../../internal/ExpandableCard/ExpandableCard.js";
import { GrantActions as G } from "../GrantActions/GrantActions.js";
import { sharedCapitalOverviewAnalyticsEventProperties as O } from "../../constants.js";
import R from "../../../../../core/Context/preact/useCoreContext.js";
import V from "../../../../../core/Context/analytics/preact/useAnalyticsContext.js";
import { Fragment as T } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/dist/preact.module.js";
import { Translation as K } from "../../../../internal/Translation/Translation.js";
import { DATE_FORMAT_CAPITAL_OVERVIEW as $ } from "../../../../../constants/dateFormats.js";
import F from "../../../../internal/ProgressBar/ProgressBar.js";
import _ from "../../../../internal/Button/Button.js";
import l from "../../../../../utils/random/uniqueId.js";
const gt = ({ grant: a, showDetails: c }) => {
  const { i18n: r } = R(), { dateFormat: d } = I(), p = V(), [u, h] = E(!1), e = m(() => N(a, u), [a, u]), g = v(() => {
    try {
      return c?.("unscheduledRepayment");
    } finally {
      p.addEvent?.("Clicked button", {
        ...O,
        subCategory: "Grant active",
        label: "Send repayment"
      });
    }
  }, [c, p]), o = m(
    () => ({
      grantAmount: l("elem"),
      grantAmountLabel: l("elem"),
      grantStatus: l("elem"),
      termEnds: l("elem")
    }),
    []
  ), C = m(
    () => /* @__PURE__ */ t("div", { className: i.cardContent, children: [
      /* @__PURE__ */ t("div", { className: i.statusContainer, children: [
        /* @__PURE__ */ t(
          n,
          {
            id: o.grantAmountLabel,
            variant: s.CAPTION,
            className: y({ [i.textSecondary]: e.isLabelColorSecondary }),
            testId: "grant-amount-label",
            children: r.get(e.amountLabelKey)
          }
        ),
        /* @__PURE__ */ t("div", { id: o.grantStatus, children: a.status === "Active" ? /* @__PURE__ */ t(T, { children: /* @__PURE__ */ t(
          n,
          {
            id: o.termEnds,
            variant: s.CAPTION,
            el: A.SPAN,
            "aria-hidden": !0,
            children: /* @__PURE__ */ t(
              K,
              {
                translationKey: "capital.overview.grants.item.termEnds",
                fills: {
                  date: /* @__PURE__ */ t(
                    "time",
                    {
                      "aria-labelledby": o.termEnds,
                      dateTime: e.repaymentPeriodEndDate.toISOString(),
                      children: /* @__PURE__ */ t(n, { variant: s.CAPTION, stronger: !0, el: A.SPAN, children: d(e.repaymentPeriodEndDate, $) })
                    }
                  )
                }
              }
            )
          }
        ) }) : e.statusKey ? e.statusTooltipKey ? /* @__PURE__ */ t(w, { content: r.get(e.statusTooltipKey), children: /* @__PURE__ */ t("div", { children: /* @__PURE__ */ t(f, { label: r.get(e.statusKey), variant: e.statusTagVariant }) }) }) : /* @__PURE__ */ t(f, { label: r.get(e.statusKey), variant: e.statusTagVariant }) : null })
      ] }),
      /* @__PURE__ */ t(
        n,
        {
          id: o.grantAmount,
          variant: s.TITLE,
          medium: !0,
          className: y({
            [i.textSecondary]: e.isAmountColorSecondary
          }),
          children: r.amount(e.amount.value, e.amount.currency)
        }
      ),
      e.isProgressBarVisible && /* @__PURE__ */ t(
        F,
        {
          className: i.progressBar,
          value: a.repaidTotalAmount.value,
          max: a.totalAmount.value,
          labels: {
            ariaLabel: r.get("capital.overview.grants.item.progressBar.a11y.label"),
            current: r.get("capital.overview.grants.item.amounts.repaid"),
            max: r.get("capital.overview.grants.item.amounts.remaining")
          },
          tooltips: {
            remaining: `${r.amount(a.remainingTotalAmount.value, a.remainingTotalAmount.currency)} ${r.get("capital.overview.grants.item.amounts.remaining")?.toLowerCase()}`,
            progress: `${r.amount(a.repaidTotalAmount.value, a.repaidTotalAmount.currency)} ${r.get("capital.overview.grants.item.amounts.repaid")?.toLowerCase()}`
          }
        }
      ),
      e.isGrantIdVisible ? /* @__PURE__ */ t("div", { className: i.grantID, children: /* @__PURE__ */ t(
        S,
        {
          textToCopy: a.id,
          visibleText: r.get("capital.common.fields.grantID"),
          copyButtonAriaLabelKey: "capital.overview.grants.item.actions.copyGrantID",
          isUnderlineVisible: !0,
          type: "Text",
          "data-testid": "grant-id-copy-text"
        }
      ) }) : null,
      e.hasUnscheduledRepaymentDetails && /* @__PURE__ */ t("div", { className: i.actionsBar, children: /* @__PURE__ */ t(
        _,
        {
          onClick: g,
          className: i.mainActionBtn,
          variant: B.SECONDARY,
          fullWidth: !0,
          children: r.get("capital.overview.grants.item.actions.sendRepayment")
        }
      ) })
    ] }),
    [o, e, r, a, d, g]
  ), b = v(() => {
    h(!0);
  }, []);
  return /* @__PURE__ */ t("div", { className: i.base, children: [
    /* @__PURE__ */ t(
      D,
      {
        "aria-describedby": `${o.grantAmountLabel} ${o.grantAmount} ${o.grantStatus}`,
        "aria-label": r.get("capital.overview.grants.item.details.a11y.label"),
        filled: e.isBackgroundFilled,
        renderContent: C,
        inFlow: !0,
        children: e.hasDetails && /* @__PURE__ */ t(x, { grant: a })
      }
    ),
    e.hasAlerts && /* @__PURE__ */ t(T, { children: a.missingActions && a.missingActions.length ? /* @__PURE__ */ t(
      G,
      {
        grantId: a.id,
        missingActions: a.missingActions,
        className: i.alert,
        offerExpiresAt: a.offerExpiresAt,
        onComplete: b
      }
    ) : /* @__PURE__ */ t(
      L,
      {
        className: i.alert,
        type: P.HIGHLIGHT,
        title: r.get("capital.overview.grants.item.alerts.processingRequest")
      }
    ) })
  ] });
};
export {
  gt as GrantItem
};
