import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import { __decorate } from "tslib";
import React, { Component } from 'react';
import defaultTo from 'lodash/defaultTo';
import isEmpty from 'lodash/isEmpty';
import isUndefined from 'lodash/isUndefined';
import isNil from 'lodash/isNil';
import sortBy from 'lodash/sortBy';
import { action, toJS } from 'mobx';
import { observer } from 'mobx-react';
import Tag from '../../../es/tag';
import { pxToRem } from '../../../es/_util/UnitConvertor';
import Animate from '../animate';
import Icon from '../icon';
import { $l } from '../locale-context';
import autobind from '../_util/autobind';
import { SelectionsPosition } from './enum';
export { SelectionsPosition };

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

  var _super = _createSuper(SelectionList);

  function SelectionList() {
    var _this;

    _classCallCheck(this, SelectionList);

    _this = _super.apply(this, arguments);

    _this.unSelect = function (record) {
      var _this$props = _this.props,
          dataSet = _this$props.dataSet,
          treeFlag = _this$props.treeFlag;

      if (treeFlag === 'Y') {
        dataSet.treeUnSelect(record);
      } else {
        dataSet.unSelect(record);
      }

      if (!isUndefined(record.parent)) {
        dataSet.unSelect(record.parent);
      }
    };

    _this.renderBelow = function () {
      var _this$props2 = _this.props,
          dataSet = _this$props2.dataSet,
          treeFlag = _this$props2.treeFlag,
          _this$props2$valueFie = _this$props2.valueField,
          valueField = _this$props2$valueFie === void 0 ? '' : _this$props2$valueFie,
          _this$props2$textFiel = _this$props2.textField,
          textField = _this$props2$textFiel === void 0 ? '' : _this$props2$textFiel,
          _this$props2$selectio = _this$props2.selectionProps,
          selectionProps = _this$props2$selectio === void 0 ? {} : _this$props2$selectio;
      var nodeRenderer = selectionProps.nodeRenderer;
      var records = (treeFlag === 'Y' ? dataSet.treeSelected : dataSet.selected).filter(function (record) {
        return !isNil(record.get(valueField));
      });

      if (isEmpty(records)) {
        return null;
      }

      var classString = "".concat(_this.prefixCls, "-selection-list-below");

      var animateChildren = _this.getRecords(records).map(function (record) {
        return /*#__PURE__*/React.createElement("li", {
          key: record.get(valueField),
          className: "".concat(classString, "-item")
        }, /*#__PURE__*/React.createElement(Tag, {
          closable: record.selectable,
          onClose: function onClose() {
            _this.unSelect(record);
          }
        }, nodeRenderer ? toJS(nodeRenderer(record)) : /*#__PURE__*/React.createElement("span", null, record.get(textField))));
      });

      return /*#__PURE__*/React.createElement("div", {
        className: classString
      }, /*#__PURE__*/React.createElement("p", {
        className: "".concat(classString, "-intro")
      }, $l('Lov', 'selection_tips', {
        count: /*#__PURE__*/React.createElement("b", {
          key: "count"
        }, records.length)
      })), /*#__PURE__*/React.createElement(Animate, {
        className: "".concat(classString, "-list"),
        transitionAppear: true,
        transitionName: "fade",
        component: "ul"
      }, animateChildren));
    };

    return _this;
  }

  _createClass(SelectionList, [{
    key: "prefixCls",
    get: function get() {
      var getProPrefixCls = this.props.context.getProPrefixCls;
      return getProPrefixCls('modal');
    }
  }, {
    key: "contentRef",
    value: function contentRef(node) {
      this.selectionNode = node;
    }
  }, {
    key: "getRecords",
    value: function getRecords(records) {
      var textField = this.props.textField;
      return sortBy(records, function (item) {
        return defaultTo(item.selectedTimestamp, -1);
      }, function (item) {
        return item.get(textField);
      });
    }
  }, {
    key: "renderSide",
    value: function renderSide() {
      var _this2 = this;

      var _this$props3 = this.props,
          dataSet = _this$props3.dataSet,
          treeFlag = _this$props3.treeFlag,
          _this$props3$valueFie = _this$props3.valueField,
          valueField = _this$props3$valueFie === void 0 ? '' : _this$props3$valueFie,
          _this$props3$textFiel = _this$props3.textField,
          textField = _this$props3$textFiel === void 0 ? '' : _this$props3$textFiel,
          _this$props3$selectio = _this$props3.selectionProps,
          selectionProps = _this$props3$selectio === void 0 ? {} : _this$props3$selectio;
      var nodeRenderer = selectionProps.nodeRenderer,
          placeholder = selectionProps.placeholder;
      var records = (treeFlag === 'Y' ? dataSet.treeSelected : dataSet.selected).filter(function (record) {
        return !isNil(record.get(valueField));
      });
      var isEmptyList = isEmpty(records);

      if (isEmptyList && !placeholder) {
        return null;
      }

      var classString = "".concat(this.prefixCls, "-selection-list");
      var animateChildren = this.getRecords(records).map(function (record) {
        return /*#__PURE__*/React.createElement("li", {
          key: record.get(valueField),
          className: "".concat(classString, "-item")
        }, nodeRenderer ? toJS(nodeRenderer(record)) : /*#__PURE__*/React.createElement("span", null, record.get(textField)), /*#__PURE__*/React.createElement(Icon, {
          type: "cancel",
          onClick: function onClick() {
            _this2.unSelect(record);
          }
        }));
      });
      return /*#__PURE__*/React.createElement("div", {
        className: "".concat(classString, "-container"),
        ref: this.contentRef
      }, /*#__PURE__*/React.createElement("p", {
        className: "".concat(classString, "-intro")
      }, isEmptyList ? placeholder : $l('Lov', 'selection_tips', {
        count: /*#__PURE__*/React.createElement("b", {
          key: "count"
        }, records.length)
      })), /*#__PURE__*/React.createElement(Animate, {
        className: "".concat(classString, "-list"),
        transitionAppear: true,
        transitionName: "slide-right",
        component: "ul"
      }, animateChildren));
    }
  }, {
    key: "updateModalStyle",
    value: function updateModalStyle() {
      if (!this.modalNode && this.selectionNode) {
        var offsetParent = this.selectionNode.offsetParent;

        if (this.selectionNode && offsetParent && offsetParent.parentNode) {
          var parentNode = offsetParent.parentNode;
          this.modalNode = parentNode;

          var _parentNode$getBoundi = parentNode.getBoundingClientRect(),
              width = _parentNode$getBoundi.width;

          _extends(parentNode.style, {
            width: pxToRem(width + 300, true)
          });
        }
      } else if (this.modalNode && !this.selectionNode) {
        _extends(this.modalNode.style, {
          width: pxToRem(this.modalNode.offsetWidth - 300, true)
        });

        this.modalNode = null;
      }
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      this.updateModalStyle();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      this.updateModalStyle();
    }
  }, {
    key: "render",
    value: function render() {
      var selectionsPosition = this.props.selectionsPosition;

      if (selectionsPosition === SelectionsPosition.side) {
        return this.renderSide();
      }

      if (selectionsPosition === SelectionsPosition.below) {
        return this.renderBelow();
      }

      return null;
    }
  }]);

  return SelectionList;
}(Component);

__decorate([autobind], SelectionList.prototype, "contentRef", null);

__decorate([action], SelectionList.prototype, "unSelect", void 0);

SelectionList = __decorate([observer], SelectionList);
export default SelectionList;
//# sourceMappingURL=SelectionList.js.map
