"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

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

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _createForOfIteratorHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelper"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var _tslib = require("tslib");

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

var _mobxReact = require("mobx-react");

var _mobx = require("mobx");

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

var _omit = _interopRequireDefault(require("lodash/omit"));

var _Select2 = require("../select/Select");

var _Option = _interopRequireDefault(require("../option/Option"));

var _OptGroup = _interopRequireDefault(require("../option/OptGroup"));

var _TransferList = _interopRequireDefault(require("./TransferList"));

var _TransferOperation = _interopRequireDefault(require("./TransferOperation"));

var _TransferSort = _interopRequireDefault(require("./TransferSort"));

var _autobind = _interopRequireDefault(require("../_util/autobind"));

var _isSameLike = _interopRequireDefault(require("../_util/isSameLike"));

var Transfer = /*#__PURE__*/function (_Select) {
  (0, _inherits2["default"])(Transfer, _Select);

  var _super = (0, _createSuper2["default"])(Transfer);

  function Transfer(props, context) {
    var _this;

    (0, _classCallCheck2["default"])(this, Transfer);
    _this = _super.call(this, props, context);
    _this.isCustom = false;

    _this.clearCurrentIndex = function () {
      var current = _this.options.current;

      if (current) {
        current.isCurrent = false;
      }
    };

    _this.changeOptionIndex = function () {
      var _optionData;

      var _assertThisInitialize = (0, _assertThisInitialized2["default"])(_this),
          valueField = _assertThisInitialize.valueField,
          options = _assertThisInitialize.options;

      var targetValues = _this.getValues(); // 这是有顺序的


      var optionData = options.data;
      var sortOpt = [];
      targetValues.forEach(function (key) {
        optionData = optionData.filter(function (record) {
          if (record.get(valueField) !== key) {
            return true;
          }

          sortOpt.push(record);
          return false;
        });
      });

      (_optionData = optionData).unshift.apply(_optionData, sortOpt);

      _this.options.data = optionData;
    };

    _this.handleRemove = function (value) {
      _this.removeValues([value]);

      _this.updateIndex();
    };

    _this.handleSetTargetOption = function (values) {
      _this.setValue(values);
    };

    (0, _mobx.runInAction)(function () {
      _this.sourceSelected = [];
      _this.targetSelected = [];

      _this.clearCurrentIndex();
    });
    _this.isCustom = typeof props.children === 'function';
    return _this;
  }

  (0, _createClass2["default"])(Transfer, [{
    key: "range",
    get: function get() {
      return false;
    }
  }, {
    key: "sourceFilter",
    value: function sourceFilter(record, index, array) {
      var valueField = this.valueField,
          optionsFilter = this.props.optionsFilter;

      if (optionsFilter && !optionsFilter(record, index, array)) {
        return false;
      }

      var values = this.getValues();

      if (values.length) {
        return values.every(function (v) {
          return !(0, _isSameLike["default"])(record.get(valueField), v);
        });
      }

      return true;
    }
  }, {
    key: "targetFilter",
    value: function targetFilter(record, index, array) {
      var valueField = this.valueField,
          optionsFilter = this.props.optionsFilter;

      if (optionsFilter && !optionsFilter(record, index, array)) {
        return false;
      }

      var values = this.getValues();

      if (values.length) {
        return values.some(function (v) {
          return (0, _isSameLike["default"])(record.get(valueField), v);
        });
      }

      return false;
    }
  }, {
    key: "handleMenuClick",
    value: function handleMenuClick(_ref) {
      var value = _ref.item.props.value;

      if (this.multiple) {
        this.selectRecord(value, this.sourceSelected);
      } else {
        this.prepareSetValue(this.processRecordToObject(value));
      }
    }
  }, {
    key: "handleTargetMenuClick",
    value: function handleTargetMenuClick(_ref2) {
      var value = _ref2.item.props.value;

      if (this.multiple) {
        this.selectRecord(value, this.targetSelected);
      } else {
        this.removeValue(value);
      }

      this.options.locate(value.index);
    }
  }, {
    key: "handleMoveToLeft",
    value: function handleMoveToLeft() {
      var valueField = this.valueField,
          isCustom = this.isCustom;

      if (!isCustom) {
        this.removeValues(this.targetSelected.map(function (record) {
          return record.get(valueField);
        }));
      } else {
        var currentValues = this.getValues();
        var targetKeys = this.targetSelected.map(function (record) {
          return record.key;
        });

        var _iterator = (0, _createForOfIteratorHelper2["default"])(targetKeys),
            _step;

        try {
          var _loop = function _loop() {
            var key = _step.value;
            currentValues = currentValues.filter(function (record) {
              return record.key !== key;
            });
          };

          for (_iterator.s(); !(_step = _iterator.n()).done;) {
            _loop();
          }
        } catch (err) {
          _iterator.e(err);
        } finally {
          _iterator.f();
        }

        this.setValue(currentValues);
      }

      this.targetSelected = [];
      this.updateIndex();
    }
  }, {
    key: "handleMoveToRight",
    value: function handleMoveToRight() {
      var valueField = this.valueField;

      if (this.isCustom) {
        var currentValues = this.getValues();
        this.setValue([].concat((0, _toConsumableArray2["default"])(currentValues), (0, _toConsumableArray2["default"])(this.sourceSelected)), true);
      } else {
        this.prepareSetValue.apply(this, (0, _toConsumableArray2["default"])(this.sourceSelected.map(function (record) {
          return record.get(valueField);
        })));
      }

      this.sourceSelected = [];
      this.updateIndex();
    }
  }, {
    key: "handleSortTo",
    value: function handleSortTo(direction) {
      var valueField = this.valueField,
          options = this.options;
      var current = options.current;

      if (current) {
        var targetFilteredOptions = options.getState('targetFilteredOptions');

        if (targetFilteredOptions) {
          var to = direction === 'up' ? -1 : 1;
          var currentValue = current.get(valueField);
          var index = targetFilteredOptions.findIndex(function (record) {
            return record.get(valueField) === currentValue;
          });
          var currentOpt = targetFilteredOptions[index];
          var moveOpt = targetFilteredOptions[index + to];

          if (currentOpt && moveOpt) {
            var currentOptValue = currentOpt.get(valueField);
            var moveOptValue = moveOpt.get(valueField);
            var optionsCurrentIndex = options.findIndex(function (record) {
              return record.get(valueField) === currentOptValue;
            });
            var optionsMoveIndex = options.findIndex(function (record) {
              return record.get(valueField) === moveOptValue;
            });

            if (optionsCurrentIndex !== -1 && optionsMoveIndex !== -1) {
              options.move(optionsCurrentIndex, optionsMoveIndex);
            }
          }
        }
      }
    }
  }, {
    key: "handleSourceSelectAllChange",
    value: function handleSourceSelectAllChange(selected) {
      this.sourceSelected = selected;
    }
  }, {
    key: "handleTargetSelectAllChange",
    value: function handleTargetSelectAllChange(selected) {
      this.targetSelected = selected;
    }
  }, {
    key: "selectRecord",
    value: function selectRecord(value, selected) {
      var index = selected.indexOf(value);

      if (index !== -1) {
        selected.splice(index, 1);
      } else {
        selected.push(value);
      }
    }
  }, {
    key: "updateIndex",
    value: function updateIndex() {
      this.changeOptionIndex();
      this.clearCurrentIndex();
    }
  }, {
    key: "renderWrapper",
    value: function renderWrapper() {
      var _this2 = this;

      var disabled = this.disabled,
          prefixCls = this.prefixCls,
          targetSelected = this.targetSelected,
          sourceSelected = this.sourceSelected,
          multiple = this.multiple,
          valueField = this.valueField,
          _this$props = this.props,
          _this$props$titles = _this$props.titles,
          titles = _this$props$titles === void 0 ? [] : _this$props$titles,
          _this$props$operation = _this$props.operations,
          operations = _this$props$operation === void 0 ? [] : _this$props$operation,
          _this$props$sortOpera = _this$props.sortOperations,
          sortOperations = _this$props$sortOpera === void 0 ? [] : _this$props$sortOpera,
          sortable = _this$props.sortable,
          oneWay = _this$props.oneWay;
      var targetValues = this.getValues();
      var currentTarget = this.options.current;
      var upActive = false;
      var downActive = false;
      var currentIndex = currentTarget ? targetValues.findIndex(function (x) {
        return x === currentTarget.get(valueField);
      }) : -1;
      var targetFilteredOptions = this.options.getState('targetFilteredOptions');

      if (targetFilteredOptions && currentTarget) {
        currentIndex = targetFilteredOptions.findIndex(function (record) {
          return record.get(valueField) === currentTarget.get(valueField);
        });
        upActive = currentIndex > -1 && currentIndex !== 0;
        downActive = currentIndex > -1 && currentIndex !== targetFilteredOptions.length - 1;
      }

      var classNameString = (0, _classnames["default"])("".concat(prefixCls, "-wrapper"), (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-sortable"), sortable));
      var oneWayProps = {};

      if (oneWay) {
        oneWayProps = {
          multiple: false,
          onRemove: this.handleRemove
        };
      }

      var customOption = {};

      if (this.isCustom) {
        customOption = {
          targetOption: targetValues
        };
      }

      var childProps = (0, _omit["default"])(this.props, ['help']);
      return /*#__PURE__*/_react["default"].createElement("span", {
        key: "wrapper",
        className: classNameString
      }, /*#__PURE__*/_react["default"].createElement(_TransferList["default"], (0, _extends2["default"])({}, childProps, {
        options: this.options,
        selected: sourceSelected,
        header: titles[0],
        onSelectAll: this.handleSourceSelectAllChange,
        onSelect: this.handleMenuClick,
        optionsFilter: this.sourceFilter,
        direction: "left"
      }, customOption)), /*#__PURE__*/_react["default"].createElement(_TransferOperation["default"], {
        className: "".concat(prefixCls, "-operation"),
        leftActive: !(!targetSelected.length || disabled),
        rightActive: !(!sourceSelected.length || disabled),
        rightArrowText: operations[0],
        leftArrowText: operations[1],
        moveToLeft: this.handleMoveToLeft,
        moveToRight: this.handleMoveToRight,
        multiple: multiple,
        oneWay: oneWay
      }), /*#__PURE__*/_react["default"].createElement(_TransferList["default"], (0, _extends2["default"])({}, childProps, {
        options: this.options,
        selected: targetSelected,
        header: titles[1],
        currentIndex: currentIndex,
        onSelectAll: this.handleTargetSelectAllChange,
        onSelect: this.handleTargetMenuClick,
        optionsFilter: this.targetFilter,
        direction: "right",
        setTargetOption: this.handleSetTargetOption
      }, customOption, oneWayProps)), sortable && !this.isCustom && /*#__PURE__*/_react["default"].createElement(_TransferSort["default"], {
        className: "".concat(prefixCls, "-sort"),
        upActive: upActive,
        downActive: downActive,
        upArrowText: sortOperations[0],
        downArrowText: sortOperations[1],
        moveToUp: function moveToUp() {
          return _this2.handleSortTo('up');
        },
        moveToDown: function moveToDown() {
          return _this2.handleSortTo('down');
        },
        multiple: multiple
      }));
    }
  }]);
  return Transfer;
}(_Select2.Select);

Transfer.displayName = 'Transfer';
Transfer.defaultProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, _Select2.Select.defaultProps), {}, {
  suffixCls: 'transfer',
  multiple: true,
  sortable: false,
  oneWay: false
});
Transfer.Option = _Option["default"];
Transfer.OptGroup = _OptGroup["default"];
(0, _tslib.__decorate)([_mobx.observable], Transfer.prototype, "sourceSelected", void 0);
(0, _tslib.__decorate)([_mobx.observable], Transfer.prototype, "targetSelected", void 0);
(0, _tslib.__decorate)([_mobx.observable], Transfer.prototype, "targetCurrentSelected", void 0);
(0, _tslib.__decorate)([_autobind["default"]], Transfer.prototype, "sourceFilter", null);
(0, _tslib.__decorate)([_autobind["default"]], Transfer.prototype, "targetFilter", null);
(0, _tslib.__decorate)([_autobind["default"]], Transfer.prototype, "handleMenuClick", null);
(0, _tslib.__decorate)([_autobind["default"]], Transfer.prototype, "handleTargetMenuClick", null);
(0, _tslib.__decorate)([_autobind["default"], _mobx.action], Transfer.prototype, "handleMoveToLeft", null);
(0, _tslib.__decorate)([_autobind["default"], _mobx.action], Transfer.prototype, "handleMoveToRight", null);
(0, _tslib.__decorate)([_autobind["default"], _mobx.action], Transfer.prototype, "handleSortTo", null);
(0, _tslib.__decorate)([_autobind["default"], _mobx.action], Transfer.prototype, "handleSourceSelectAllChange", null);
(0, _tslib.__decorate)([_autobind["default"], _mobx.action], Transfer.prototype, "handleTargetSelectAllChange", null);
(0, _tslib.__decorate)([_mobx.action], Transfer.prototype, "selectRecord", null);
(0, _tslib.__decorate)([_mobx.action], Transfer.prototype, "clearCurrentIndex", void 0);
(0, _tslib.__decorate)([_mobx.action], Transfer.prototype, "updateIndex", null);
Transfer = (0, _tslib.__decorate)([_mobxReact.observer], Transfer);
var _default = Transfer;
exports["default"] = _default;
//# sourceMappingURL=Transfer.js.map
