function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useEffect, useMemo, useState } from "react";
import { Button, Checkbox, Space, Menu } from "antd";
import FilterFunnel01Fill from "@pisell/icon/es/FilterFunnel01Fill";
import FilterFunnel01 from "@pisell/icon/es/FilterFunnel01";
import { getText } from "../../../locales";
import "./index.less";
var FilterDropdown = function FilterDropdown(props) {
  var setSelectedKeys = props.setSelectedKeys,
    selectedKeys = props.selectedKeys,
    confirm = props.confirm,
    clearFilters = props.clearFilters,
    filters = props.filters;
  var _useState = useState((selectedKeys === null || selectedKeys === void 0 ? void 0 : selectedKeys[0]) || []),
    _useState2 = _slicedToArray(_useState, 2),
    value = _useState2[0],
    setValue = _useState2[1];
  useEffect(function () {
    setValue((selectedKeys === null || selectedKeys === void 0 ? void 0 : selectedKeys[0]) || []);
  }, [selectedKeys === null || selectedKeys === void 0 ? void 0 : selectedKeys[0]]);
  var items = useMemo(function () {
    return filters.map(function (item) {
      return {
        label: /*#__PURE__*/React.createElement("span", {
          onClick: function onClick(e) {
            // 防止menu onClick触发两次
            e.preventDefault();
          }
        }, /*#__PURE__*/React.createElement(Checkbox, {
          checked: value.includes(item.value)
        }, item.text)),
        key: item.value
      };
    });
  }, [filters, value]);
  var handleClick = function handleClick(_ref) {
    var key = _ref.key;
    setValue(function (v) {
      if (v.includes(key)) {
        return v.filter(function (item) {
          return item !== key;
        });
      }
      return [].concat(_toConsumableArray(v), [key]);
    });
  };
  useEffect(function () {
    setSelectedKeys(value !== null && value !== void 0 && value.length ? [value] : []);
  }, [value]);
  return /*#__PURE__*/React.createElement("div", {
    onKeyDown: function onKeyDown(e) {
      return e.stopPropagation();
    },
    className: "pisell-table-filter-dropdown"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-table-filter-dropdown-top",
    style: {
      padding: 0
    }
  }, /*#__PURE__*/React.createElement(Menu, {
    onClick: handleClick,
    items: items,
    selectedKeys: [],
    style: {
      width: 272
    }
  })), /*#__PURE__*/React.createElement(Space, {
    className: "pisell-table-filter-dropdown-bottom",
    styles: {
      item: {
        flex: 1
      }
    }
  }, /*#__PURE__*/React.createElement(Button, {
    onClick: function onClick() {
      clearFilters && clearFilters();
      confirm();
    },
    block: true,
    style: {
      flex: 1
    }
  }, getText("table-filter-clear")), /*#__PURE__*/React.createElement(Button, {
    type: "primary",
    onClick: function onClick() {
      return confirm();
    },
    block: true,
    style: {
      flex: 1
    }
  }, getText("table-filter-filter"))));
};
var getColumnSelectProps = function getColumnSelectProps(_ref2) {
  var dataIndex = _ref2.dataIndex,
    filters = _ref2.filters;
  return {
    filterDropdown: function filterDropdown(props) {
      return /*#__PURE__*/React.createElement(FilterDropdown, _extends({}, props, {
        filters: filters
      }));
    },
    filterIcon: function filterIcon(filtered) {
      return filtered ? /*#__PURE__*/React.createElement(FilterFunnel01Fill, {
        style: {
          color: "#7f56d9",
          fontSize: 14
        }
      }) : /*#__PURE__*/React.createElement(FilterFunnel01, {
        style: {
          color: "#98A2B3",
          fontSize: 14
        }
      });
    }
  };
};
export default getColumnSelectProps;