import { jsx as e } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { useState as S, useCallback as c, useMemo as A } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { Alert as y } from "../../../../internal/Alert/Alert.js";
import { AlertTypeOption as h } from "../../../../internal/Alert/types.js";
import { ButtonVariant as M } from "../../../../internal/Button/types.js";
import k from "../../../../../hooks/useMutation/useMutation.js";
import { useActionsAlertTitles as I } from "../../hooks.js";
import { setTopWindowHref as R, getTopWindowHref as x } from "../GrantActions/utils.js";
import { sharedCapitalOverviewAnalyticsEventProperties as B, GRANT_ACTION_CONFIGS as u } from "../../constants.js";
/* empty css                        */
import O from "../../../../../core/Context/preact/useCoreContext.js";
import W from "../../../../../core/Context/analytics/preact/useAnalyticsContext.js";
import v from "../../../../internal/Button/Button.js";
import { useConfigContext as w } from "../../../../../core/ConfigContext/preact/context.js";
import { EMPTY_OBJECT as G } from "../../../../../utils/value/constants.js";
const s = {
  button: "adyen-pe-grant-actions-hosted__button",
  buttonLabel: "adyen-pe-grant-actions-hosted__button-label",
  actionsContainer: "adyen-pe-grant-actions-hosted__actions-container"
}, $ = ({ className: d, expirationDate: T, missingActions: n }) => {
  const { i18n: o, updateCore: E } = O(), p = W(), { anaCreditActionDetails: m, signToSActionDetails: f } = w().endpoints, [g, l] = S(null), L = c((t) => {
    t?.url ? R(t.url) : l(null);
  }, []), N = A(
    () => ({
      signToS: f,
      AnaCredit: m
    }),
    [m, f]
  ), r = k({
    queryFn: (t) => {
      const _ = {
        query: { redirectUrl: x(), locale: o.locale }
      };
      return N[t]?.(G, _);
    },
    options: {
      onSuccess: L,
      // Reset the loading state when the mutation finishes (success or error)
      onError: () => {
        l(null);
      }
    }
  }), C = c(
    (t) => {
      p.addEvent?.("Clicked link", {
        ...B,
        subCategory: "Missing action",
        label: t
      });
    },
    [p]
  ), a = c(
    (t) => /* @__PURE__ */ e(
      v,
      {
        className: s.button,
        onClick: () => {
          try {
            l(t), r.mutate(t);
          } finally {
            C(u[t].eventLabel);
          }
        },
        variant: M.TERTIARY,
        "aria-label": o.get(u[t].buttonLabelKey),
        disabled: r.isLoading,
        state: g === t ? "loading" : void 0,
        children: /* @__PURE__ */ e("span", { className: s.buttonLabel, children: o.get(u[t].buttonLabelKey) })
      }
    ),
    [r, o, g, C]
  ), i = I(T), b = A(() => {
    if (n.length > 1)
      return {
        title: i.multiple,
        description: /* @__PURE__ */ e("ol", { className: s.actionsContainer, children: n.map((t) => /* @__PURE__ */ e("li", { children: a(t.type) }, t.type)) })
      };
    {
      const t = n[0].type;
      return {
        title: i.single,
        description: a(t)
      };
    }
  }, [i.multiple, i.single, n, a]);
  return r.error ? /* @__PURE__ */ e(
    y,
    {
      className: d,
      type: h.CRITICAL,
      title: o.get("capital.overview.grants.item.alerts.somethingWentWrong"),
      description: /* @__PURE__ */ e(v, { className: s.button, onClick: E, children: o.get("common.actions.refresh.labels.default") })
    }
  ) : /* @__PURE__ */ e(y, { className: d, type: h.WARNING, title: b.title, description: b.description });
};
export {
  $ as GrantActionsHosted
};
