import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import { transformZoomData } from 'choerodon-ui/shared/util';
import { cloneElement, memo, useCallback, useState } from 'react';

function wrapEvent(element, eventName, callback) {
  return function (e) {
    var originalEvent = element.props[eventName];

    if (originalEvent) {
      originalEvent(e);
    }

    callback(e);
  };
}

var MouseDown = function MouseDown(props) {
  var _useState = useState(),
      _useState2 = _slicedToArray(_useState, 2),
      size = _useState2[0],
      setSize = _useState2[1];

  var show = useCallback(function (e) {
    var currentTarget = e.currentTarget;
    var pos = currentTarget.getBoundingClientRect();
    var ownerDocument = currentTarget.ownerDocument;
    var defaultView = ownerDocument && ownerDocument.defaultView;
    setSize({
      x: transformZoomData(e.clientX) - pos.left,
      y: transformZoomData(e.clientY) - pos.top,
      width: currentTarget.clientWidth,
      height: currentTarget.clientHeight,
      position: defaultView ? defaultView.getComputedStyle(currentTarget).position : null
    });
  }, []);
  var hide = useCallback(function () {
    setSize(undefined);
  }, []);
  var children = props.children,
      rippleChild = props.rippleChild;
  var element = children(rippleChild, size);
  var newProps = {
    onMouseDown: wrapEvent(element, 'onMouseDown', show)
  };

  if (size) {
    _extends(newProps, {
      onMouseUp: wrapEvent(element, 'onMouseUp', hide),
      onMouseLeave: wrapEvent(element, 'onMouseLeave', hide),
      onDragEnd: wrapEvent(element, 'onDragEnd', hide)
    });
  }

  return /*#__PURE__*/cloneElement(element, newProps);
};

MouseDown.displayName = 'MouseDown';
export default /*#__PURE__*/memo(MouseDown);
//# sourceMappingURL=MouseDown.js.map
