"use strict";

var __assign = void 0 && (void 0).__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

var __spreadArray = void 0 && (void 0).__spreadArray || function (to, from) {
  for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) {
    to[j] = from[i];
  }

  return to;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

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

var jsx_runtime_1 = require("react/jsx-runtime");

var React = __importStar(require("react"));

var check_list_1 = __importStar(require("../check-list"));

var classnames_1 = __importDefault(require("classnames"));

var sort_list_1 = __importDefault(require("../sort-list"));

require("./style/transfer.css");

var useStaticCallback_1 = __importDefault(require("../../hooks/useStaticCallback"));

var scrollbar_1 = __importDefault(require("../scrollbar"));

var find_1 = __importDefault(require("lodash/find"));

var map_1 = __importDefault(require("lodash/map"));

var react_use_1 = require("react-use");

var prefixCls = 'dmc-transfer';

var getChild = function getChild(arr) {
  var childs = [];
  arr.forEach(function (item) {
    if (item.children) {
      childs.splice.apply(childs, __spreadArray([-1, 0], getChild(item.children)));
    } else {
      childs.push(item);
    }
  });
  return childs;
};

var TransferMode;

(function (TransferMode) {
  TransferMode["plain"] = "plain";
  TransferMode["normal"] = "normal";
})(TransferMode = exports.TransferMode || (exports.TransferMode = {}));

