"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var image_exports = {};
__export(image_exports, {
  Image: () => Image
});
module.exports = __toCommonJS(image_exports);
var import_react = __toESM(require("react"));
var import_context = require("./../../pm-converters/context.js");
var import_consts = require("./../../ricos-content/consts.js");
var import_ricos_context = require("./../../ricos-context/index.js");
var import_some_editor_props_context = require("./../../_shared/some-editor-props-context.js");
var import_ui_components = require("./../../ui-components/index.js");
var import_get_container_classes = require("../../_shared/figure/get-container-classes.js");
var import_corner_radius_handles = require("../components/corner-radius-handles.js");
var import_from_pm_image_any_state = require("../converter/from-pm-image-any-state.js");
var import_image = require("../viewer/image.js");
const Image = ({
  node,
  editor,
  getPos,
  selected,
  updateAttributes
}) => {
  const { t, isMobile } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const converters = (0, import_context.useConverters)();
  const containerRef = (0, import_react.useRef)(null);
  const [isHovering, setIsHovering] = (0, import_react.useState)(false);
  const [isDraggingHandle, setIsDraggingHandle] = (0, import_react.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 = (0, import_from_pm_image_any_state.fromPmImageAnyState)(selectedFigure?.toJSON(), converters.fromPmNode);
  const helpers = (0, import_some_editor_props_context.useEditorProp)((s) => s.helpers);
  const currentRadius = node.attrs.styles?.border?.radius ?? 0;
  const handleRadiusChange = (0, import_react.useCallback)(
    (radius) => {
      updateAttributes({
        styles: {
          ...node.attrs.styles,
          border: {
            ...node.attrs.styles?.border,
            radius
          }
        }
      });
    },
    [node.attrs.styles, updateAttributes]
  );
  return selectedFigure && ricosNode && shouldRender ? /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      ref: containerRef,
      className: (0, import_get_container_classes.getContainerClasses)(editor, node.attrs.id, isMobile),
      style: { position: "relative" },
      draggable: true,
      "data-drag-handle": true,
      onPointerEnter: () => setIsHovering(true),
      onPointerLeave: () => setIsHovering(false)
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_image.Image,
      {
        isMobile,
        node: ricosNode,
        dataUrl: tempData,
        imageClickActionConfig: { type: "regular" },
        shouldLazyLoadImages: false,
        helpers
      }
    ),
    selected && (isHovering || isDraggingHandle) && /* @__PURE__ */ import_react.default.createElement(
      import_corner_radius_handles.CornerRadiusHandles,
      {
        containerRef,
        currentRadius,
        onRadiusChange: handleRadiusChange,
        onDraggingChange: setIsDraggingHandle
      }
    ),
    loading && /* @__PURE__ */ import_react.default.createElement(import_ui_components.Loader, { type: "medium", percent: loadingPercentage }),
    error && /* @__PURE__ */ import_react.default.createElement(import_ui_components.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 === import_consts.TIPTAP_FIGURE_TYPE) {
          return parent;
        }
      }
    } catch {
    }
  }
  const figures = editor.$nodes(import_consts.TIPTAP_FIGURE_TYPE);
  const figureNodeWithImageId = figures?.find((nodePos) => {
    return nodePos.node.firstChild?.attrs.id === node.attrs.id;
  });
  return figureNodeWithImageId?.node ?? null;
}
//# sourceMappingURL=image.js.map
