"use strict";

exports.__esModule = true;
exports.default = void 0;

var _babelRuntimeJsxStyleTransform = require("babel-runtime-jsx-style-transform");

var _tslib = require("tslib");

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

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

var _button = _interopRequireDefault(require("../button"));

var _checkbox = _interopRequireDefault(require("../checkbox"));

var _icon = _interopRequireDefault(require("../icon"));

var _view = _interopRequireDefault(require("../view"));

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 _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); }

var Transfer = function (props, ref) {
  var _props$dataSource = props.dataSource,
      dataSource = _props$dataSource === void 0 ? [] : _props$dataSource,
      _props$value = props.value,
      value = _props$value === void 0 ? [] : _props$value,
      _props$titles = props.titles,
      titles = _props$titles === void 0 ? [] : _props$titles,
      _props$tabIndex = props.tabIndex,
      tabIndex = _props$tabIndex === void 0 ? 0 : _props$tabIndex,
      _props$defaultValue = props.defaultValue,
      defaultValue = _props$defaultValue === void 0 ? [] : _props$defaultValue,
      _props$tabChange = props.tabChange,
      tabChange = _props$tabChange === void 0 ? function () {} : _props$tabChange,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      _props$leftDisabled = props.leftDisabled,
      leftDisabled = _props$leftDisabled === void 0 ? false : _props$leftDisabled,
      _props$rightDisabled = props.rightDisabled,
      rightDisabled = _props$rightDisabled === void 0 ? false : _props$rightDisabled,
      _props$notFoundConten = props.notFoundContent,
      notFoundContent = _props$notFoundConten === void 0 ? 'Not Found' : _props$notFoundConten,
      _props$defaultLeftChe = props.defaultLeftChecked,
      defaultLeftChecked = _props$defaultLeftChe === void 0 ? [] : _props$defaultLeftChe,
      _props$defaultRightCh = props.defaultRightChecked,
      defaultRightChecked = _props$defaultRightCh === void 0 ? [] : _props$defaultRightCh,
      _props$onChange = props.onChange,
      onChange = _props$onChange === void 0 ? function () {} : _props$onChange,
      _props$buttonText = props.buttonText,
      buttonText = _props$buttonText === void 0 ? 'Transfer to' : _props$buttonText,
      _props$selectItemText = props.selectItemText,
      selectItemText = _props$selectItemText === void 0 ? 'Item' : _props$selectItemText,
      _props$style = props.style,
      style = _props$style === void 0 ? {} : _props$style,
      others = (0, _tslib.__rest)(props, ["dataSource", "value", "titles", "tabIndex", "defaultValue", "tabChange", "disabled", "leftDisabled", "rightDisabled", "notFoundContent", "defaultLeftChecked", "defaultRightChecked", "onChange", "buttonText", "selectItemText", "style"]);
  var rightValue = props.value || defaultValue || [];

  var _useState = (0, _react.useState)(tabIndex),
      currentTabIndex = _useState[0],
      setTabIndex = _useState[1];

  var _filterTransferValue = (0, _utils.filterTransferValue)(dataSource, rightValue),
      leftList = _filterTransferValue.leftList,
      rightList = _filterTransferValue.rightList;

  if ('defaultLeftChecked' in props) {
    leftList = (0, _utils.setCheckedItem)(leftList, defaultLeftChecked);
  }

  if ('defaultRightChecked' in props) {
    rightList = (0, _utils.setCheckedItem)(rightList, defaultRightChecked);
  }

  var useStateLeft = (0, _react.useState)(leftList);
  var transferLeftList = useStateLeft[0];
  var setTransferLeftList = useStateLeft[1];
  var useStateRight = (0, _react.useState)(rightList);
  var transferRightList = useStateRight[0];
  var setTransferRightList = useStateRight[1];

  if (leftDisabled) {
    transferLeftList = (0, _utils.setListDisabled)(transferLeftList);
  }

  if (rightDisabled) {
    transferRightList = (0, _utils.setListDisabled)(transferRightList);
  }

  if (disabled) {
    transferLeftList = (0, _utils.setListDisabled)(transferLeftList);
    transferRightList = (0, _utils.setListDisabled)(transferRightList);
  }

  var transferButtonText = currentTabIndex === 0 ? titles[1] : titles[0];
  var transferList = currentTabIndex === 0 ? transferLeftList : transferRightList;

  var _getDataStatus = (0, _utils.getDataStatus)(transferList),
      listDataSize = _getDataStatus.listDataSize,
      disabledSize = _getDataStatus.disabledSize,
      checkedSize = _getDataStatus.checkedSize,
      isCheckAll = _getDataStatus.isCheckAll;

  var prefix = 'mt-';
  var classNameObj = {
    transferCls: (0, _classnames.default)(prefix + "transfer"),
    tabCls: (0, _classnames.default)(prefix + "transfer-tab"),
    tabMainWrap: (0, _classnames.default)(prefix + "transfer-tab-main"),
    tabItemCls: (0, _classnames.default)(prefix + "transfer-tab-main-item"),
    tabIconWrapCls: (0, _classnames.default)(prefix + "transfer-tab-icon-wrap"),
    tabIconCls: (0, _classnames.default)(prefix + "transfer-tab-icon-wrap-icon"),
    tabItemActiveCls: (0, _classnames.default)(prefix + "transfer-tab-main-item", prefix + "transfer-tab-main-item--selected"),
    transferList: (0, _classnames.default)(prefix + "transfer-list", prefix + "transfer-list--selected"),
    transferItem: (0, _classnames.default)(prefix + "transfer-list-item"),
    transferTips: (0, _classnames.default)(prefix + "transfer-select-tip"),
    checkbox: (0, _classnames.default)(prefix + "transfer-checkbox"),
    transferListScroll: (0, _classnames.default)(prefix + "transfer-list-scroll"),
    transferEmptyContent: (0, _classnames.default)(prefix + "transfer-empty-content"),
    transferButton: (0, _classnames.default)(prefix + "transfer-button")
  };

  var handleClickTab = function (e, item, index) {
    setTabIndex(index);

    if (tabChange) {
      tabChange(e, item, index);
    }
  };

  var handleClickItem = function (checked, e, item, index) {
    if (currentTabIndex === 0) {
      var newTransferLeftList = transferLeftList.map(function (obj) {
        return Object.assign({}, obj);
      });
      newTransferLeftList[index].isChecked = checked;
      setTransferLeftList(newTransferLeftList);
    } else {
      var newTransferRightList = transferRightList.map(function (obj) {
        return Object.assign({}, obj);
      });
      newTransferRightList[index].isChecked = checked;
      setTransferRightList(newTransferRightList);
    }
  };

  var handleClickSelectTip = function (checked) {
    if (currentTabIndex === 0) {
      var newTransferLeftList = (0, _utils.checkAllData)(transferLeftList, checked);
      setTransferLeftList([].concat(newTransferLeftList));
    } else {
      var newTransferRightList = (0, _utils.checkAllData)(transferRightList, checked);
      setTransferRightList([].concat(newTransferRightList));
    }
  };

  var handleChangeButton = function () {
    var tabsIndex = currentTabIndex === 0 ? 1 : 0;
    var startList = currentTabIndex === 0 ? transferLeftList : transferRightList;
    var endList = currentTabIndex === 0 ? transferRightList : transferLeftList;

    var _changeCheckedItem = (0, _utils.changeCheckedItem)(startList, endList),
        initList = _changeCheckedItem.initList,
        initValue = _changeCheckedItem.initValue,
        goalList = _changeCheckedItem.goalList,
        goalValue = _changeCheckedItem.goalValue,
        changeList = _changeCheckedItem.changeList,
        changeListValue = _changeCheckedItem.changeListValue;

    var leftData = currentTabIndex === 0 ? initList : goalList;
    var rightData = currentTabIndex === 0 ? goalList : initList;
    setTransferLeftList([].concat(leftData));
    setTransferRightList([].concat(rightData));
    setTabIndex(tabsIndex);

    if (onChange) {
      onChange(currentTabIndex === 0 ? goalValue : initValue, currentTabIndex === 0 ? goalList : initList, {
        leftValue: currentTabIndex === 0 ? initValue : goalValue,
        leftData: currentTabIndex === 0 ? initList : goalList,
        movedValue: changeListValue,
        movedData: changeList,
        redirection: currentTabIndex === 0 ? 'right' : 'left'
      });
    }
  };

  (0, _react.useEffect)(function () {
    if ('value' in props) {
      if (useStateLeft[0].toString() !== leftList.toString()) {
        setTransferLeftList(leftList);
      }

      if (useStateRight[0].toString() !== rightList.toString()) {
        setTransferRightList(rightList);
      }
    }
  }, [value, props.dataSource]);
  return /*#__PURE__*/(0, _react.createElement)(_view.default, _extends({}, others, {
    ref: ref,
    className: classNameObj.transferCls,
    style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(props.style)
  }), /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: classNameObj.tabCls
  }, /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: classNameObj.tabMainWrap
  }, titles.map(function (item, index) {
    return /*#__PURE__*/(0, _react.createElement)(_view.default, {
      className: currentTabIndex === index ? classNameObj.tabItemActiveCls : classNameObj.tabItemCls,
      key: index,
      onClick: function onClick(e) {
        return handleClickTab(e, item, index);
      }
    }, item);
  })), /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: classNameObj.tabIconWrapCls
  }, /*#__PURE__*/(0, _react.createElement)(_icon.default, {
    name: "switch",
    className: classNameObj.tabIconCls
  }))), /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: classNameObj.transferList
  }, listDataSize > 0 ? /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: classNameObj.transferTips
  }, /*#__PURE__*/(0, _react.createElement)(_checkbox.default, {
    size: "medium",
    disabled: disabledSize === listDataSize,
    checked: isCheckAll,
    indeterminate: !!(!isCheckAll && checkedSize > 0),
    onChange: function (checked, e) {
      return handleClickSelectTip(checked, e);
    }
  }, checkedSize > 0 ? checkedSize + "/" : '', listDataSize, " ", selectItemText)) : null, /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: classNameObj.transferListScroll
  }, transferList.map(function (item, index) {
    return /*#__PURE__*/(0, _react.createElement)(_view.default, {
      className: classNameObj.transferItem,
      key: index
    }, /*#__PURE__*/(0, _react.createElement)(_checkbox.default, {
      size: "medium",
      className: classNameObj.checkbox,
      disabled: item.disabled,
      checked: item.isChecked,
      onChange: function (checked, e) {
        return handleClickItem(checked, e, item, index);
      }
    }, item.label));
  }), listDataSize <= 0 ? /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: classNameObj.transferEmptyContent
  }, notFoundContent) : null)), /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: classNameObj.transferButton
  }, /*#__PURE__*/(0, _react.createElement)(_button.default, {
    type: "primary",
    size: "large",
    disabled: disabledSize === listDataSize || checkedSize === 0,
    fullWidth: true,
    onClick: function onClick(e) {
      return handleChangeButton(e);
    }
  }, buttonText, " ", transferButtonText)));
};

var _default = /*#__PURE__*/(0, _react.forwardRef)(Transfer);

exports.default = _default;