var __assign = this && this.__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 __rest = this && this.__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 = this && this.__spreadArray || function (to, from) {
  for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) {
    to[j] = from[i];
  }

  return to;
};

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import CheckList, { TreeCheckList } from "../check-list";
import classnames from 'classnames';
import SortList from "../sort-list";
import "./style/transfer.css";
import useStaticCallback from "../../hooks/useStaticCallback";
import Scrollbar from "../scrollbar";
import find from 'lodash/find';
import map from 'lodash/map';
import { useMeasure } from 'react-use';
import _ from 'lodash';
var prefixCls = 'dmc-transfer';

var getChild = function getChild(arr) {
  var childs = [];
  arr.forEach(function (item) {
    var _a;

    if ((_a = item.children) === null || _a === void 0 ? void 0 : _a.length) {
      childs.splice.apply(childs, __spreadArray([-1, 0], getChild(item.children)));
    } else {
      childs.push(item);
    }
  });
  return childs;
};

function flatTree(array) {
  var arr = [];

  (function recurrence(array) {
    Array.from(array, function (item) {
      var _a;

      if ((_a = item.children) === null || _a === void 0 ? void 0 : _a.length) {
        arr.push(item);
        recurrence(item.children);
      } else {
        arr.push(item);
      }
    });
  })(array);

  return arr;
}

export var TransferMode;

(function (TransferMode) {
  TransferMode["plain"] = "plain";
  TransferMode["normal"] = "normal";
})(TransferMode || (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,
      showTip = _e.showTip,
      checkStrictly = _e.checkStrictly,
      noTreeIcon = _e.noTreeIcon,
      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", "showTip", "checkStrictly", "noTreeIcon"]);

  var isTree = options.some(function (opt) {
    var _a;

    return (_a = opt.children) === null || _a === void 0 ? void 0 : _a.length;
  });
  var cls = classnames(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 = useMeasure(),
      ref = _h[0],
      _j = _h[1],
      sortListWidth = _j.width,
      sortListHeight = _j.height;

  var optionArray = React.useMemo(function () {
    return checkStrictly ? flatTree(options) : getChild(options);
  }, [checkStrictly, options]);
  var sortlistVal = React.useMemo(function () {
    var sVal = [];
    val.forEach(function (v) {
      var item = find(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(function (selected) {
    if (!_.isArray(selected)) {
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      var newVal_1 = selected.checked;
      !isControl && setUnControlVal(newVal_1);
      onChange && onChange(newVal_1);
      return;
    }

    var newVal = selected;
    !isControl && setUnControlVal(newVal);
    onChange && onChange(newVal);
  });
  var onListRemove = useStaticCallback(function (selected) {
    var newVal = map(selected, function (val) {
      return val.key;
    });
    !isControl && setUnControlVal(newVal);
    onRemove && onRemove(newVal);
  });
  var handleClear = useStaticCallback(function () {
    !isControl && setUnControlVal([]);
    onRemove && onRemove([]);
  });
  return _jsxs("div", __assign({}, restProps, {
    className: cls,
    style: {
      width: width,
      height: height
    }
  }, {
    children: [isTree ? _jsx(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,
      checkStrictly: checkStrictly,
      noTreeIcon: noTreeIcon
    }, void 0) : _jsx(CheckList, {
      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,
      showTip: showTip
    }, void 0), _jsxs("div", __assign({
      className: classnames(prefixCls + "-checked", (_b = {}, _b[prefixCls + "-plain-sort"] = mode === TransferMode.plain, _b))
    }, {
      children: [_jsxs("div", __assign({
        className: prefixCls + "-checked-top-right"
      }, {
        children: [sortListDescriptionText && _jsx("span", __assign({
          className: prefixCls + "-sort-list-desc"
        }, {
          children: sortListDescriptionText
        }), void 0), _jsxs("span", __assign({
          className: classnames(prefixCls + "-checked-top-right-other", {
            'align-right': showCount && !allowClear || !showCount && allowClear,
            'space-between': showCount && allowClear
          })
        }, {
          children: [showCount && _jsx("div", __assign({
            className: prefixCls + "-checked-count"
          }, {
            children: countStr
          }), void 0), allowClear && val.length > 0 && _jsx("span", __assign({
            className: "clear",
            onClick: handleClear
          }, {
            children: allowClearText
          }), void 0)]
        }), void 0)]
      }), void 0), _jsx("div", __assign({
        className: classnames(prefixCls + "-checked-list", (_c = {}, _c[prefixCls + "-" + type] = type, _c))
      }, {
        children: _jsx(Scrollbar, __assign({
          type: type
        }, {
          children: _jsx("div", __assign({
            ref: ref,
            className: classnames(prefixCls + "-checked-list-wrapper", (_d = {}, _d[prefixCls + "-" + type] = type, _d[prefixCls + "-checked-list-plain"] = mode === TransferMode.plain, _d))
          }, {
            children: _jsx(SortList, {
              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;
export default Transfer;

