"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _menu = _interopRequireDefault(require("../../menu"));

var _util = require("../../util");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var Item = _menu["default"].Item,
    CheckboxItem = _menu["default"].CheckboxItem;
var bindCtx = _util.func.bindCtx;
var pickOthers = _util.obj.pickOthers;
var getOffset = _util.dom.getOffset;

var TransferItem =
/*#__PURE__*/
function (_Component) {
  _inherits(TransferItem, _Component);

  function TransferItem(props) {
    var _this;

    _classCallCheck(this, TransferItem);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(TransferItem).call(this, props));
    _this.state = {
      highlight: false
    };
    bindCtx(_assertThisInitialized(_this), ['getItemDOM', 'handleClick', 'handleDragStart', 'handleDragOver', 'handleDragEnd', 'handleDrop']);
    return _this;
  }

  _createClass(TransferItem, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      if (this.props.needHighlight) {
        this.addHighlightTimer = setTimeout(function () {
          _this2.setState({
            highlight: true
          });
        }, 1);
        this.removeHighlightTimer = setTimeout(function () {
          _this2.setState({
            highlight: false
          });
        }, 201);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      clearTimeout(this.addHighlightTimer);
      clearTimeout(this.removeHighlightTimer);
    }
  }, {
    key: "getItemDOM",
    value: function getItemDOM(ref) {
      this.item = ref;
    }
  }, {
    key: "handleClick",
    value: function handleClick() {
      var _this$props = this.props,
          onClick = _this$props.onClick,
          panelPosition = _this$props.panelPosition,
          item = _this$props.item;
      onClick(panelPosition === 'left' ? 'right' : 'left', item.value);
    }
  }, {
    key: "handleDragStart",
    value: function handleDragStart() {
      var _this$props2 = this.props,
          onDragStart = _this$props2.onDragStart,
          panelPosition = _this$props2.panelPosition,
          item = _this$props2.item;
      onDragStart(panelPosition, item.value);
    }
  }, {
    key: "getDragGap",
    value: function getDragGap(e) {
      var referenceTop = getOffset(e.currentTarget).top;
      var referenceHeight = e.currentTarget.offsetHeight;
      return e.pageY <= referenceTop + referenceHeight / 2 ? 'before' : 'after';
    }
  }, {
    key: "handleDragOver",
    value: function handleDragOver(e) {
      var _this$props3 = this.props,
          panelPosition = _this$props3.panelPosition,
          dragPosition = _this$props3.dragPosition,
          onDragOver = _this$props3.onDragOver,
          item = _this$props3.item;

      if (panelPosition === dragPosition) {
        e.preventDefault();
        var dragGap = this.getDragGap(e);

        if (this.dragGap !== dragGap) {
          this.dragGap = dragGap;
          onDragOver(item.value);
        }
      }
    }
  }, {
    key: "handleDragEnd",
    value: function handleDragEnd() {
      var onDragEnd = this.props.onDragEnd;
      onDragEnd();
    }
  }, {
    key: "handleDrop",
    value: function handleDrop(e) {
      e.preventDefault();
      var _this$props4 = this.props,
          onDrop = _this$props4.onDrop,
          item = _this$props4.item,
          panelPosition = _this$props4.panelPosition,
          dragValue = _this$props4.dragValue;
      onDrop(panelPosition, dragValue, item.value, this.dragGap);
    }
  }, {
    key: "render",
    value: function render() {
      var _cx;

      var _this$props5 = this.props,
          prefix = _this$props5.prefix,
          mode = _this$props5.mode,
          checked = _this$props5.checked,
          disabled = _this$props5.disabled,
          item = _this$props5.item,
          onCheck = _this$props5.onCheck,
          itemRender = _this$props5.itemRender,
          draggable = _this$props5.draggable,
          dragOverValue = _this$props5.dragOverValue,
          panelPosition = _this$props5.panelPosition,
          dragPosition = _this$props5.dragPosition;
      var others = pickOthers(Object.keys(TransferItem.propTypes), this.props);
      var highlight = this.state.highlight;
      var isSimple = mode === 'simple';
      var classNames = (0, _classnames["default"])((_cx = {}, _defineProperty(_cx, "".concat(prefix, "transfer-panel-item"), true), _defineProperty(_cx, "".concat(prefix, "insert-").concat(this.dragGap), dragOverValue === item.value && panelPosition === dragPosition), _defineProperty(_cx, "".concat(prefix, "focused"), highlight), _defineProperty(_cx, "".concat(prefix, "simple"), isSimple), _cx));

      var itemProps = _objectSpread({
        ref: this.getItemDOM,
        className: classNames,
        children: itemRender(item),
        disabled: disabled,
        draggable: draggable && !disabled,
        onDragStart: this.handleDragStart,
        onDragOver: this.handleDragOver,
        onDragEnd: this.handleDragEnd,
        onDrop: this.handleDrop
      }, others);

      if (isSimple) {
        if (!itemProps.disabled) {
          itemProps.onClick = this.handleClick;
        }

        return _react["default"].createElement(Item, itemProps);
      }

      return _react["default"].createElement(CheckboxItem, _extends({
        checked: checked,
        onChange: onCheck.bind(this, item.value)
      }, itemProps));
    }
  }]);

  return TransferItem;
}(_react.Component);

exports["default"] = TransferItem;

_defineProperty(TransferItem, "menuChildType", CheckboxItem.menuChildType);

_defineProperty(TransferItem, "propTypes", {
  prefix: _propTypes["default"].string,
  mode: _propTypes["default"].oneOf(['normal', 'simple']),
  value: _propTypes["default"].array,
  disabled: _propTypes["default"].bool,
  item: _propTypes["default"].object,
  onCheck: _propTypes["default"].func,
  onClick: _propTypes["default"].func,
  needHighlight: _propTypes["default"].bool,
  itemRender: _propTypes["default"].func,
  draggable: _propTypes["default"].bool,
  onDragStart: _propTypes["default"].func,
  onDragOver: _propTypes["default"].func,
  onDragEnd: _propTypes["default"].func,
  onDrop: _propTypes["default"].func,
  dragPosition: _propTypes["default"].oneOf(['left', 'right']),
  dragValue: _propTypes["default"].string,
  dragOverValue: _propTypes["default"].string,
  panelPosition: _propTypes["default"].oneOf(['left', 'right'])
});