"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 gradient_preview_exports = {};
__export(gradient_preview_exports, {
  GradientPreview: () => GradientPreview
});
module.exports = __toCommonJS(gradient_preview_exports);
var import_design_system = require("@wix/design-system");
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_schema_gradient_to_css = require("./../../../../../_shared/schema-gradient-to-css.js");
var import_gradient_picker_scss = __toESM(require("./gradient-picker-scss.js"));
const GradientPreview = ({ value, onChange }) => {
  const [isDragging, setIsDragging] = (0, import_react.useState)(false);
  const handleMouseUp = () => {
    setIsDragging(false);
  };
  (0, import_react.useEffect)(() => {
    if (isDragging) {
      document.addEventListener("mouseup", handleMouseUp);
    }
    return () => document.removeEventListener("mouseup", handleMouseUp);
  }, [isDragging]);
  const handleMouseDown = (event) => {
    event.preventDefault();
    event.stopPropagation();
    setIsDragging(true);
  };
  const handleMouseMove = (event) => {
    if (isDragging && value.type === "RADIAL") {
      const rect = event.currentTarget.getBoundingClientRect();
      const x = event.clientX - rect.left;
      const y = event.clientY - rect.top;
      const centerX = Math.round(Math.max(0, Math.min(100, x / rect.width * 100)));
      const centerY = Math.round(Math.max(0, Math.min(100, y / rect.height * 100)));
      onChange({ ...value, centerX, centerY });
    }
  };
  const handleCenterKeyDown = (event) => {
    if (value.type !== "RADIAL") {
      return;
    }
    let x = value.centerX ?? 50;
    let y = value.centerY ?? 50;
    const step = 1;
    let changed = false;
    switch (event.key) {
      case "ArrowLeft":
        x = Math.max(0, x - step);
        changed = true;
        break;
      case "ArrowRight":
        x = Math.min(100, x + step);
        changed = true;
        break;
      case "ArrowUp":
        y = Math.max(0, y - step);
        changed = true;
        break;
      case "ArrowDown":
        y = Math.min(100, y + step);
        changed = true;
        break;
      default:
        return;
    }
    if (changed) {
      event.preventDefault();
      onChange({ ...value, centerX: x, centerY: y });
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)(import_gradient_picker_scss.default.preview, {
        [import_gradient_picker_scss.default.radial]: value.type === "RADIAL"
      }),
      style: {
        "--gp-gradient": (0, import_schema_gradient_to_css.schemaGradientToCss)(value)
      },
      onMouseDown: handleMouseDown,
      onMouseMove: handleMouseMove
    },
    value.type === "LINEAR" && /* @__PURE__ */ import_react.default.createElement(
      import_design_system.AngleInput,
      {
        value: value.angle ?? 90,
        step: 1,
        onChange: (angle) => onChange({ ...value, angle })
      }
    ),
    value.type === "RADIAL" && /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        role: "button",
        tabIndex: 0,
        "aria-live": "polite",
        className: import_gradient_picker_scss.default.radialCenter,
        style: {
          "--gp-radial-center-x": `${value.centerX ?? 50}%`,
          "--gp-radial-center-y": `${value.centerY ?? 50}%`
        },
        onKeyDown: handleCenterKeyDown
      }
    )
  );
};
//# sourceMappingURL=gradient-preview.js.map
