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;
};

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import classnames from 'classnames';
import Input from "../input";
import Tree from "../tree";
import Spin from "../spin";
import useStaticCallback from "../../hooks/useStaticCallback";
import useMeasure from 'react-use/lib/useMeasure';
import Icon from "../icon";
import cloneDeep from 'lodash/cloneDeep';
import "./style/check-list.css";
var prefixCls = 'dmc-check-list';
var defaultProps = {
  checkable: true,
  maxHeight: 330,
  options: [],
  value: [],
  defaultValue: []
};

var searchTree = function searchTree(options, search, filterItem) {
  if (filterItem === void 0) {
    filterItem = function filterItem(val, search) {
      return val.title.indexOf(search) > -1;
    };
  }

  var opts = cloneDeep(options);
  var len = opts.length;

  for (var i = 0; i < len; i++) {
    var item = opts[i];

    if (item) {
      if (item.children) {
        var child = searchTree(item.children, search);

        if (child.length) {
          opts[i].children = child;
        } else {
          if (!filterItem(item, search)) {
            opts.splice(i, 1);
            i--;
          }
        }
      } else {
        if (!filterItem(item, search)) {
          opts.splice(i, 1);
          i--;
        }
      }
    }
  }

  return opts;
};

var TreeCheckList = function TreeCheckList(props) {
  var _a;

  var _b = props,
      hideSearch = _b.hideSearch,
      type = _b.type,
      text = _b.text,
      size = _b.size,
      flexHeight = _b.flexHeight,
      loading = _b.loading,
      options = _b.options,
      value = _b.value,
      defaultValue = _b.defaultValue,
      className = _b.className,
      onChange = _b.onChange,
      noContentText = _b.noContentText,
      noResultText = _b.noResultText,
      placeholder = _b.placeholder,
      filterList = _b.filterList,
      filterItem = _b.filterItem,
      width = _b.width,
      listHeight = _b.listHeight,
      maxHeight = _b.maxHeight,
      checkable = _b.checkable,
      restProps = __rest(_b, ["hideSearch", "type", "text", "size", "flexHeight", "loading", "options", "value", "defaultValue", "className", "onChange", "noContentText", "noResultText", "placeholder", "filterList", "filterItem", "width", "listHeight", "maxHeight", "checkable"]);

  var _c = React.useState(''),
      search = _c[0],
      setSearch = _c[1];

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

  var searchList = React.useMemo(function () {
    return search ? filterList ? filterList(options, search) : filterItem ? searchTree(options, search, filterItem) : searchTree(options, search) : options;
  }, [search, filterList, options, filterItem]);
  var isControl = ('value' in props);
  var val = isControl ? value : unControlVal;
  var cls = classnames(className, "" + prefixCls, (_a = {}, _a[prefixCls + "-flex-height"] = flexHeight, _a[prefixCls + "-" + type] = type, _a));

  var _e = useMeasure(),
      ref = _e[0],
      eleHeight = _e[1].height;

  var onInputChange = useStaticCallback(function (event) {
    var value = event.target.value.trim();
    setSearch(value);
  });
  var onCheckChange = useStaticCallback(function (selected) {
    var newVal = selected;
    !isControl && setUnControlVal(newVal);
    onChange && onChange(newVal);
  });

  var NoRowsRenderer = function NoRowsRenderer() {
    var text = !options.length ? noContentText : noResultText;
    return _jsx("div", __assign({
      className: prefixCls + "-no-content"
    }, {
      children: text
    }), void 0);
  };

  var calcHeight = flexHeight ? eleHeight : listHeight ? eleHeight : maxHeight;
  return _jsx("div", __assign({
    className: cls,
    style: {
      width: width
    }
  }, {
    children: _jsxs("div", __assign({
      className: prefixCls + "-wrap"
    }, {
      children: [!hideSearch && _jsx("div", __assign({
        className: prefixCls + "-search"
      }, {
        children: _jsx(Input, {
          value: search,
          block: true,
          allowClear: true,
          type: type,
          size: size,
          prefixIcon: "search",
          placeholder: placeholder,
          className: prefixCls + "-search-input",
          onChange: onInputChange
        }, void 0)
      }), void 0), loading ? _jsx("div", __assign({
        className: prefixCls + "-icon-wrap"
      }, {
        children: _jsx(Spin, {
          className: prefixCls + "-icon-loading"
        }, void 0)
      }), void 0) : _jsxs("div", __assign({
        className: prefixCls + "-list"
      }, {
        children: [!!text && _jsx("div", __assign({
          className: prefixCls + "-list-description"
        }, {
          children: text
        }), void 0), !!searchList.length ? _jsx("div", __assign({
          className: prefixCls + "-list-tree",
          ref: ref,
          style: {
            height: listHeight
          }
        }, {
          children: !(flexHeight && !eleHeight) && _jsx(Tree, __assign({}, restProps, {
            checkable: checkable,
            height: calcHeight,
            selectable: false,
            onCheck: onCheckChange,
            checkedKeys: val,
            icon: function icon(props) {
              var _a;

              return ((_a = props.data) === null || _a === void 0 ? void 0 : _a.children) ? _jsx(Icon, {
                icon: "folder",
                size: 18
              }, void 0) : null;
            },
            treeData: searchList
          }), void 0)
        }), void 0) : _jsx(NoRowsRenderer, {}, void 0)]
      }), void 0)]
    }), void 0)
  }), void 0);
};

TreeCheckList.defaultProps = defaultProps;
TreeCheckList.displayName = 'TreeCheckList';
export default TreeCheckList;
var TreeCheckListMemo = /*#__PURE__*/React.memo(TreeCheckList);
TreeCheckListMemo.displayName = 'TreeCheckListMemo';
export { TreeCheckListMemo };

