;
{
  try {
    let e = "undefined" != typeof window ? window : "undefined" != typeof global ? global : "undefined" != typeof globalThis ? globalThis : "undefined" != typeof self ? self : {}, n = new e.Error().stack;
    n && (e._sentryDebugIds = e._sentryDebugIds || {}, e._sentryDebugIds[n] = "4373cbf0-bace-4736-853b-b979be6a7f54", e._sentryDebugIdIdentifier = "sentry-dbid-4373cbf0-bace-4736-853b-b979be6a7f54");
  } catch (e) {
  }
}
;
import { jsx, jsxs } from "preact/jsx-runtime";
import { useRef, useCallback, useEffect, useState } from "preact/hooks";
import { V as Button, u as useTranslation, t as IconButton, q as TaskStatuses } from "./StoreProvider-CqQrndhS.js";
import { C as Card } from "./Card-OQuFzmxm.js";
import { T as Tag } from "./Tag-DdkVScyr.js";
import { V as VerificationErrorAlert } from "./VerificationErrorAlert-CHLilqXN.js";
import { C as Confirm } from "./Confirm-BeimUgOe.js";
import { T as TaskVerificationStatus } from "./TaskVerificationStatus-C19Z4x3D.js";
import { g as getRoleName, a as getRoleTagVariant } from "./roleMetadata-BhGtDULY.js";
function useClickAwayListener(ref, onClickAway) {
  const handleClickAway = useCallback(
    (event) => {
      if (ref.current && !ref.current.contains(event.target)) {
        onClickAway();
      }
    },
    [ref, onClickAway]
  );
  useEffect(() => {
    document.addEventListener("mousedown", handleClickAway);
    return () => {
      document.removeEventListener("mousedown", handleClickAway);
    };
  }, [handleClickAway]);
}
const ClickAwayWrapper = ({ children, onClickAway, ...divProps }) => {
  const ref = useRef(null);
  useClickAwayListener(ref, onClickAway);
  return /* @__PURE__ */ jsx("div", { ref, ...divProps, children });
};
const actionsMenu = "adyen-kyc-actions-menu";
const styles$1 = {
  actionsMenu
};
const ActionsMenu = ({ options, open, setOpen }) => {
  const menuItems = options.map(({ label, onSelect, icon }) => /* @__PURE__ */ jsx(
    Button,
    {
      onClick: (event) => {
        event.stopPropagation();
        setOpen(false);
        onSelect();
      },
      variant: "tertiary",
      icon,
      children: label
    },
    label
  ));
  return open ? /* @__PURE__ */ jsx(
    ClickAwayWrapper,
    {
      onClickAway: () => {
        setOpen(false);
      },
      className: styles$1.actionsMenu,
      role: "menu",
      children: menuItems
    }
  ) : null;
};
const entityAssociationWrapper = "adyen-kyc-entity-association-wrapper";
const entityAssociation = "adyen-kyc-entity-association";
const entityAssociationTop = "adyen-kyc-entity-association-top";
const entityAssociationTitle = "adyen-kyc-entity-association-title";
const entityAssociationInfo = "adyen-kyc-entity-association-info";
const entityAssociationTag = "adyen-kyc-entity-association-tag";
const entityAssociationActions = "adyen-kyc-entity-association-actions";
const entityAssociationAlert = "adyen-kyc-entity-association-alert";
const styles = {
  entityAssociationWrapper,
  entityAssociation,
  entityAssociationTop,
  entityAssociationTitle,
  entityAssociationInfo,
  entityAssociationTag,
  entityAssociationActions,
  entityAssociationAlert
};
const EntityAssociation = ({
  name,
  types,
  status,
  actions,
  problems,
  infoContent,
  onNavigateToEntity,
  deleteEntity,
  isDeleteDisabled
}) => {
  const { t } = useTranslation("common");
  const [showingActions, setShowingActions] = useState(false);
  const [isConfirmShown, setIsConfirmShown] = useState(false);
  const isDeletable = !!deleteEntity;
  const handleRemoveIntent = (event) => {
    event.stopPropagation();
    setIsConfirmShown(true);
  };
  const dismissConfirm = () => {
    setIsConfirmShown(false);
  };
  return /* @__PURE__ */ jsxs("div", { className: styles.entityAssociationWrapper, children: [
    /* @__PURE__ */ jsx(Card, { onClick: onNavigateToEntity, children: /* @__PURE__ */ jsxs("div", { className: styles.entityAssociation, children: [
      /* @__PURE__ */ jsxs("div", { className: styles.entityAssociationTop, children: [
        /* @__PURE__ */ jsx("h3", { className: styles.entityAssociationTitle, children: name }),
        /* @__PURE__ */ jsxs("div", { className: styles.entityAssociationInfo, children: [
          /* @__PURE__ */ jsx(TaskVerificationStatus, { status }),
          actions && /* @__PURE__ */ jsxs("div", { className: styles.entityAssociationActions, children: [
            /* @__PURE__ */ jsx(
              IconButton,
              {
                variant: "tertiary",
                icon: "more-horiz",
                onClick: (event) => {
                  event.stopPropagation();
                  setShowingActions(!showingActions);
                },
                ariaLabel: t(($) => $["toggleAssociationMenu"], { name })
              }
            ),
            /* @__PURE__ */ jsx(
              ActionsMenu,
              {
                options: actions,
                open: showingActions,
                setOpen: setShowingActions
              }
            )
          ] }),
          isDeletable && /* @__PURE__ */ jsx(
            IconButton,
            {
              ariaLabel: t(($) => $["delete"]),
              icon: "bin",
              variant: "tertiary",
              onClick: handleRemoveIntent,
              disabled: isConfirmShown || isDeleteDisabled
            }
          )
        ] })
      ] }),
      /* @__PURE__ */ jsx("div", { children: types.map((type) => /* @__PURE__ */ jsx(
        Tag,
        {
          className: styles.entityAssociationTag,
          variant: getRoleTagVariant(type),
          children: t(($) => $[getRoleName(type)])
        },
        type
      )) }),
      status === TaskStatuses.ERROR && /* @__PURE__ */ jsx(VerificationErrorAlert, { className: styles.entityAssociationAlert, problems }),
      infoContent
    ] }) }),
    isConfirmShown && isDeletable && /* @__PURE__ */ jsx(
      Confirm,
      {
        title: t(($) => $["deleteItem"], { item: name }),
        description: t(($) => $["thisActionIsPermanent"]),
        onConfirm: deleteEntity,
        confirmText: t(($) => $["delete"]),
        onCancel: dismissConfirm,
        critical: true
      }
    )
  ] });
};
export {
  EntityAssociation as E
};
