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

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

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

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

var check_list_1 = __importDefault(require("../check-list"));

var CreateFilter_1 = __importDefault(require("./CreateFilter"));

require("./style/text-filter.css");

var filter_1 = __importDefault(require("lodash/filter"));

var isEmpty_1 = __importDefault(require("lodash/isEmpty"));

var isString_1 = __importDefault(require("lodash/isString"));

var map_1 = __importDefault(require("lodash/map"));

var prefixCls = 'dmc-text-filter';

var formatList = function formatList(list, emptyValText) {
  return map_1["default"](list, function (it) {
    return isEmpty_1["default"](it) && emptyValText ? {
      label: emptyValText,
      value: it
    } : isString_1["default"](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_1["default"](list, function (it) {
    return !isEmpty_1["default"](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_runtime_1.jsx(check_list_1["default"], {
      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_runtime_1.jsx(CreateFilter_1["default"], __assign({
    onOk: handleConfim,
    overlay: overlay,
    onClose: handleClose,
    footerClassName: prefixCls + "-footer",
    okButtonProps: cancelButtonProps,
    cancelButtonProps: cancelButtonProps,
    className: classnames_1["default"](prefixCls, className)
  }, restProps, {
    loading: false
  }, {
    children: children
  }), void 0);
};

TextFilter.displayName = 'TextFilter';
exports["default"] = TextFilter;
var defaultProps = {
  allSelectLabel: '全选',
  placement: 'bottomLeft',
  emptyValText: '空值'
};
TextFilter.defaultProps = defaultProps;
var TextFilterMemo = React.memo(TextFilter);
exports.TextFilterMemo = TextFilterMemo;
TextFilterMemo.displayName = 'TextFilterMemo';

