"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _icon = _interopRequireDefault(require("@teamix/icon"));

var _components = require("@alicloudfe/components");

var _utils = require("@teamix/utils");

require("./index.scss");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var CheckboxItem = _components.Menu.CheckboxItem;
var cls = (0, _utils.baseClass)('teamix-pro-table-toolbar-filter');

var Filter = function Filter(props) {
  var column = props.column,
      actionRef = props.actionRef; // 选中过滤项

  var _useState = (0, _react.useState)([]),
      _useState2 = _slicedToArray(_useState, 2),
      selected = _useState2[0],
      setSelected = _useState2[1];

  var _useState3 = (0, _react.useState)(false),
      _useState4 = _slicedToArray(_useState3, 2),
      visible = _useState4[0],
      setVisible = _useState4[1]; // 获取过滤项


  var filters = (0, _react.useMemo)(function () {
    var filters = column.options; // 转换 valueEnum 为 options

    if (column.valueEnum) {
      filters = Object.entries(column.valueEnum).map(function (_ref) {
        var _v$label, _v$color;

        var _ref2 = _slicedToArray(_ref, 2),
            k = _ref2[0],
            v = _ref2[1];

        return _objectSpread(_objectSpread({
          value: k
        }, v), {}, {
          label: (_v$label = v.label) !== null && _v$label !== void 0 ? _v$label : k,
          color: (_v$color = v.color) !== null && _v$color !== void 0 ? _v$color : k
        });
      });
    } // console.log(filters);


    return filters;
  }, []);
  return /*#__PURE__*/_react.default.createElement(_components.Dropdown, {
    trigger: /*#__PURE__*/_react.default.createElement(_icon.default, {
      size: "small",
      type: "filter-line",
      className: cls({
        icon: true,
        'icon-selected': selected.length > 0
      }),
      onClick: function onClick() {
        setVisible(!visible);
      }
    }),
    triggerType: ['click'],
    align: "tl bl",
    visible: visible,
    onVisibleChange: function onVisibleChange() {
      return setVisible(false);
    }
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: cls()
  }, /*#__PURE__*/_react.default.createElement(_components.Menu, null, filters === null || filters === void 0 ? void 0 : filters.map(function (_ref3) {
    var label = _ref3.label,
        value = _ref3.value;
    var valueStr = value.toString();
    return /*#__PURE__*/_react.default.createElement(CheckboxItem, {
      key: valueStr,
      checked: selected.includes(valueStr),
      onChange: function onChange(check) {
        if (check) {
          setSelected([].concat(_toConsumableArray(selected), [valueStr]));
        } else {
          setSelected(selected.filter(function (item) {
            return item !== valueStr;
          }));
        }
      }
    }, label);
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: cls({
      'btn-box': true
    })
  }, /*#__PURE__*/_react.default.createElement(_components.Button, {
    type: "primary",
    size: "small",
    className: cls({
      btn: true
    }),
    onClick: function onClick() {
      var _actionRef$current, _actionRef$current$se, _actionRef$current2, _actionRef$current2$f;

      (_actionRef$current = actionRef.current) === null || _actionRef$current === void 0 ? void 0 : (_actionRef$current$se = _actionRef$current.setFiltersDataSourceRules) === null || _actionRef$current$se === void 0 ? void 0 : _actionRef$current$se.call(_actionRef$current, selected);
      (_actionRef$current2 = actionRef.current) === null || _actionRef$current2 === void 0 ? void 0 : (_actionRef$current2$f = _actionRef$current2.filterDataSource) === null || _actionRef$current2$f === void 0 ? void 0 : _actionRef$current2$f.call(_actionRef$current2, column.dataIndex);
      setVisible(false);
    }
  }, "\u786E\u5B9A"), /*#__PURE__*/_react.default.createElement(_components.Button, {
    type: "normal",
    size: "small",
    className: cls({
      btn: true
    }),
    onClick: function onClick() {
      var _actionRef$current3, _actionRef$current3$s, _actionRef$current4, _actionRef$current4$f;

      setSelected([]);
      (_actionRef$current3 = actionRef.current) === null || _actionRef$current3 === void 0 ? void 0 : (_actionRef$current3$s = _actionRef$current3.setFiltersDataSourceRules) === null || _actionRef$current3$s === void 0 ? void 0 : _actionRef$current3$s.call(_actionRef$current3, []);
      (_actionRef$current4 = actionRef.current) === null || _actionRef$current4 === void 0 ? void 0 : (_actionRef$current4$f = _actionRef$current4.filterDataSource) === null || _actionRef$current4$f === void 0 ? void 0 : _actionRef$current4$f.call(_actionRef$current4, column.dataIndex); // setVisible(!visible);
    }
  }, "\u91CD\u7F6E")))));
};

var _default = Filter;
exports.default = _default;