import { jsx as i } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { Fragment as U } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/dist/preact.module.js";
import { useRef as w, useCallback as e, useState as v, useMemo as M, useEffect as R } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { Alert as X } from "../../../../internal/Alert/Alert.js";
import { AlertTypeOption as x } from "../../../../internal/Alert/types.js";
import { ButtonVariant as G } from "../../../../internal/Button/types.js";
import { GRANT_ACTION_CONFIGS as Y, sharedCapitalOverviewAnalyticsEventProperties as F } from "../../constants.js";
import { useActionsAlertTitles as Z } from "../../hooks.js";
/* empty css                          */
import $ from "../../../../../core/Context/preact/useCoreContext.js";
import tt from "../../../../../core/Context/analytics/preact/useAnalyticsContext.js";
import { Icon as et } from "../../../../internal/Icon/Icon.js";
import nt from "../../../../internal/Button/Button.js";
import ot from "../../../../internal/Modal/Modal.js";
import { useConfigContext as it } from "../../../../../core/ConfigContext/preact/context.js";
import { EMPTY_OBJECT as st } from "../../../../../utils/value/constants.js";
const D = {
  actionButtonsContainer: "adyen-pe-grant-actions-embedded__action-buttons-container",
  actionButton: "adyen-pe-grant-actions-embedded__action-button"
}, rt = 50, b = {
  ...F,
  category: "Missing action modal"
}, Et = ({
  className: K,
  expirationDate: P,
  legalEntityId: y,
  missingActions: s,
  onComplete: S
}) => {
  const { i18n: r, environment: E } = $(), n = tt(), { getOnboardingConfiguration: T } = it().endpoints, u = w(/* @__PURE__ */ new Set()), p = w(/* @__PURE__ */ new Map()), k = e(async () => ({ token: (await T?.(st))?.token || "" }), [T]), [B, W] = v([]), [c, I] = v(void 0), [l, H] = v([]), d = M(() => l.length === s.length, [l.length, s.length]);
  R(() => {
    d && S();
  }, [d, S]), R(() => {
    const t = p.current;
    return () => {
      t.forEach((o) => clearTimeout(o)), t.clear();
    };
  }, []);
  const L = e(
    async (t) => {
      if (!B.includes(t)) {
        switch (t) {
          case "AnaCredit":
            await import("../../../../../external/.pnpm/@adyen_kyc-components@4.0.0-compact.9_react@19.2.0_typescript@5.8.3/node_modules/@adyen/kyc-components/dist/adyen-business-financing.js");
            break;
          case "signToS":
            await import("../../../../../external/.pnpm/@adyen_kyc-components@4.0.0-compact.9_react@19.2.0_typescript@5.8.3/node_modules/@adyen/kyc-components/dist/adyen-terms-of-service-management.js");
            break;
        }
        W((o) => [...o, t]);
      }
    },
    [B]
  ), N = e(
    async (t) => {
      await L(t), I(t), n.addEvent?.("Clicked button", {
        ...F,
        subCategory: "Missing action",
        label: Y[t].eventLabel
      });
    },
    [L, n]
  ), O = e(
    (t) => s.find((a) => !l.includes(a.type))?.type === t ? G.PRIMARY : G.SECONDARY,
    [l, s]
  ), A = e(
    (t) => {
      const o = l.includes(t), a = Y[t];
      return /* @__PURE__ */ i(
        nt,
        {
          className: D.actionButton,
          onClick: () => N(t),
          variant: O(t),
          "aria-label": r.get(a.buttonLabelKey),
          iconLeft: o ? /* @__PURE__ */ i(et, { name: "checkmark-circle-fill" }) : void 0,
          children: o ? r.get(a.successButtonLabelKey) : r.get(a.buttonLabelKey)
        }
      );
    },
    [l, O, N, r]
  ), C = Z(P), h = e(
    (t) => d ? r.get("capital.overview.grants.item.alerts.actionsCompleted") : t,
    [d, r]
  ), _ = M(() => {
    if (s.length > 1)
      return {
        title: h(C.multiple),
        description: /* @__PURE__ */ i("div", { className: D.actionButtonsContainer, children: s.map((t) => /* @__PURE__ */ i(U, { children: A(t.type) }, t.type)) })
      };
    {
      const t = s[0].type;
      return {
        title: h(C.single),
        description: A(t)
      };
    }
  }, [C.multiple, C.single, s, h, A]), m = e(() => {
    I(void 0);
  }, []), f = e(() => {
    c && H((t) => t.includes(c) ? t : [...t, c]), m();
  }, [c, m]), g = e(
    (t, o) => {
      m();
      const a = p.current.get(t);
      a !== void 0 && clearTimeout(a);
      const Q = setTimeout(() => {
        u.current.has(t) || n.addEvent?.("Clicked button", {
          ...b,
          ...o
        }), u.current.delete(t), p.current.delete(t);
      }, rt);
      p.current.set(t, Q);
    },
    [m, n]
  ), V = e(() => {
    g("AnaCredit", {
      subCategory: "Information",
      label: "Dismissed AnaCredit information"
    });
  }, [g]), j = e(() => {
    u.current.add("AnaCredit"), f(), n.addEvent?.("Clicked button", {
      ...b,
      subCategory: "Information",
      label: "Submitted AnaCredit information"
    });
  }, [f, n]), q = e(() => {
    g("signToS", {
      subCategory: "Terms & conditions",
      label: "Dismissed terms & conditions"
    });
  }, [g]), z = e(() => {
    n.addEvent?.("Clicked button", {
      ...b,
      subCategory: "Terms & conditions",
      label: "Signed terms & conditions"
    });
  }, [n]), J = e(() => {
    u.current.add("signToS"), f(), n.addEvent?.("Clicked button", {
      ...b,
      subCategory: "Terms & conditions",
      label: "Finished terms & conditions"
    });
  }, [f, n]);
  return /* @__PURE__ */ i("div", { children: [
    /* @__PURE__ */ i(
      X,
      {
        className: K,
        type: d ? x.HIGHLIGHT : x.WARNING,
        title: _.title,
        description: _.description
      }
    ),
    /* @__PURE__ */ i(ot, { isOpen: !!c, onClose: m, isDismissible: !1, headerWithBorder: !1, size: "large", children: [
      c === "AnaCredit" && /* @__PURE__ */ i(
        "adyen-business-financing",
        {
          locale: r.locale,
          environment: E,
          fetchToken: k,
          rootlegalentityid: y,
          oncomplete: j,
          onclose: V
        }
      ),
      c === "signToS" && /* @__PURE__ */ i(
        "adyen-terms-of-service-management",
        {
          locale: r.locale,
          environment: E,
          fetchToken: k,
          rootlegalentityid: y,
          onaccept: z,
          oncomplete: J,
          onclose: q
        }
      )
    ] })
  ] });
};
export {
  Et as GrantActionsEmbedded
};
