import React, { useCallback, useContext, useRef, useState } from "react";
import { useConverters } from "./../../pm-converters/context.js";
import { TIPTAP_FIGURE_TYPE } from "./../../ricos-content/consts.js";
import { RicosContext } from "./../../ricos-context/index.js";
import { useEditorProp } from "./../../_shared/some-editor-props-context.js";
import { Loader, MediaItemErrorMsg } from "./../../ui-components/index.js";
import { getContainerClasses } from "../../_shared/figure/get-container-classes.js";
import { CornerRadiusHandles } from "../components/corner-radius-handles.js";
import { fromPmImageAnyState } from "../converter/from-pm-image-any-state.js";
import { Image as ViewerImage } from "../viewer/image.js";
const Image = ({
  node,
  editor,
  getPos,
  selected,
  updateAttributes
}) => {
  const { t, isMobile } = useContext(RicosContext);
  const converters = useConverters();
  const containerRef = useRef(null);
  const [isHovering, setIsHovering] = useState(false);
  const [isDraggingHandle, setIsDraggingHandle] = useState(false);
  const selectedFigure = findFigureForImage({ editor, node, getPos });
  const { loading, loadingPercentage, error, tempData } = node.attrs;
  const { src } = node.attrs.image || {};
  const shouldRender = !!tempData || src?.url || src?.id;
  const ricosNode = fromPmImageAnyState(selectedFigure?.toJSON(), converters.fromPmNode);
  const helpers = useEditorProp((s) => s.helpers);
  const currentRadius = node.attrs.styles?.border?.radius ?? 0;
  const handleRadiusChange = useCallback(
    (radius) => {
      updateAttributes({
        styles: {
          ...node.attrs.styles,
          border: {
            ...node.attrs.styles?.border,
            radius
          }
        }
      });
    },
    [node.attrs.styles, updateAttributes]
  );
  return selectedFigure && ricosNode && shouldRender ? /* @__PURE__ */ React.createElement(
    "div",
    {
      ref: containerRef,
      className: getContainerClasses(editor, node.attrs.id, isMobile),
      style: { position: "relative" },
      draggable: true,
      "data-drag-handle": true,
      onPointerEnter: () => setIsHovering(true),
      onPointerLeave: () => setIsHovering(false)
    },
    /* @__PURE__ */ React.createElement(
      ViewerImage,
      {
        isMobile,
        node: ricosNode,
        dataUrl: tempData,
        imageClickActionConfig: { type: "regular" },
        shouldLazyLoadImages: false,
        helpers
      }
    ),
    selected && (isHovering || isDraggingHandle) && /* @__PURE__ */ React.createElement(
      CornerRadiusHandles,
      {
        containerRef,
        currentRadius,
        onRadiusChange: handleRadiusChange,
        onDraggingChange: setIsDraggingHandle
      }
    ),
    loading && /* @__PURE__ */ React.createElement(Loader, { type: "medium", percent: loadingPercentage }),
    error && /* @__PURE__ */ React.createElement(MediaItemErrorMsg, { error, t })
  ) : null;
};
function findFigureForImage({
  editor,
  node,
  getPos
}) {
  if (getPos) {
    try {
      const pos = getPos();
      const doc = editor.state.doc;
      const $pos = doc.resolve(pos);
      for (let depth = $pos.depth; depth >= 0; depth--) {
        const parent = $pos.node(depth);
        if (parent.type.name === TIPTAP_FIGURE_TYPE) {
          return parent;
        }
      }
    } catch {
    }
  }
  const figures = editor.$nodes(TIPTAP_FIGURE_TYPE);
  const figureNodeWithImageId = figures?.find((nodePos) => {
    return nodePos.node.firstChild?.attrs.id === node.attrs.id;
  });
  return figureNodeWithImageId?.node ?? null;
}
export {
  Image
};
//# sourceMappingURL=image.js.map
