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, jsxs as _jsxs } 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 { IconButton, LinkButton } from "../button";
import zhCn from "./languages/zh-CN";
import { TextFilterTypeSet } from "./__private__/constant";
import { isString, isEmpty, filter, map, first, get } from 'lodash';
import { translateFilterData, formatFilterData } from "./__private__/helper";
import FilterList from "./__private__/FilterList";
var prefixCls = 'dmc-text-filter';

var generateLabelValList = function generateLabelValList(list, emptyValText) {
  if (emptyValText === void 0) {
    emptyValText = '空值';
  }

  return map(list, function (it) {
    return isEmpty(it) ? {
      label: emptyValText,
      value: it
    } : isString(it) ? {
      label: it,
      value: it
    } : it;
  });
};
/**
 * 将多个空值汇聚成一个，加上剩下的list并返回
 * @param list 传入的list,可能是 (string | null)[]| LabelValueItemInterface[]
 * @param emptyValText 空值的显示label ，默认为空值
 * @returns
 */


var formatList = function formatList(list, emptyValText) {
  //以第一个item作为判断是string或者LabelValueItemInterface
  var firstItem = first(list); //如果是(string | null [])

  if (!get(firstItem, 'value')) {
    var compactList = filter(list, function (it) {
      return !isEmpty(it);
    });
    var isExistFalseVal = compactList.length !== list.length;
    var allList = isExistFalseVal ? __spreadArray([null], compactList) : list;
    return generateLabelValList(allList, emptyValText);
  } else {
    var compactList = filter(list, function (it) {
      return !isEmpty(it.value);
    });
    var isExistFalseVal = compactList.length !== list.length;
    var allList = isExistFalseVal ? __spreadArray([{
      label: emptyValText,
      value: null
    }], compactList) : list;
    return allList;
  }
};

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;

  var _b = props,
      _c = _b.values,
      values = _c === void 0 ? defaultValues : _c,
      _d = _b.list,
      list = _d === void 0 ? defaultList : _d,
      filterData = _b.filterData,
      filterTitle = _b.filterTitle,
      className = _b.className,
      children = _b.children,
      onOk = _b.onOk,
      onClose = _b.onClose,
      allSelectLabel = _b.allSelectLabel,
      loading = _b.loading,
      emptyValText = _b.emptyValText,
      radio = _b.radio,
      type = _b.type,
      locale = _b.locale,
      listHeight = _b.listHeight,
      searchOptions = _b.searchOptions,
      onSearch = _b.onSearch,
      onVisibleChange = _b.onVisibleChange,
      restProps = __rest(_b, ["values", "list", "filterData", "filterTitle", "className", "children", "onOk", "onClose", "allSelectLabel", "loading", "emptyValText", "radio", "type", "locale", "listHeight", "searchOptions", "onSearch", "onVisibleChange"]);

  var trans = useMemo(function () {
    return type && filterData ? translateFilterData(filterData) : [];
  }, [filterData, type]);

  var _e = useState(type),
      _type = _e[0],
      setType = _e[1];

  var actualList = formatList(list, emptyValText);
  var isFilterMode = _type === TextFilterTypeSet.filter;

  var _f = useState(trans),
      fData = _f[0],
      setFilterData = _f[1];

  var filterRef = useRef({
    filterData: fData
  });
  filterRef.current = {
    filterData: fData
  };

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

  var stateRef = useRef({
    selected: selected
  });
  stateRef.current = {
    selected: selected
  };
  var handleConfim = useCallback(function () {
    onOk && onOk(stateRef.current.selected, _type ? formatFilterData(filterRef.current.filterData) : undefined);
  }, [onOk, _type]);
  var handleClose = useCallback(function () {
    if (onClose) {
      onClose();
    }

    if (TextFilterTypeSet.standard) {
      setSelected(values);
    }

    if (TextFilterTypeSet.filter) {
      setFilterData(trans);
    }
  }, [onClose, trans, values]);
  useEffect(function () {
    setSelected(values);
  }, [values]); // =============== Render ===============

  var extraNode = useMemo(function () {
    return _type ? _jsx(IconButton, {
      icon: "maximize-2",
      style: {
        marginLeft: '4px'
      },
      onClick: function onClick() {
        setType(TextFilterTypeSet.filter);
      }
    }, void 0) : undefined;
  }, [_type]);
  var overlay = useMemo(function () {
    return _jsxs(React.Fragment, {
      children: [(!_type || _type === TextFilterTypeSet.standard) && _jsx(CheckList, {
        size: "compact",
        // type="menu-bg"
        loading: loading,
        onChange: setSelected,
        options: actualList,
        value: selected,
        allSelectLabel: allSelectLabel,
        className: prefixCls + "-checklist",
        radio: radio,
        searchExtra: extraNode,
        listHeight: listHeight,
        showTip: true,
        onSearch: onSearch
      }, void 0), _type && isFilterMode && _jsxs("div", __assign({
        className: prefixCls + "-filter"
      }, {
        children: [filterTitle && _jsx("div", {
          children: filterTitle
        }, void 0), _jsx(FilterList, {
          locale: locale,
          searchOptions: searchOptions,
          dataSource: fData,
          onChange: setFilterData
        }, void 0)]
      }), void 0)]
    }, void 0);
  }, [_type, loading, actualList, selected, allSelectLabel, radio, extraNode, listHeight, onSearch, isFilterMode, filterTitle, locale, searchOptions, fData]);
  var cancelButtonProps = {
    size: 'compact'
  };
  return _jsx(CreateFilter, __assign({
    onOk: handleConfim,
    overlay: overlay,
    onVisibleChange: onVisibleChange,
    onClose: handleClose,
    footerClassName: classNames((_a = {}, _a[prefixCls + "-footer"] = true, _a[prefixCls + "-footer-noBorder"] = isFilterMode, _a)),
    footerPrefixEl: function footerPrefixEl() {
      return isFilterMode && _jsx(LinkButton, __assign({
        size: "compact",
        className: prefixCls + "-footer-btn",
        onClick: function onClick() {
          setType(TextFilterTypeSet.standard);
        }
      }, {
        children: "\u8FD4\u56DE\u6807\u51C6\u6A21\u5F0F"
      }), void 0);
    },
    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: '空值',
  locale: zhCn
};
TextFilter.defaultProps = defaultProps;
var TextFilterMemo = /*#__PURE__*/React.memo(TextFilter);
TextFilterMemo.displayName = 'TextFilterMemo';
export { TextFilterMemo };

