var _excluded = ["visible", "value", "allowClear", "onChange", "list", "titles"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useImperativeHandle, forwardRef, useRef } from 'react';
// import Sortable from 'sortablejs'
import { ReactSortable } from 'react-sortablejs';
import LocaleUtil from "./../../../utils/LocaleUtil";
import Card from "./../../Card";
import Item from "./Item";

// 穿梭框
var Transfer = function Transfer(_ref, ref) {
  var visible = _ref.visible,
    value = _ref.value,
    allowClear = _ref.allowClear,
    onChange = _ref.onChange,
    list = _ref.list,
    titles = _ref.titles,
    props = _objectWithoutProperties(_ref, _excluded);
  // 容器
  var mainRef = useRef(null);

  // 暴露方法
  useImperativeHandle(ref, function () {
    return {
      mainDOM: mainRef.current,
      getMainDOM: function getMainDOM() {
        return mainRef.current;
      }
    };
  });

  // 删除
  function handleDelete(item, index) {
    if (onChange) {
      onChange(value.filter(function (selected) {
        return selected.id !== item.id;
      }));
    }
  }

  // 添加
  function handleAdd(item, index) {
    var _iterator = _createForOfIteratorHelper(list),
      _step;
    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var originItem = _step.value;
        if (originItem.id === item.id) {
          if (onChange) {
            onChange([].concat(_toConsumableArray(value), [item]));
          }
          break;
        }
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
  }

  // 未选列表
  var unSelectedList = list === null || list === void 0 ? void 0 : list.filter(function (item, index) {
    return !(value !== null && value !== void 0 && value.some(function (selected) {
      return selected.id === item.id;
    }));
  });
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", _extends({}, props, {
    className: "transfer-main".concat(props !== null && props !== void 0 && props.className ? ' ' + props.className : ''),
    ref: mainRef
  }), value !== null && value !== void 0 && value.length ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "transfer-title"
  }, /*#__PURE__*/React.createElement("div", {
    className: "transfer-title-content"
  }, (titles === null || titles === void 0 ? void 0 : titles.selected) || LocaleUtil.locale('已添加', 'SeedsUI_added')), "".concat(value.length, "/").concat((list === null || list === void 0 ? void 0 : list.length) || 0)), /*#__PURE__*/React.createElement(ReactSortable, {
    className: "card transfer-card",
    list: value,
    setList: function setList(newValue) {
      // 如果值未发生变化则不触发onChange
      if (JSON.stringify(value.map(function (item) {
        return item.id;
      })) === JSON.stringify(newValue.map(function (item) {
        return item.id;
      }))) {
        return;
      }
      if (onChange) {
        onChange(newValue);
      }
    }
  }, value.map(function (item, index) {
    return /*#__PURE__*/React.createElement(Item, {
      key: item.id,
      onDelete: function onDelete() {
        return handleDelete(item, index);
      },
      sortable: true
    }, (item === null || item === void 0 ? void 0 : item.name) || '');
  }))) : null, unSelectedList !== null && unSelectedList !== void 0 && unSelectedList.length ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "transfer-title"
  }, /*#__PURE__*/React.createElement("div", {
    className: "transfer-title-content"
  }, (titles === null || titles === void 0 ? void 0 : titles.unSelected) || LocaleUtil.locale('未添加', 'SeedsUI_not_added')), "".concat(unSelectedList.length, "/").concat((list === null || list === void 0 ? void 0 : list.length) || 0)), /*#__PURE__*/React.createElement(Card, {
    className: "transfer-card"
  }, unSelectedList.map(function (item, index) {
    return /*#__PURE__*/React.createElement(Item, {
      key: item.id,
      onAdd: function onAdd() {
        return handleAdd(item, index);
      }
    }, (item === null || item === void 0 ? void 0 : item.name) || '');
  }))) : null));
};
export default /*#__PURE__*/forwardRef(Transfer);