/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
var defaultsOptions = {
  start: function start(coordinate, event) {},
  drag: function drag(coordinate, event) {},
  end: function end(coordinate, event) {}
};
var _dragStart = /*#__PURE__*/new WeakSet();
var _drag = /*#__PURE__*/new WeakSet();
var _dragEnd = /*#__PURE__*/new WeakSet();
var _getCoordinate = /*#__PURE__*/new WeakSet();
var Draggable = /*#__PURE__*/function () {
  function Draggable(el, options) {
    _classCallCheck__default["default"](this, Draggable);
    _classPrivateMethodInitSpec(this, _getCoordinate);
    _classPrivateMethodInitSpec(this, _dragEnd);
    _classPrivateMethodInitSpec(this, _drag);
    _classPrivateMethodInitSpec(this, _dragStart);
    _defineProperty__default["default"](this, "dragging", false);
    _defineProperty__default["default"](this, "$el", void 0);
    _defineProperty__default["default"](this, "props", void 0);
    _defineProperty__default["default"](this, "handles", void 0);
    this.$el = el;
    this.props = _objectSpread(_objectSpread({}, defaultsOptions), options);
    this.handles = {
      start: _classPrivateMethodGet(this, _dragStart, _dragStart2).bind(this),
      drag: _classPrivateMethodGet(this, _drag, _drag2).bind(this),
      end: _classPrivateMethodGet(this, _dragEnd, _dragEnd2).bind(this)
    };
    this.$el.addEventListener("mousedown", this.handles.start, false);
  }
  _createClass__default["default"](Draggable, [{
    key: "destroy",
    value: function destroy() {
      this.$el.removeEventListener("mousedown", this.handles.start, false);
      window.removeEventListener("mousemove", this.handles.drag, false);
      window.removeEventListener("mouseup", this.handles.end, false);
      window.removeEventListener("contextmenu", this.handles.end, false);
    }
  }]);
  return Draggable;
}();
function _dragStart2(event) {
  if (this.dragging) {
    return;
  }
  window.addEventListener("mousemove", this.handles.drag, false);
  window.addEventListener("mouseup", this.handles.end, false);
  window.addEventListener("contextmenu", this.handles.end, false);
  this.dragging = true;
  this.props.start(_classPrivateMethodGet(this, _getCoordinate, _getCoordinate2).call(this, event), event);
}
function _drag2(event) {
  if (!this.dragging) {
    return;
  }
  this.props.drag(_classPrivateMethodGet(this, _getCoordinate, _getCoordinate2).call(this, event), event);
}
function _dragEnd2(event) {
  var _this = this;
  setTimeout(function () {
    _this.dragging = false;
    _this.props.end(_classPrivateMethodGet(_this, _getCoordinate, _getCoordinate2).call(_this, event), event);
  }, 0);
  window.removeEventListener("mousemove", this.handles.drag, false);
  window.removeEventListener("mouseup", this.handles.end, false);
  window.removeEventListener("contextmenu", this.handles.end, false);
}
function _getCoordinate2(event) {
  var rect = this.$el.getBoundingClientRect();
  var mouseEvent = event;
  var left = mouseEvent.clientX - rect.left;
  var top = mouseEvent.clientY - rect.top;
  return {
    y: Math.min(Math.max(0, top), rect.height),
    x: Math.min(Math.max(0, left), rect.width)
  };
}

exports.Draggable = Draggable;
exports["default"] = Draggable;
//# sourceMappingURL=draggable.js.map
