"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 __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

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

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

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

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

var input_1 = __importDefault(require("../input"));

var tree_1 = __importDefault(require("../tree"));

var spin_1 = __importDefault(require("../spin"));

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

var useMeasure_1 = __importDefault(require("react-use/lib/useMeasure"));

var icon_1 = __importDefault(require("../icon"));

var cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));

require("./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_1["default"](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_1["default"](className, "" + prefixCls, (_a = {}, _a[prefixCls + "-flex-height"] = flexHeight, _a[prefixCls + "-" + type] = type, _a));

  var _e = useMeasure_1["default"](),
      ref = _e[0],
      eleHeight = _e[1].height;

  var onInputChange = useStaticCallback_1["default"](function (event) {
    var value = event.target.value.trim();
    setSearch(value);
  });
  var onCheckChange = useStaticCallback_1["default"](function (selected) {
    var newVal = selected;
    !isControl && setUnControlVal(newVal);
    onChange && onChange(newVal);
  });

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

  var calcHeight = flexHeight ? eleHeight : listHeight ? eleHeight : maxHeight;
  return jsx_runtime_1.jsx("div", __assign({
    className: cls,
    style: {
      width: width
    }
  }, {
    children: jsx_runtime_1.jsxs("div", __assign({
      className: prefixCls + "-wrap"
    }, {
      children: [!hideSearch && jsx_runtime_1.jsx("div", __assign({
        className: prefixCls + "-search"
      }, {
        children: jsx_runtime_1.jsx(input_1["default"], {
          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_runtime_1.jsx("div", __assign({
        className: prefixCls + "-icon-wrap"
      }, {
        children: jsx_runtime_1.jsx(spin_1["default"], {
          className: prefixCls + "-icon-loading"
        }, void 0)
      }), void 0) : jsx_runtime_1.jsxs("div", __assign({
        className: prefixCls + "-list"
      }, {
        children: [!!text && jsx_runtime_1.jsx("div", __assign({
          className: prefixCls + "-list-description"
        }, {
          children: text
        }), void 0), !!searchList.length ? jsx_runtime_1.jsx("div", __assign({
          className: prefixCls + "-list-tree",
          ref: ref,
          style: {
            height: listHeight
          }
        }, {
          children: !(flexHeight && !eleHeight) && jsx_runtime_1.jsx(tree_1["default"], __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_runtime_1.jsx(icon_1["default"], {
                icon: "folder",
                size: 18
              }, void 0) : null;
            },
            treeData: searchList
          }), void 0)
        }), void 0) : jsx_runtime_1.jsx(NoRowsRenderer, {}, void 0)]
      }), void 0)]
    }), void 0)
  }), void 0);
};

TreeCheckList.defaultProps = defaultProps;
TreeCheckList.displayName = 'TreeCheckList';
exports["default"] = TreeCheckList;
var TreeCheckListMemo = React.memo(TreeCheckList);
exports.TreeCheckListMemo = TreeCheckListMemo;
TreeCheckListMemo.displayName = 'TreeCheckListMemo';

