"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 color_stops_exports = {};
__export(color_stops_exports, {
  ColorStops: () => ColorStops
});
module.exports = __toCommonJS(color_stops_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_theme_color_token = require("./../../../../../_shared/theme-color-token.js");
var import_gradient_picker_utils = require("./gradient-picker-utils.js");
var import_gradient_picker_scss = __toESM(require("./gradient-picker-scss.js"));
const ColorStops = ({ value, onChange, onStopClick, canAddStop }) => {
  const containerRef = (0, import_react.useRef)(null);
  const wasDraggingRef = (0, import_react.useRef)(false);
  const handleAddStopAtPosition = (event) => {
    if (!canAddStop || wasDraggingRef.current) {
      wasDraggingRef.current = false;
      return;
    }
    const clickX = event.clientX;
    const containerRect = containerRef.current?.getBoundingClientRect();
    const containerX = containerRect?.left ?? 0;
    const clickXRelativeToContainer = clickX - containerX;
    const clickXRelativeToContainerPercentage = clickXRelativeToContainer / (containerRect?.width ?? 1) * 100;
    const newStops = [
      ...value.stops ?? [],
      {
        color: value.stops?.[value.stops.length - 1]?.color,
        position: (0, import_gradient_picker_utils.roundPosition)(clickXRelativeToContainerPercentage / 100)
      }
    ].sort((a, b) => (a.position ?? 0) - (b.position ?? 0));
    onChange({
      ...value,
      stops: newStops
    });
  };
  return /* @__PURE__ */ import_react.default.createElement(import_design_system.Box, { paddingTop: "SP3", paddingLeft: "SP1", paddingRight: "SP1" }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: import_gradient_picker_scss.default.stops,
      ref: containerRef,
      role: "presentation",
      "aria-label": "Gradient stops bar. Click to add a stop or use the Add button.",
      style: {
        "--gp-stops-gradient": (0, import_schema_gradient_to_css.schemaGradientToCss)({
          type: "LINEAR",
          stops: value.stops,
          angle: 90
        })
      },
      onClick: handleAddStopAtPosition
    },
    value.stops?.map((stop, index) => /* @__PURE__ */ import_react.default.createElement(
      ColorStop,
      {
        key: index,
        stop,
        onChange: (newStop) => {
          onChange({
            ...value,
            stops: value.stops?.map((s, i) => i === index ? newStop : s)
          });
        },
        onStopClick: () => {
          onStopClick(index);
        },
        containerRef,
        wasDraggingRef
      }
    ))
  ));
};
const ColorStop = ({
  stop,
  onChange,
  containerRef,
  onStopClick,
  wasDraggingRef
}) => {
  const [isDragging, setIsDragging] = (0, import_react.useState)(false);
  const draggedDuringMouseDownRef = (0, import_react.useRef)(false);
  const stopRef = (0, import_react.useRef)(stop);
  const onChangeRef = (0, import_react.useRef)(onChange);
  stopRef.current = stop;
  onChangeRef.current = onChange;
  const handleKeyDown = (event) => {
    switch (event.key) {
      case "ArrowLeft": {
        event.preventDefault();
        const currentPosition = (stop.position ?? 0) * 100;
        const newPositionLeft = Math.max(0, currentPosition - 1);
        onChange({ ...stop, position: (0, import_gradient_picker_utils.roundPosition)(newPositionLeft / 100) });
        break;
      }
      case "ArrowRight": {
        event.preventDefault();
        const currentPosition = (stop.position ?? 0) * 100;
        const newPositionRight = Math.min(100, currentPosition + 1);
        onChange({ ...stop, position: (0, import_gradient_picker_utils.roundPosition)(newPositionRight / 100) });
        break;
      }
      case "Enter": {
        event.preventDefault();
        onStopClick();
        break;
      }
      default:
        break;
    }
  };
  (0, import_react.useEffect)(() => {
    if (!isDragging) {
      return;
    }
    const handleMouseMove = (event) => {
      if (containerRef.current) {
        draggedDuringMouseDownRef.current = true;
        const containerRect = containerRef.current.getBoundingClientRect();
        const x = event.clientX - containerRect.left;
        const centerX = Math.max(0, Math.min(100, x / containerRect.width * 100));
        onChangeRef.current({
          ...stopRef.current,
          position: (0, import_gradient_picker_utils.roundPosition)(centerX / 100)
        });
      }
    };
    const handleMouseUp = (event) => {
      event.stopPropagation();
      if (draggedDuringMouseDownRef.current) {
        wasDraggingRef.current = true;
      }
      setIsDragging(false);
    };
    document.addEventListener("mousemove", handleMouseMove);
    document.addEventListener("mouseup", handleMouseUp);
    return () => {
      document.removeEventListener("mousemove", handleMouseMove);
      document.removeEventListener("mouseup", handleMouseUp);
    };
  }, [isDragging]);
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      role: "button",
      tabIndex: 0,
      "aria-live": "polite",
      className: (0, import_classnames.default)(import_gradient_picker_scss.default.stopContainer, {
        [import_gradient_picker_scss.default.active]: isDragging
      }),
      style: { left: `${(stop.position ?? 0) * 100}%` },
      onClick: (event) => {
        event.stopPropagation();
        if (!draggedDuringMouseDownRef.current) {
          onStopClick();
        }
      },
      onMouseDown: (event) => {
        event.stopPropagation();
        setIsDragging(true);
        draggedDuringMouseDownRef.current = false;
      },
      onKeyDown: handleKeyDown
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: import_gradient_picker_scss.default.stopFillContainer }, /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: import_gradient_picker_scss.default.stop,
        style: { background: (0, import_theme_color_token.resolveDocumentColorToCss)(stop.color) ?? stop.color ?? void 0 }
      }
    )),
    /* @__PURE__ */ import_react.default.createElement("div", { className: import_gradient_picker_scss.default.stopArrow })
  );
};
//# sourceMappingURL=color-stops.js.map
