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 } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames';
import CheckList from "../check-list";
import CreateFilter from "./CreateFilter";
import "./style/text-filter.css";
import filter from 'lodash/filter';
import isEmpty from 'lodash/isEmpty';
import isString from 'lodash/isString';
import map from 'lodash/map';
var prefixCls = 'dmc-text-filter';

var formatList = function formatList(list, emptyValText) {
  return map(list, function (it) {
    return isEmpty(it) && emptyValText ? {
      label: emptyValText,
      value: it
    } : isString(it) ? {
      label: it,
      value: it
    } : it;
  });
};

var useState = React.useState,
    useCallback = React.useCallback,
    useMemo = React.useMemo,
    useRef = React.useRef,
    useEffect = React.useEffect;
var defaultValues = [];
var defaultList = [];

var TextFilter = function TextFilter(props) {
  var _a = props,
      _b = _a.values,
      values = _b === void 0 ? defaultValues : _b,
      _c = _a.list,
      list = _c === void 0 ? defaultList : _c,
      className = _a.className,
      children = _a.children,
      onOk = _a.onOk,
      onClose = _a.onClose,
      allSelectLabel = _a.allSelectLabel,
      loading = _a.loading,
      emptyValText = _a.emptyValText,
      radio = _a.radio,
      restProps = __rest(_a, ["values", "list", "className", "children", "onOk", "onClose", "allSelectLabel", "loading", "emptyValText", "radio"]);

  var compactList = filter(list, function (it) {
    return !isEmpty(it);
  });
  var isExistFalseVal = compactList.length !== list.length;
  var allList = isExistFalseVal ? __spreadArray([null], compactList) : list;
  var actualList = formatList(allList, emptyValText);

  var _d = useState(values),
      selected = _d[0],
      setSelected = _d[1];

  var stateRef = useRef({
    selected: selected
  });
  stateRef.current = {
    selected: selected
  };
  var handleConfim = useCallback(function () {
    if (onOk) {
      onOk(stateRef.current.selected);
    }
  }, [onOk]);
  var handleClose = useCallback(function () {
    if (onClose) {
      onClose();
    }

    setSelected(values);
  }, [onClose, values]);
  useEffect(function () {
    setSelected(values);
  }, [values]);
  var overlay = useMemo(function () {
    return _jsx(CheckList, {
      size: "compact",
      // type="menu-bg"
      loading: loading,
      onChange: setSelected,
      options: actualList,
      value: selected,
      allSelectLabel: allSelectLabel,
      className: prefixCls + "-checklist",
      radio: radio
    }, void 0);
  }, [loading, actualList, selected, allSelectLabel, radio]);
  var cancelButtonProps = {
    size: 'compact'
  };
  return _jsx(CreateFilter, __assign({
    onOk: handleConfim,
    overlay: overlay,
    onClose: handleClose,
    footerClassName: prefixCls + "-footer",
    okButtonProps: cancelButtonProps,
    cancelButtonProps: cancelButtonProps,
    className: classNames(prefixCls, className)
  }, restProps, {
    loading: false
  }, {
    children: children
  }), void 0);
};

TextFilter.displayName = 'TextFilter';
export default TextFilter;
var defaultProps = {
  allSelectLabel: '全选',
  placement: 'bottomLeft',
  emptyValText: '空值'
};
TextFilter.defaultProps = defaultProps;
var TextFilterMemo = /*#__PURE__*/React.memo(TextFilter);
TextFilterMemo.displayName = 'TextFilterMemo';
export { TextFilterMemo };

