import { jsx, Fragment, jsxs } from "react/jsx-runtime";
import { useRef } from "react";
import { getAssetUrl } from "../../utils/assetBase.js";
const CommentIndicator = getAssetUrl("icons/comment-indicator.svg");
const withCommentIcon = (Renderer) => {
  return (props) => {
    var _a, _b;
    const iconRef = useRef(null);
    const {
      handleCommentIconHover,
      handleMouseEnter,
      handleMouseLeave,
      popup,
      resolvedComments,
      getLatestUnresolvedThreadId,
      configuration,
      colIdentifierKey
    } = props.context;
    const rowIdentifier = colIdentifierKey ? ((_a = props.data) == null ? void 0 : _a[colIdentifierKey]) || props.node.id : props.node.id;
    const baseCellId = `${rowIdentifier}__${props.column.getColId()}`;
    let effectiveCellId = baseCellId;
    if ((popup == null ? void 0 : popup.cellId) != baseCellId && !(configuration == null ? void 0 : configuration.disableMultiCommentThread)) {
      const baseResolved = Array.isArray(resolvedComments) ? resolvedComments.includes(baseCellId) : false;
      if (baseResolved) {
        const latestUnresolved = getLatestUnresolvedThreadId(baseCellId);
        if (latestUnresolved) {
          effectiveCellId = latestUnresolved;
        } else {
          effectiveCellId = baseCellId;
        }
      }
    }
    const commentsArray = (_b = props.context.comments) == null ? void 0 : _b[effectiveCellId];
    const hasComment = commentsArray && Array.isArray(commentsArray) && commentsArray.length > 0;
    const isPortalOpen = (popup == null ? void 0 : popup.cellId) === effectiveCellId;
    const displayValue = props.valueFormatted !== void 0 && props.valueFormatted !== null ? props.valueFormatted : props.value;
    const safeValue = displayValue !== null && typeof displayValue === "object" ? JSON.stringify(displayValue) : displayValue ?? "";
    const Content = Renderer ? /* @__PURE__ */ jsx(Renderer, { ...props }) : /* @__PURE__ */ jsx(Fragment, { children: safeValue });
    const isResolved = Array.isArray(resolvedComments) && resolvedComments.find((comment) => comment === effectiveCellId);
    return /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(
        "div",
        {
          style: {
            position: "absolute",
            top: 0,
            left: 0,
            right: 0,
            bottom: 0,
            border: hasComment && isPortalOpen ? "1px solid #4259ee" : "none",
            boxSizing: "border-box",
            pointerEvents: "none",
            zIndex: 1
          }
        }
      ),
      Content,
      hasComment && !isResolved && /* @__PURE__ */ jsx(
        "div",
        {
          ref: iconRef,
          style: {
            position: "absolute",
            top: -15,
            right: 0,
            cursor: "pointer",
            zIndex: 2
          },
          title: `${commentsArray.length} comment${commentsArray.length > 1 ? "s" : ""}`,
          onMouseLeave: handleMouseLeave,
          onMouseOver: () => {
            handleMouseEnter();
            handleCommentIconHover(props);
          },
          children: /* @__PURE__ */ jsx("img", { alt: "", src: CommentIndicator })
        }
      )
    ] });
  };
};
export {
  withCommentIcon
};
