"use strict";

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

require("maycur-antd/lib/tag/style/css");

var _tag = _interopRequireDefault(require("maycur-antd/lib/tag"));

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

var _utils = _interopRequireDefault(require("../utils/utils"));

var _ManageSelectionModal = _interopRequireDefault(require("./ManageSelectionModal"));

var _constants = _interopRequireDefault(require("../utils/constants"));

var _lodash = _interopRequireDefault(require("lodash"));

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

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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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

var prefix = _utils["default"].prefixCls;
var MANAGE_PICKER = _constants["default"].MANAGE_PICKER;

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

  function ManagePicker(props) {
    var _this;

    _classCallCheck(this, ManagePicker);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(ManagePicker).call(this, props));
    _this.state = {
      // rc-form组件契约：接受value属性成为受控组件
      value: props.value || []
    };
    _this.onChange = _this.onChange.bind(_assertThisInitialized(_this));
    _this.openModal = _this.openModal.bind(_assertThisInitialized(_this));
    _this.convertFromValue = _this.convertFromValue.bind(_assertThisInitialized(_this));
    _this.convertToValue = _this.convertToValue.bind(_assertThisInitialized(_this));
    return _this;
  } // rc-form组件契约：注册onChange事件为form提供值变化感知


  _createClass(ManagePicker, [{
    key: "onChange",
    value: function onChange(value) {
      var onChange = this.props.onChange;

      if (typeof onChange === 'function') {
        onChange(value);
      }
    } // rc-form组件契约：接受value属性成为受控组件

  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if (_lodash["default"].isEqual(nextProps.value, this.props.value)) {
        return;
      } else {
        var value = nextProps.value;
        this.setState({
          value: value
        });
      }
    }
  }, {
    key: "convertFromValue",
    value: function convertFromValue(value) {
      value = value || [];
      return _lodash["default"].map(value, function (item) {
        return {
          code: item.code,
          name: item.name,
          type: item.type,
          userEmployeeId: item.userEmployeeId
        };
      });
    }
  }, {
    key: "convertToValue",
    value: function convertToValue(selectList) {
      selectList = selectList || [];
      return _lodash["default"].map(selectList, function (item) {
        return {
          code: item.code,
          name: item.name,
          type: item.type,
          userEmployeeId: item.userEmployeeId
        };
      });
    }
  }, {
    key: "openModal",
    value: function openModal(e) {
      var _this2 = this;

      var value = this.state.value;
      var _this$props = this.props,
          scope = _this$props.scope,
          title = _this$props.title,
          isShowUserGroup = _this$props.isShowUserGroup,
          maxSelectLength = _this$props.maxSelectLength,
          disabled = _this$props.disabled,
          fetchSourceList = _this$props.fetchSourceList,
          fetchSearchList = _this$props.fetchSearchList,
          nameAlias = _this$props.nameAlias,
          codeAlias = _this$props.codeAlias,
          isSwitch = _this$props.isSwitch,
          switchChecked = _this$props.switchChecked,
          switchDisabled = _this$props.switchDisabled,
          paging = _this$props.paging,
          pageSize = _this$props.pageSize,
          ModalManager = _this$props.ModalManager,
          convertValue = _this$props.convertValue;
      e.stopPropagation();

      if (disabled) {
        return;
      }

      ModalManager.open({
        component: _ManageSelectionModal["default"],
        componentProps: {
          selectList: convertValue && this.convertFromValue(value) || value,
          scope: scope,
          title: title,
          isShowUserGroup: isShowUserGroup,
          maxSelectLength: maxSelectLength,
          fetchSourceList: fetchSourceList,
          fetchSearchList: fetchSearchList,
          paging: paging,
          pageSize: pageSize,
          nameAlias: nameAlias,
          codeAlias: codeAlias,
          isSwitch: isSwitch,
          switchChecked: switchChecked,
          switchDisabled: switchDisabled
        }
      }).then(function (selectList) {
        var value = convertValue && _this2.convertToValue(selectList) || selectList;

        _this2.setState({
          value: value
        });

        _this2.onChange(value);
      }, _utils["default"].noop);
    }
  }, {
    key: "renderCheckedValues",
    value: function renderCheckedValues() {
      var value = this.state.value;
      var _this$props2 = this.props,
          scope = _this$props2.scope,
          maxShowLength = _this$props2.maxShowLength,
          nameAlias = _this$props2.nameAlias,
          emptyText = _this$props2.emptyText,
          title = _this$props2.title,
          isEasy = _this$props2.isEasy,
          inForm = _this$props2.inForm;
      var defaultEmptyText;

      if (!emptyText) {
        switch (scope) {
          case MANAGE_PICKER.SCOPE_CATEGORY:
            defaultEmptyText = "未选择分类";
            break;

          case MANAGE_PICKER.SCOPE_ITEM:
            defaultEmptyText = "未选择" + title;
            break;

          default:
            defaultEmptyText = "未选择" + title;
            break;
        }
      }

      if (value && value.length) {
        if (isEasy) {
          var joinNames = value.map(function (item) {
            return item[nameAlias];
          }).join('、');
          return _react["default"].createElement("span", {
            className: "joinNames"
          }, joinNames);
        }

        return _lodash["default"].take(value, maxShowLength).map(function (item) {
          return _react["default"].createElement(_tag["default"], {
            className: "name",
            key: item.code
          }, _react["default"].createElement("span", {
            className: item.icon
          }), item[nameAlias]);
        });
      } else {
        return _react["default"].createElement("span", {
          className: "empty ".concat(inForm ? 'form-empty' : '')
        }, emptyText || defaultEmptyText);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var value = this.state.value;
      var _this$props3 = this.props,
          className = _this$props3.className,
          maxShowLength = _this$props3.maxShowLength,
          disabled = _this$props3.disabled,
          isInTable = _this$props3.isInTable,
          isEasy = _this$props3.isEasy;
      var containerClass = (0, _classnames["default"])("".concat(prefix, "-manage-picker"), className, {
        disabled: disabled
      });
      return _react["default"].createElement("div", {
        className: containerClass,
        onClick: this.openModal,
        style: {
          marginTop: isInTable ? 0 : 5
        }
      }, this.renderCheckedValues(), value && value.length > maxShowLength && !isEasy ? _react["default"].createElement("span", {
        className: 'stats'
      }, "\u5171", _react["default"].createElement("span", {
        className: 'strong'
      }, value.length), "\u9879") : null);
    }
  }]);

  return ManagePicker;
}(_react.Component);

ManagePicker.defaultProps = {
  maxShowLength: 6,
  maxSelectLength: -1,
  scope: MANAGE_PICKER.SCOPE_ALL,
  nameAlias: 'name',
  codeAlias: 'code'
};
var _default = ManagePicker;
exports["default"] = _default;