import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import React, { cloneElement, Component } from 'react';
import { findDOMNode } from 'react-dom';
import closest from 'dom-closest';
import classNames from 'classnames';
import shallowequal from 'shallowequal';
import Dropdown from '../dropdown';
import Icon from '../icon';
import Checkbox from '../checkbox';
import Radio from '../radio';
import FilterDropdownMenuWrapper from './FilterDropdownMenuWrapper';
import Menu, { Item as MenuItem, SubMenu } from '../rc-components/menu';

var FilterMenu = /*#__PURE__*/function (_Component) {
  _inherits(FilterMenu, _Component);

  var _super = _createSuper(FilterMenu);

  function FilterMenu(props) {
    var _this;

    _classCallCheck(this, FilterMenu);

    _this = _super.call(this, props);

    _this.setNeverShown = function (column) {
      var rootNode = findDOMNode(_assertThisInitialized(_this));
      var prefixCls = _this.props.prefixCls;
      var filterBelongToScrollBody = !!closest(rootNode, "".concat(prefixCls, "-scroll"));

      if (filterBelongToScrollBody) {
        // When fixed column have filters, there will be two dropdown menus
        // Filter dropdown menu inside scroll body should never be shown
        _this.neverShown = !!column.fixed;
      }
    };

    _this.setSelectedKeys = function (_ref) {
      var selectedKeys = _ref.selectedKeys;

      _this.setState({
        selectedKeys: selectedKeys
      });
    };

    _this.handleClearFilters = function () {
      _this.setState({
        selectedKeys: []
      }, _this.handleConfirm);
    };

    _this.handleConfirm = function () {
      _this.setVisible(false);

      _this.confirmFilter();
    };

    _this.onVisibleChange = function (visible) {
      _this.setVisible(visible);

      if (!visible) {
        _this.confirmFilter();
      }
    };

    _this.handleFilterDropdownMenuClick = function (e) {
      e.preventDefault();
    };

    _this.handleMenuItemClick = function (info) {
      if (!info.keyPath || info.keyPath.length <= 1) {
        return;
      }

      var _this$state = _this.state,
          keyPathOfSelectedItem = _this$state.keyPathOfSelectedItem,
          selectedKeys = _this$state.selectedKeys;

      if (selectedKeys.indexOf(info.key) >= 0) {
        // deselect SubMenu child
        delete keyPathOfSelectedItem[info.key];
      } else {
        // select SubMenu child
        keyPathOfSelectedItem[info.key] = info.keyPath;
      }

      _this.setState({
        keyPathOfSelectedItem: keyPathOfSelectedItem
      });
    };

    _this.renderFilterIcon = function () {
      var _this$props = _this.props,
          column = _this$props.column,
          locale = _this$props.locale,
          prefixCls = _this$props.prefixCls,
          selectedKeys = _this$props.selectedKeys;
      var filterd = selectedKeys.length > 0;
      var filterIcon = column.filterIcon;

      if (typeof filterIcon === 'function') {
        filterIcon = filterIcon(filterd);
      }

      var dropdownSelectedClass = filterd ? "".concat(prefixCls, "-selected") : '';
      return filterIcon ? /*#__PURE__*/cloneElement(filterIcon, {
        title: locale.filterTitle,
        className: classNames("".concat(prefixCls, "-icon"), filterIcon.props.className)
      }) : /*#__PURE__*/React.createElement(Icon, {
        title: locale.filterTitle,
        type: "filter_list",
        className: dropdownSelectedClass
      });
    };

    var visible = 'filterDropdownVisible' in props.column ? props.column.filterDropdownVisible : false;
    _this.state = {
      selectedKeys: props.selectedKeys,
      keyPathOfSelectedItem: {},
      visible: visible
    };
    return _this;
  }

  _createClass(FilterMenu, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var column = this.props.column;
      this.setNeverShown(column);
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      var column = nextProps.column;
      this.setNeverShown(column);
      var newState = {};
      var selectedKeys = this.props.selectedKeys;

      if ('selectedKeys' in nextProps && !shallowequal(selectedKeys, nextProps.selectedKeys)) {
        newState.selectedKeys = nextProps.selectedKeys;
      }

      if ('filterDropdownVisible' in column) {
        newState.visible = column.filterDropdownVisible;
      }

      if (Object.keys(newState).length > 0) {
        this.setState(newState);
      }
    }
  }, {
    key: "setVisible",
    value: function setVisible(visible) {
      var column = this.props.column;

      if (!('filterDropdownVisible' in column)) {
        this.setState({
          visible: visible
        });
      }

      if (column.onFilterDropdownVisibleChange) {
        column.onFilterDropdownVisibleChange(visible);
      }
    }
  }, {
    key: "confirmFilter",
    value: function confirmFilter() {
      var _this$props2 = this.props,
          propSelectedKeys = _this$props2.selectedKeys,
          column = _this$props2.column,
          confirmFilter = _this$props2.confirmFilter;
      var selectedKeys = this.state.selectedKeys;

      if (!shallowequal(selectedKeys, propSelectedKeys)) {
        confirmFilter(column, selectedKeys);
      }
    }
  }, {
    key: "renderMenuItem",
    value: function renderMenuItem(item) {
      var _this$props3 = this.props,
          column = _this$props3.column,
          radioPrefixCls = _this$props3.radioPrefixCls,
          checkboxPrefixCls = _this$props3.checkboxPrefixCls;
      var selectedKeys = this.state.selectedKeys;
      var multiple = 'filterMultiple' in column ? column.filterMultiple : true;
      var input = multiple ? /*#__PURE__*/React.createElement(Checkbox, {
        prefixCls: checkboxPrefixCls,
        checked: selectedKeys.indexOf(item.value.toString()) >= 0
      }) : /*#__PURE__*/React.createElement(Radio, {
        prefixCls: radioPrefixCls,
        checked: selectedKeys.indexOf(item.value.toString()) >= 0
      });
      return /*#__PURE__*/React.createElement(MenuItem, {
        key: item.value,
        checkable: false
      }, input, /*#__PURE__*/React.createElement("span", null, item.text));
    }
  }, {
    key: "hasSubMenu",
    value: function hasSubMenu() {
      var _this$props$column$fi = this.props.column.filters,
          filters = _this$props$column$fi === void 0 ? [] : _this$props$column$fi;
      return filters.some(function (item) {
        return !!(item.children && item.children.length > 0);
      });
    }
  }, {
    key: "renderMenus",
    value: function renderMenus(items) {
      var _this2 = this;

      return items.map(function (item) {
        if (item.children && item.children.length > 0) {
          var dropdownPrefixCls = _this2.props.dropdownProps.prefixCls;
          var keyPathOfSelectedItem = _this2.state.keyPathOfSelectedItem;
          var containSelected = Object.keys(keyPathOfSelectedItem).some(function (key) {
            return keyPathOfSelectedItem[key].indexOf(item.value) >= 0;
          });
          var subMenuCls = containSelected ? "".concat(dropdownPrefixCls, "-submenu-contain-selected") : '';
          return /*#__PURE__*/React.createElement(SubMenu, {
            title: item.text,
            className: subMenuCls,
            key: item.value.toString()
          }, _this2.renderMenus(item.children));
        }

        return _this2.renderMenuItem(item);
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var _this$props4 = this.props,
          column = _this$props4.column,
          locale = _this$props4.locale,
          prefixCls = _this$props4.prefixCls,
          dropdownProps = _this$props4.dropdownProps,
          getPopupContainer = _this$props4.getPopupContainer,
          rippleDisabled = _this$props4.rippleDisabled;
      var _this$state2 = this.state,
          visible = _this$state2.visible,
          selectedKeys = _this$state2.selectedKeys;
      var dropdownPrefixCls = dropdownProps.prefixCls; // default multiple selection in filter dropdown

      var multiple = 'filterMultiple' in column ? column.filterMultiple : true;
      var dropdownMenuClass = classNames(_defineProperty({}, "".concat(dropdownPrefixCls, "-menu-without-submenu"), !this.hasSubMenu()));
      var filterDropdown = column.filterDropdown;

      if (filterDropdown && typeof filterDropdown === 'function') {
        filterDropdown = filterDropdown({
          prefixCls: "".concat(dropdownPrefixCls, "-custom"),
          setSelectedKeys: function setSelectedKeys($selectedKeys) {
            return _this3.setSelectedKeys({
              selectedKeys: $selectedKeys
            });
          },
          selectedKeys: selectedKeys,
          confirm: this.handleConfirm,
          clearFilters: this.handleClearFilters,
          filters: column.filters,
          getPopupContainer: function getPopupContainer(triggerNode) {
            return triggerNode.parentNode;
          }
        });
      }

      var menus = filterDropdown ? /*#__PURE__*/React.createElement(FilterDropdownMenuWrapper, {
        onClick: this.handleFilterDropdownMenuClick
      }, filterDropdown) : /*#__PURE__*/React.createElement(FilterDropdownMenuWrapper, {
        className: "".concat(prefixCls, "-dropdown"),
        onClick: this.handleFilterDropdownMenuClick
      }, /*#__PURE__*/React.createElement(Menu, {
        multiple: multiple,
        rippleDisabled: rippleDisabled,
        onClick: this.handleMenuItemClick,
        prefixCls: "".concat(dropdownPrefixCls, "-menu"),
        className: dropdownMenuClass,
        onSelect: this.setSelectedKeys,
        onDeselect: this.setSelectedKeys,
        selectedKeys: selectedKeys,
        getPopupContainer: function getPopupContainer(triggerNode) {
          return triggerNode.parentNode;
        }
      }, this.renderMenus(column.filters)), /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-dropdown-btns")
      }, /*#__PURE__*/React.createElement("a", {
        className: "".concat(prefixCls, "-dropdown-link confirm"),
        onClick: this.handleConfirm
      }, locale.filterConfirm), /*#__PURE__*/React.createElement("a", {
        className: "".concat(prefixCls, "-dropdown-link clear"),
        onClick: this.handleClearFilters
      }, locale.filterReset)));
      return /*#__PURE__*/React.createElement(Dropdown, _extends({}, dropdownProps, {
        trigger: ['click'],
        overlay: menus,
        visible: this.neverShown ? false : visible,
        onVisibleChange: this.onVisibleChange,
        getPopupContainer: getPopupContainer,
        forceRender: true
      }), this.renderFilterIcon());
    }
  }]);

  return FilterMenu;
}(Component);

export { FilterMenu as default };
FilterMenu.defaultProps = {
  handleFilter: function handleFilter() {},
  column: {}
};
//# sourceMappingURL=filterDropdown.js.map
