"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 corner_radius_handles_exports = {};
__export(corner_radius_handles_exports, {
  CornerRadiusHandles: () => CornerRadiusHandles
});
module.exports = __toCommonJS(corner_radius_handles_exports);
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../ricos-context/index.js");
var import_get_element_scale = require("./../../_shared/get-element-scale.js");
var import_corner_radius_handles_scss = __toESM(require("./corner-radius-handles-scss.js"));
const MAX_RADIUS = 300;
const MIN_RADIUS = 0;
const HANDLE_SIZE = 8;
const HANDLE_INSET = 8;
const CENTER_GAP = 8;
const SHIFT_SNAP_STEP = 20;
const clampRadius = (value, upperBound) => Math.round(Math.max(MIN_RADIUS, Math.min(upperBound, value)));
const snapToStep = (value, step) => Math.round(value / step) * step;
const CornerRadiusHandles = ({
  containerRef,
  currentRadius,
  onRadiusChange,
  onDraggingChange
}) => {
  const { t } = (0, import_react.useContext)(import_ricos_context.RicosContext) ?? {
    t: (key, _opts) => _opts?.value !== void 0 ? `Radius ${_opts.value}` : key
  };
  const [draggingCorner, setDraggingCorner] = (0, import_react.useState)(null);
  const dragCornerRef = (0, import_react.useRef)(null);
  const computeRadiusFromPoint = (0, import_react.useCallback)(
    (clientX, clientY, corner) => {
      const el = containerRef.current;
      if (!el) {
        return currentRadius;
      }
      const rect2 = el.getBoundingClientRect();
      const { scaleX, scaleY } = (0, import_get_element_scale.getElementScale)(el);
      const x = (clientX - rect2.left) / scaleX;
      const y = (clientY - rect2.top) / scaleY;
      const width2 = rect2.width / scaleX;
      const height2 = rect2.height / scaleY;
      const halfHandle2 = HANDLE_SIZE / 2;
      const originOffset = HANDLE_INSET + halfHandle2;
      const maxPhysical2 = Math.max(
        0,
        Math.min(width2 / 2 - originOffset - CENTER_GAP, height2 / 2 - originOffset - CENTER_GAP)
      );
      if (maxPhysical2 <= 0) {
        return currentRadius;
      }
      let physicalRadius;
      switch (corner) {
        case "topLeft":
          physicalRadius = Math.max(0, Math.min(x - originOffset, y - originOffset));
          break;
        case "topRight":
          physicalRadius = Math.max(0, Math.min(width2 - originOffset - x, y - originOffset));
          break;
        case "bottomLeft":
          physicalRadius = Math.max(0, Math.min(x - originOffset, height2 - originOffset - y));
          break;
        case "bottomRight":
          physicalRadius = Math.max(
            0,
            Math.min(width2 - originOffset - x, height2 - originOffset - y)
          );
          break;
        default:
          return currentRadius;
      }
      const normalized = Math.min(1, physicalRadius / maxPhysical2);
      return clampRadius(Math.round(normalized * MAX_RADIUS), MAX_RADIUS);
    },
    [containerRef, currentRadius]
  );
  const handlePointerDown = (0, import_react.useCallback)(
    (event, corner) => {
      event.preventDefault();
      event.stopPropagation();
      dragCornerRef.current = corner;
      setDraggingCorner(corner);
      onDraggingChange?.(true);
      event.target.setPointerCapture(event.pointerId);
    },
    [onDraggingChange]
  );
  const handlePointerMove = (0, import_react.useCallback)(
    (event, corner) => {
      if (dragCornerRef.current === null) {
        return;
      }
      event.preventDefault();
      let newRadius = computeRadiusFromPoint(event.clientX, event.clientY, corner);
      if (event.shiftKey) {
        newRadius = snapToStep(newRadius, SHIFT_SNAP_STEP);
      }
      onRadiusChange(newRadius);
    },
    [computeRadiusFromPoint, onRadiusChange]
  );
  const handlePointerUp = (0, import_react.useCallback)(
    (event) => {
      dragCornerRef.current = null;
      setDraggingCorner(null);
      onDraggingChange?.(false);
      event.target.releasePointerCapture(event.pointerId);
    },
    [onDraggingChange]
  );
  const rect = containerRef.current?.getBoundingClientRect();
  const width = rect?.width ?? 0;
  const height = rect?.height ?? 0;
  const halfHandle = HANDLE_SIZE / 2;
  const maxPhysical = Math.max(
    0,
    Math.min(
      width / 2 - HANDLE_INSET - halfHandle - CENTER_GAP,
      height / 2 - HANDLE_INSET - halfHandle - CENTER_GAP
    )
  );
  const radiusClamped = Math.min(Math.max(currentRadius, MIN_RADIUS), MAX_RADIUS);
  const effectiveRadius = maxPhysical > 0 ? radiusClamped / MAX_RADIUS * maxPhysical : 0;
  const LABEL_OFFSET_HORIZONTAL = 6;
  const LABEL_OFFSET_VERTICAL = 4;
  const handlePositions = {
    topLeft: {
      left: HANDLE_INSET + effectiveRadius,
      top: HANDLE_INSET + effectiveRadius
    },
    topRight: {
      right: HANDLE_INSET + effectiveRadius,
      top: HANDLE_INSET + effectiveRadius
    },
    bottomLeft: {
      left: HANDLE_INSET + effectiveRadius,
      bottom: HANDLE_INSET + effectiveRadius
    },
    bottomRight: {
      right: HANDLE_INSET + effectiveRadius,
      bottom: HANDLE_INSET + effectiveRadius
    }
  };
  const radiusLabelPosition = {
    topLeft: {
      left: HANDLE_INSET + effectiveRadius + LABEL_OFFSET_HORIZONTAL,
      top: HANDLE_INSET + effectiveRadius - LABEL_OFFSET_VERTICAL,
      transform: "translateY(-100%)"
    },
    topRight: {
      right: HANDLE_INSET + effectiveRadius + LABEL_OFFSET_HORIZONTAL,
      top: HANDLE_INSET + effectiveRadius - LABEL_OFFSET_VERTICAL,
      transform: "translateY(-100%)"
    },
    bottomLeft: {
      left: HANDLE_INSET + effectiveRadius + LABEL_OFFSET_HORIZONTAL,
      bottom: HANDLE_INSET + effectiveRadius + HANDLE_SIZE + LABEL_OFFSET_VERTICAL
    },
    bottomRight: {
      right: HANDLE_INSET + effectiveRadius + LABEL_OFFSET_HORIZONTAL,
      bottom: HANDLE_INSET + effectiveRadius + HANDLE_SIZE + LABEL_OFFSET_VERTICAL
    }
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: import_corner_radius_handles_scss.default.overlay, "aria-hidden": true }, draggingCorner !== null && /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: import_corner_radius_handles_scss.default.radiusLabel,
      style: radiusLabelPosition[draggingCorner],
      "data-hook": "image-corner-radius-label"
    },
    t("ImagePlugin_Corner_Radius_Label", { value: currentRadius })
  ), ["topLeft", "topRight", "bottomLeft", "bottomRight"].map((corner) => /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      key: corner,
      className: import_corner_radius_handles_scss.default.handle,
      "data-corner": corner,
      "data-hook": "image-corner-radius-handle",
      style: handlePositions[corner],
      onPointerDown: (e) => handlePointerDown(e, corner),
      onPointerMove: (e) => handlePointerMove(e, corner),
      onPointerUp: handlePointerUp,
      onPointerCancel: handlePointerUp,
      role: "slider",
      "aria-valuenow": currentRadius,
      "aria-valuemin": MIN_RADIUS,
      "aria-valuemax": MAX_RADIUS
    },
    /* @__PURE__ */ import_react.default.createElement("span", { className: import_corner_radius_handles_scss.default.handleDot })
  )));
};
//# sourceMappingURL=corner-radius-handles.js.map
