"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 button_1 = require("../button");

var zh_CN_1 = __importDefault(require("./languages/zh-CN"));

var constant_1 = require("./__private__/constant");

var lodash_1 = require("lodash");

var helper_1 = require("./__private__/helper");

var FilterList_1 = __importDefault(require("./__private__/FilterList"));

var prefixCls = 'dmc-text-filter';

var formatList = function formatList(list, emptyValText) {
  return lodash_1.map(list, function (it) {
    return lodash_1.isEmpty(it) && emptyValText ? {
      label: emptyValText,
      value: it
    } : lodash_1.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;

  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,
      searchOptions = _b.searchOptions,
      restProps = __rest(_b, ["values", "list", "filterData", "filterTitle", "className", "children", "onOk", "onClose", "allSelectLabel", "loading", "emptyValText", "radio", "type", "locale", "searchOptions"]);

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

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

  var compactList = lodash_1.filter(list, function (it) {
    return !lodash_1.isEmpty(it);
  });
  var isExistFalseVal = compactList.length !== list.length;
  var allList = isExistFalseVal ? __spreadArray([null], compactList) : list;
  var actualList = formatList(allList, emptyValText);
  var isFilterMode = _type === constant_1.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 ? helper_1.formatFilterData(filterRef.current.filterData) : undefined);
  }, [onOk, _type]);
  var handleClose = useCallback(function () {
    if (onClose) {
      onClose();
    }

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

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

  var extraNode = useMemo(function () {
    return _type ? jsx_runtime_1.jsx(button_1.IconButton, {
      icon: "maximize-2",
      style: {
        marginLeft: '4px'
      },
      onClick: function onClick() {
        setType(constant_1.TextFilterTypeSet.filter);
      }
    }, void 0) : undefined;
  }, [_type]);
  var overlay = useMemo(function () {
    return jsx_runtime_1.jsxs(React.Fragment, {
      children: [(!_type || _type === constant_1.TextFilterTypeSet.standard) && 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,
        searchExtra: extraNode
      }, void 0), _type && isFilterMode && jsx_runtime_1.jsxs("div", __assign({
        className: prefixCls + "-filter"
      }, {
        children: [filterTitle && jsx_runtime_1.jsx("div", {
          children: filterTitle
        }, void 0), jsx_runtime_1.jsx(FilterList_1["default"], {
          locale: locale,
          searchOptions: searchOptions,
          dataSource: fData,
          onChange: setFilterData
        }, void 0)]
      }), void 0)]
    }, void 0);
  }, [_type, loading, actualList, selected, allSelectLabel, radio, extraNode, isFilterMode, filterTitle, locale, searchOptions, fData]);
  var cancelButtonProps = {
    size: 'compact'
  };
  return jsx_runtime_1.jsx(CreateFilter_1["default"], __assign({
    onOk: handleConfim,
    overlay: overlay,
    onClose: handleClose,
    footerClassName: classnames_1["default"]((_a = {}, _a[prefixCls + "-footer"] = true, _a[prefixCls + "-footer-noBorder"] = isFilterMode, _a)),
    footerPrefixEl: function footerPrefixEl() {
      return isFilterMode && jsx_runtime_1.jsx(button_1.LinkButton, __assign({
        size: "compact",
        className: prefixCls + "-footer-btn",
        onClick: function onClick() {
          setType(constant_1.TextFilterTypeSet.standard);
        }
      }, {
        children: "\u8FD4\u56DE\u6807\u51C6\u6A21\u5F0F"
      }), void 0);
    },
    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: '空值',
  locale: zh_CN_1["default"]
};
TextFilter.defaultProps = defaultProps;
var TextFilterMemo = React.memo(TextFilter);
exports.TextFilterMemo = TextFilterMemo;
TextFilterMemo.displayName = 'TextFilterMemo';