var Transfer = function Transfer(props) {
  var _a, _b, _c, _d;

  var _e = props,
      _f = _e.mode,
      mode = _f === void 0 ? TransferMode.normal : _f,
      type = _e.type,
      size = _e.size,
      allSelectLabel = _e.allSelectLabel,
      placeholder = _e.placeholder,
      options = _e.options,
      value = _e.value,
      defaultValue = _e.defaultValue,
      onChange = _e.onChange,
      onRemove = _e.onRemove,
      sortable = _e.sortable,
      removable = _e.removable,
      visibleUpDown = _e.visibleUpDown,
      width = _e.width,
      height = _e.height,
      labelKey = _e.labelKey,
      valueKey = _e.valueKey,
      noContentText = _e.noContentText,
      noResultText = _e.noResultText,
      sortListVirtualized = _e.sortListVirtualized,
      checkListDescriptionText = _e.checkListDescriptionText,
      sortListDescriptionText = _e.sortListDescriptionText,
      showCount = _e.showCount,
      allowClear = _e.allowClear,
      allowClearText = _e.allowClearText,
      filterList = _e.filterList,
      filterTreeList = _e.filterTreeList,
      checkListDynamicRowHeight = _e.checkListDynamicRowHeight,
      restProps = __rest(_e, ["mode", "type", "size", "allSelectLabel", "placeholder", "options", "value", "defaultValue", "onChange", "onRemove", "sortable", "removable", "visibleUpDown", "width", "height", "labelKey", "valueKey", "noContentText", "noResultText", "sortListVirtualized", "checkListDescriptionText", "sortListDescriptionText", "showCount", "allowClear", "allowClearText", "filterList", "filterTreeList", "checkListDynamicRowHeight"]);

  var isTree = options.some(function (opt) {
    return !!opt.children;
  });
  var cls = classnames_1["default"](prefixCls, (_a = {}, _a[prefixCls + "-tree"] = isTree, _a[prefixCls + "-plain"] = mode === TransferMode.plain, _a[prefixCls + "-no-padding"] = mode === TransferMode.normal, _a));

  var _g = React.useState(defaultValue),
      unControlVal = _g[0],
      setUnControlVal = _g[1];

  var isControl = ('value' in props);
  var val = isControl ? value : unControlVal;

  var _h = react_use_1.useMeasure(),
      ref = _h[0],
      _j = _h[1],
      sortListWidth = _j.width,
      sortListHeight = _j.height;

  var optionArray = React.useMemo(function () {
    return getChild(options);
  }, [options]);
  var sortlistVal = React.useMemo(function () {
    var sVal = [];
    val.forEach(function (v) {
      var item = find_1["default"](optionArray, function (option) {
        return option.key === v;
      });

      if (item) {
        sVal.push(item);
      }
    });
    return sVal;
  }, [optionArray, val]);
  var countStr = sortlistVal.length + "/" + optionArray.length;
  var onCheckChange = useStaticCallback_1["default"](function (selected) {
    var newVal = selected;
    !isControl && setUnControlVal(newVal);
    onChange && onChange(newVal);
  });
  var onListRemove = useStaticCallback_1["default"](function (selected) {
    var newVal = map_1["default"](selected, function (val) {
      return val.key;
    });
    !isControl && setUnControlVal(newVal);
    onRemove && onRemove(newVal);
  });
  var handleClear = useStaticCallback_1["default"](function () {
    !isControl && setUnControlVal([]);
    onRemove && onRemove([]);
  });
  return jsx_runtime_1.jsxs("div", __assign({}, restProps, {
    className: cls,
    style: {
      width: width,
      height: height
    }
  }, {
    children: [isTree ? jsx_runtime_1.jsx(check_list_1.TreeCheckList, {
      className: prefixCls + "-tree-checklist",
      type: type,
      size: size,
      options: options,
      value: val,
      onChange: onCheckChange,
      filterList: filterTreeList,
      placeholder: placeholder,
      flexHeight: true,
      noContentText: noContentText,
      noResultText: noResultText
    }, void 0) : jsx_runtime_1.jsx(check_list_1["default"], {
      className: prefixCls + "-checklist",
      type: type,
      mode: mode,
      size: (size !== null && size !== void 0 ? size : mode === TransferMode.plain) ? 'compact' : undefined,
      options: options,
      value: val,
      showCheckbox: true,
      text: checkListDescriptionText,
      allSelectLabel: allSelectLabel,
      placeholder: placeholder,
      labelKey: labelKey || 'title',
      valueKey: valueKey || 'key',
      onChange: onCheckChange,
      filterList: filterList,
      flexHeight: true,
      noContentText: noContentText,
      noResultText: noResultText,
      dynamicRowHeight: checkListDynamicRowHeight
    }, void 0), jsx_runtime_1.jsxs("div", __assign({
      className: classnames_1["default"](prefixCls + "-checked", (_b = {}, _b[prefixCls + "-plain-sort"] = mode === TransferMode.plain, _b))
    }, {
      children: [jsx_runtime_1.jsxs("div", __assign({
        className: prefixCls + "-checked-top-right"
      }, {
        children: [sortListDescriptionText && jsx_runtime_1.jsx("span", __assign({
          className: prefixCls + "-sort-list-desc"
        }, {
          children: sortListDescriptionText
        }), void 0), jsx_runtime_1.jsxs("span", __assign({
          className: classnames_1["default"](prefixCls + "-checked-top-right-other", {
            'align-right': showCount && !allowClear || !showCount && allowClear,
            'space-between': showCount && allowClear
          })
        }, {
          children: [showCount && jsx_runtime_1.jsx("div", __assign({
            className: prefixCls + "-checked-count"
          }, {
            children: countStr
          }), void 0), allowClear && val.length > 0 && jsx_runtime_1.jsx("span", __assign({
            className: "clear",
            onClick: handleClear
          }, {
            children: allowClearText
          }), void 0)]
        }), void 0)]
      }), void 0), jsx_runtime_1.jsx("div", __assign({
        className: classnames_1["default"](prefixCls + "-checked-list", (_c = {}, _c[prefixCls + "-" + type] = type, _c))
      }, {
        children: jsx_runtime_1.jsx(scrollbar_1["default"], __assign({
          type: type
        }, {
          children: jsx_runtime_1.jsx("div", __assign({
            ref: ref,
            className: classnames_1["default"](prefixCls + "-checked-list-wrapper", (_d = {}, _d[prefixCls + "-" + type] = type, _d[prefixCls + "-checked-list-plain"] = mode === TransferMode.plain, _d))
          }, {
            children: jsx_runtime_1.jsx(sort_list_1["default"], {
              type: type,
              mode: mode,
              disabled: !sortable,
              removable: removable,
              visibleUpDown: visibleUpDown,
              list: sortlistVal,
              listWidth: sortListWidth,
              listHeight: sortListHeight,
              labelKey: labelKey || 'title',
              valueKey: valueKey || 'key',
              // eslint-disable-next-line @typescript-eslint/ban-ts-comment
              // @ts-ignore
              useVirtual: sortListVirtualized,
              onChange: onListRemove
            }, void 0)
          }), void 0)
        }), void 0)
      }), void 0)]
    }), void 0)]
  }), void 0);
};

var defaultProps = {
  sortable: false,
  removable: true,
  height: 379,
  options: [],
  // value: [] as string[],
  defaultValue: [],
  sortListDescriptionText: '',
  showCount: true,
  allowClear: false,
  allowClearText: '清空'
};
Transfer.defaultProps = defaultProps;
exports["default"] = Transfer;

