"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.CheckListMemo = void 0;

var react_1 = require("react");

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

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

var react_2 = require("react");

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

var pinyinUtil_1 = require("../_utils/pinyinUtil");

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

var formateOptions_1 = __importDefault(require("../_utils/formateOptions"));

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

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

var checkbox_1 = __importDefault(require("../checkbox"));

var virtualized_list_1 = __importDefault(require("../virtualized-list"));

require("./style/check-list.css");

var field_item_1 = __importDefault(require("../field-item")); // interface StateRefInterface {
//   selected: string[];
//   searchList: string[];
// }


var prefixCls = 'dmc-check-list';
var defaultProps = {
  maxHeight: 330,
  rowHeight: 36,
  options: [],
  valueKey: 'value',
  labelKey: 'label',
  // value: [],
  defaultValue: []
};

var plainRowRenderer = function plainRowRenderer(checked, title, _value, item, index) {
  return jsx_runtime_1.jsx("div", __assign({
    className: prefixCls + "-plain-row-wrap"
  }, {
    children: jsx_runtime_1.jsx(field_item_1["default"], {
      type: item.type,
      name: title,
      checked: checked
    }, index)
  }), void 0);
};

var CheckList = function CheckList(props) {
  var _a, _b;

  var _c = props,
      hideSearch = _c.hideSearch,
      type = _c.type,
      mode = _c.mode,
      text = _c.text,
      size = _c.size,
      listHeight = _c.listHeight,
      flexHeight = _c.flexHeight,
      loading = _c.loading,
      options = _c.options,
      valueKey = _c.valueKey,
      labelKey = _c.labelKey,
      value = _c.value,
      defaultValue = _c.defaultValue,
      allSelectLabel = _c.allSelectLabel,
      className = _c.className,
      listDescriptionClassName = _c.listDescriptionClassName,
      onChange = _c.onChange,
      maxHeight = _c.maxHeight,
      noContentText = _c.noContentText,
      noResultText = _c.noResultText,
      placeholder = _c.placeholder,
      filterList = _c.filterList,
      filterItem = _c.filterItem,
      rowHeight = _c.rowHeight,
      renderer = _c.renderer,
      radio = _c.radio,
      showCheckbox = _c.showCheckbox,
      dynamicRowHeight = _c.dynamicRowHeight,
      minRowHeight = _c.minRowHeight,
      searchExtra = _c.searchExtra,
      restProps = __rest(_c, ["hideSearch", "type", "mode", "text", "size", "listHeight", "flexHeight", "loading", "options", "valueKey", "labelKey", "value", "defaultValue", "allSelectLabel", "className", "listDescriptionClassName", "onChange", "maxHeight", "noContentText", "noResultText", "placeholder", "filterList", "filterItem", "rowHeight", "renderer", "radio", "showCheckbox", "dynamicRowHeight", "minRowHeight", "searchExtra"]);

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

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

  var opts = React.useMemo(function () {
    return formateOptions_1["default"](options, labelKey, valueKey);
  }, [options, labelKey, valueKey]);
  var pinyinOpts = React.useMemo(function () {
    return opts.map(function (opt) {
      return pinyinUtil_1.getFullChars(typeof opt === 'string' ? opt : (opt === null || opt === void 0 ? void 0 : opt[labelKey]) || '').toLowerCase();
    });
  }, [opts, labelKey]);
  var searchList = React.useMemo(function () {
    if (filterList) {
      return filterList(opts, search);
    }

    return search ? filterItem ? opts.filter(function (t) {
      return filterItem(t, search);
    }) : opts.filter(function (t, index) {
      return (t[labelKey] + " || ''").includes(search) || pinyinOpts[index].includes(search.toLowerCase());
    }) : opts;
  }, [search, filterList, opts, filterItem, labelKey, pinyinOpts]);
  var isControl = ('value' in props);
  var val = isControl ? radio ? (value || []).slice(0, 1) : value || [] : unControlVal;
  var sLen = searchList.length;
  var checkedAll = sLen > 0 && searchList.every(function (it) {
    return val.includes(it[valueKey]);
  });
  var indeterminate = sLen > 0 && searchList.some(function (it) {
    return val.includes(it[valueKey]);
  });
  react_2.useEffect(function () {
    if (radio && Array.isArray(value) && value.length > 1) {
      console.warn('the length of value is at most one when using radio');
    }

    if (allSelectLabel && radio) {
      console.warn('do not using allSelectLabel with radio');
    }
  }, [value, radio, allSelectLabel]);
  var onInputChange = useStaticCallback_1["default"](function (event) {
    var value = event.target.value.trim();
    setSearch(value);
  });
  var onCheckAllChange = useStaticCallback_1["default"](function (checked) {
    if (!searchList.length) return;
    var sk = searchList.map(function (it) {
      return it[valueKey];
    });
    var newVal = checked ? Array.from(new Set(__spreadArray(__spreadArray([], val), sk))) : val.filter(function (it) {
      return !sk.includes(it);
    });
    !isControl && setUnControlVal(newVal);
    onChange && onChange(newVal);
  });
  var onCheckChange = useStaticCallback_1["default"](function (checked, key) {
    var newVal;

    if (radio) {
      newVal = checked ? [key] : [];
    } else {
      newVal = checked ? __spreadArray(__spreadArray([], val), [key]) : val.filter(function (it) {
        return it !== key;
      });
    }

    !isControl && setUnControlVal(newVal);
    onChange && onChange(newVal);
  });
  var cls = classnames_1["default"](className, "" + prefixCls, (_a = {}, _a[prefixCls + "-flex-height"] = flexHeight, _a[prefixCls + "-" + type] = type, _a));

  var rowRenderer = function rowRenderer(_a) {
    var _b;

    var item = _a.item,
        style = _a.style,
        index = _a.index;
    var checked = val.includes(item[valueKey]);
    var checkBoxProps = {
      type: type,
      icon: !item.children && item.icon,
      className: prefixCls + "-checkbox",
      checked: !!checked,
      value: item[valueKey],
      onChange: onCheckChange,
      disabled: item.disabled
    };

    if (renderer || mode === 'plain') {
      // priority
      var _renderer = renderer || plainRowRenderer;

      return jsx_runtime_1.jsxs("div", __assign({
        style: style,
        className: classnames_1["default"](prefixCls + "-list-item", (_b = {}, _b[prefixCls + "-list-item-gap"] = mode === 'plain', _b)),
        onClick: function onClick() {
          onCheckChange(!checked, item[valueKey]);
        }
      }, {
        children: [showCheckbox && react_1.createElement(checkbox_1["default"], __assign({}, checkBoxProps, {
          size: "compact",
          key: index
        })), _renderer(checked, item[labelKey], item[valueKey], item, index)]
      }), void 0);
    }

    return jsx_runtime_1.jsx("div", __assign({
      style: style,
      className: prefixCls + "-list-item"
    }, {
      children: react_1.createElement(checkbox_1["default"], __assign({}, checkBoxProps, {
        size: "compact",
        title: item[labelKey],
        key: index
      }))
    }), void 0);
  };

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

  return jsx_runtime_1.jsx("div", __assign({}, restProps, {
    className: cls
  }, {
    children: jsx_runtime_1.jsxs("div", __assign({
      className: classnames_1["default"](prefixCls + "-wrap", (_b = {}, _b[prefixCls + "-plain"] = mode === 'plain', _b))
    }, {
      children: [!hideSearch && jsx_runtime_1.jsxs("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), searchExtra]
      }), 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 " + (listDescriptionClassName || '')
        }, {
          children: text
        }), void 0), !!allSelectLabel && !!!radio && jsx_runtime_1.jsx(checkbox_1["default"], {
          size: "compact",
          title: allSelectLabel,
          type: type,
          checked: checkedAll,
          onChange: onCheckAllChange,
          indeterminate: indeterminate,
          className: prefixCls + "-checkbox"
        }, void 0), jsx_runtime_1.jsx(virtualized_list_1["default"], {
          minRowHeight: minRowHeight,
          dynamicRowHeight: dynamicRowHeight,
          height: listHeight,
          type: type,
          data: searchList,
          rowHeight: mode === 'plain' ? 30 : rowHeight,
          ItemView: rowRenderer,
          className: prefixCls + "-virtual-list",
          noRowsRenderer: noRowsRenderer,
          maxHeight: flexHeight ? undefined : listHeight ? listHeight : maxHeight
        }, void 0)]
      }), void 0)]
    }), void 0)
  }), void 0);
};

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

