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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

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; }

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import Checkbox from '../../checkbox';
import Search from '../../search';
import Menu from '../../menu';
import { func, htmlId } from '../../util';
import TransferItem from './transfer-item';
var bindCtx = func.bindCtx;

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

  function TransferPanel(props, context) {
    var _this;

    _classCallCheck(this, TransferPanel);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(TransferPanel).call(this, props, context));
    _this.state = {
      searchedValue: '',
      dragValue: null,
      dragOverValue: null
    };
    _this.footerId = props.baseId ? htmlId.escapeForId("".concat(props.baseId, "-panel-footer-").concat(props.position)) : '';
    _this.headerId = props.baseId ? htmlId.escapeForId("".concat(props.baseId, "-panel-header-").concat(props.position)) : '';
    bindCtx(_assertThisInitialized(_this), ['handleCheck', 'handleAllCheck', 'handleSearch', 'handleItemDragStart', 'handleItemDragOver', 'handleItemDragEnd', 'handleItemDrop', 'getListDOM']);
    _this.firstRender = true;
    return _this;
  }

  _createClass(TransferPanel, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.firstRender = false;
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      if (prevProps.dataSource.length !== this.props.dataSource.length && this.list) {
        if (this.list.scrollTop > 0) {
          this.list.scrollTop = 0;
        }
      }

      this.searched = false;
    }
  }, {
    key: "getListDOM",
    value: function getListDOM(ref) {
      this.list = ref;
    }
  }, {
    key: "handleAllCheck",
    value: function handleAllCheck(allChecked) {
      var _this$props = this.props,
          position = _this$props.position,
          onChange = _this$props.onChange;
      var newValue;

      if (allChecked) {
        newValue = this.enabledDatasource.map(function (item) {
          return item.value;
        });
      } else {
        newValue = [];
      }

      onChange && onChange(position, newValue);
    }
  }, {
    key: "handleCheck",
    value: function handleCheck(itemValue, checked) {
      var _this$props2 = this.props,
          position = _this$props2.position,
          value = _this$props2.value,
          onChange = _this$props2.onChange;

      var newValue = _toConsumableArray(value);

      var index = value.indexOf(itemValue);

      if (checked && index === -1) {
        newValue.push(itemValue);
      } else if (!checked && index > -1) {
        newValue.splice(index, 1);
      }

      onChange && onChange(position, newValue);
    }
  }, {
    key: "handleSearch",
    value: function handleSearch(searchedValue) {
      this.setState({
        searchedValue: searchedValue
      });
      this.searched = true;
      var _this$props3 = this.props,
          onSearch = _this$props3.onSearch,
          position = _this$props3.position;
      onSearch(searchedValue, position);
    }
  }, {
    key: "handleItemDragStart",
    value: function handleItemDragStart(position, value) {
      this.setState({
        dragPosition: position,
        dragValue: value
      });
    }
  }, {
    key: "handleItemDragOver",
    value: function handleItemDragOver(value) {
      this.setState({
        dragOverValue: value
      });
    }
  }, {
    key: "handleItemDragEnd",
    value: function handleItemDragEnd() {
      this.setState({
        dragOverValue: null
      });
    }
  }, {
    key: "handleItemDrop",
    value: function handleItemDrop() {
      var _this$props4;

      this.setState({
        dragOverValue: null
      });

      (_this$props4 = this.props).onSort.apply(_this$props4, arguments);
    }
  }, {
    key: "renderHeader",
    value: function renderHeader() {
      var _this$props5 = this.props,
          title = _this$props5.title,
          prefix = _this$props5.prefix;
      return React.createElement("div", {
        id: this.headerId,
        className: "".concat(prefix, "transfer-panel-header")
      }, title);
    }
  }, {
    key: "renderSearch",
    value: function renderSearch() {
      var _this$props6 = this.props,
          prefix = _this$props6.prefix,
          searchPlaceholder = _this$props6.searchPlaceholder,
          locale = _this$props6.locale;
      return React.createElement(Search, {
        "aria-labelledby": this.headerId,
        shape: "simple",
        className: "".concat(prefix, "transfer-panel-search"),
        placeholder: searchPlaceholder || locale.searchPlaceholder,
        onChange: this.handleSearch
      });
    }
  }, {
    key: "renderList",
    value: function renderList(dataSource) {
      var _cx,
          _this2 = this;

      var _this$props7 = this.props,
          prefix = _this$props7.prefix,
          position = _this$props7.position,
          mode = _this$props7.mode,
          value = _this$props7.value,
          onMove = _this$props7.onMove,
          disabled = _this$props7.disabled,
          listClassName = _this$props7.listClassName,
          listStyle = _this$props7.listStyle,
          itemRender = _this$props7.itemRender,
          sortable = _this$props7.sortable,
          customerList = _this$props7.customerList;
      var _this$state = this.state,
          dragPosition = _this$state.dragPosition,
          dragValue = _this$state.dragValue,
          dragOverValue = _this$state.dragOverValue;
      var newClassName = cx((_cx = {}, _defineProperty(_cx, "".concat(prefix, "transfer-panel-list"), true), _defineProperty(_cx, listClassName, !!listClassName), _cx));
      var customerPanel = customerList && customerList(this.props);

      if (customerPanel) {
        return React.createElement("div", {
          className: newClassName,
          style: listStyle,
          ref: this.getListDOM
        }, customerPanel);
      }

      return dataSource.length ? React.createElement(Menu, {
        className: newClassName,
        style: listStyle,
        ref: this.getListDOM
      }, dataSource.map(function (item) {
        return React.createElement(TransferItem, {
          key: item.value,
          prefix: prefix,
          mode: mode,
          checked: value.indexOf(item.value) > -1,
          disabled: disabled || item.disabled,
          item: item,
          onCheck: _this2.handleCheck,
          onClick: onMove,
          needHighlight: !_this2.firstRender && !_this2.searched,
          itemRender: itemRender,
          draggable: sortable,
          onDragStart: _this2.handleItemDragStart,
          onDragOver: _this2.handleItemDragOver,
          onDragEnd: _this2.handleItemDragEnd,
          onDrop: _this2.handleItemDrop,
          dragPosition: dragPosition,
          dragValue: dragValue,
          dragOverValue: dragOverValue,
          panelPosition: position
        });
      })) : React.createElement("div", {
        className: newClassName,
        style: listStyle
      }, this.renderNotFoundContent());
    }
  }, {
    key: "renderNotFoundContent",
    value: function renderNotFoundContent() {
      var _this$props8 = this.props,
          prefix = _this$props8.prefix,
          notFoundContent = _this$props8.notFoundContent;
      return React.createElement("div", {
        className: "".concat(prefix, "transfer-panel-not-found-container")
      }, React.createElement("div", {
        className: "".concat(prefix, "transfer-panel-not-found")
      }, notFoundContent));
    }
  }, {
    key: "renderFooter",
    value: function renderFooter() {
      var _this$props9 = this.props,
          prefix = _this$props9.prefix,
          position = _this$props9.position,
          mode = _this$props9.mode,
          disabled = _this$props9.disabled,
          locale = _this$props9.locale,
          showCheckAll = _this$props9.showCheckAll;

      if (mode === 'simple') {
        var _cx2;

        var onMoveAll = this.props.onMoveAll;
        var classNames = cx((_cx2 = {}, _defineProperty(_cx2, "".concat(prefix, "transfer-panel-move-all"), true), _defineProperty(_cx2, "".concat(prefix, "disabled"), disabled), _cx2));
        return React.createElement("div", {
          className: "".concat(prefix, "transfer-panel-footer")
        }, React.createElement("a", {
          className: classNames,
          onClick: onMoveAll.bind(this, position === 'left' ? 'right' : 'left')
        }, locale.moveAll));
      }

      var _this$props10 = this.props,
          value = _this$props10.value,
          dataSource = _this$props10.dataSource;
      var checkedCount = value.length;
      var totalCount = dataSource.length;
      var totalEnabledCount = this.enabledDatasource.length;
      var checked = checkedCount > 0 && checkedCount >= totalEnabledCount;
      var indeterminate = checkedCount > 0 && checkedCount < totalEnabledCount;
      var items = totalCount > 1 ? locale.items : locale.item;
      var countLabel = checkedCount === 0 ? "".concat(totalCount, " ").concat(items) : "".concat(checkedCount, "/").concat(totalCount, " ").concat(items);
      return React.createElement("div", {
        className: "".concat(prefix, "transfer-panel-footer")
      }, showCheckAll && React.createElement(Checkbox, {
        disabled: disabled,
        checked: checked,
        indeterminate: indeterminate,
        onChange: this.handleAllCheck,
        "aria-labelledby": this.footerId
      }), React.createElement("span", {
        className: "".concat(prefix, "transfer-panel-count"),
        id: this.footerId
      }, countLabel));
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props11 = this.props,
          prefix = _this$props11.prefix,
          title = _this$props11.title,
          showSearch = _this$props11.showSearch,
          filter = _this$props11.filter;
      var searchedValue = this.state.searchedValue;
      var dataSource = this.props.dataSource;
      this.enabledDatasource = dataSource.filter(function (item) {
        return !item.disabled;
      });

      if (showSearch && searchedValue) {
        dataSource = dataSource.filter(function (item) {
          return filter(searchedValue, item);
        });
      }

      return React.createElement("div", {
        className: "".concat(prefix, "transfer-panel")
      }, title ? this.renderHeader() : null, showSearch ? this.renderSearch() : null, this.renderList(dataSource), this.renderFooter());
    }
  }]);

  return TransferPanel;
}(Component);

_defineProperty(TransferPanel, "propTypes", {
  prefix: PropTypes.string,
  position: PropTypes.oneOf(['left', 'right']),
  mode: PropTypes.oneOf(['normal', 'simple']),
  dataSource: PropTypes.array,
  value: PropTypes.array,
  onChange: PropTypes.func,
  onMove: PropTypes.func,
  onMoveAll: PropTypes.func,
  disabled: PropTypes.bool,
  locale: PropTypes.object,
  title: PropTypes.node,
  showSearch: PropTypes.bool,
  filter: PropTypes.func,
  onSearch: PropTypes.func,
  searchPlaceholder: PropTypes.string,
  notFoundContent: PropTypes.node,
  listClassName: PropTypes.string,
  listStyle: PropTypes.object,
  itemRender: PropTypes.func,
  sortable: PropTypes.bool,
  onSort: PropTypes.func,
  baseId: PropTypes.string,
  customerList: PropTypes.func,
  showCheckAll: PropTypes.bool
});

export { TransferPanel as default };