"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

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

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

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

var _Button = _interopRequireDefault(require("../button/Button"));

var _SelectTrigger = _interopRequireDefault(require("../rc-components/select/SelectTrigger"));

var _menu = require("../rc-components/menu");

var _util = require("../rc-components/select/util");

var _util2 = require("./util");

var _enum = require("../_util/enum");

var _ConfigContext = _interopRequireDefault(require("../config-provider/ConfigContext"));

var ColumnFilter = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(ColumnFilter, _Component);

  var _super = (0, _createSuper2["default"])(ColumnFilter);

  function ColumnFilter() {
    var _this;

    (0, _classCallCheck2["default"])(this, ColumnFilter);
    _this = _super.apply(this, arguments);
    _this.state = {
      open: false
    };

    _this.onMenuSelect = function (item) {
      item.item.props.value.hidden = false;

      _this.fireChange(item);
    };

    _this.onMenuDeselect = function (item) {
      item.item.props.value.hidden = true;

      _this.fireChange(item);
    };

    _this.onDropdownVisibleChange = function (open) {
      var _assertThisInitialize = (0, _assertThisInitialized2["default"])(_this),
          state = _assertThisInitialize.state;

      if (state.open !== open) {
        _this.setState({
          open: open
        });
      }
    };

    return _this;
  }

  (0, _createClass2["default"])(ColumnFilter, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          prefixCls = _this$props.prefixCls,
          getPopupContainer = _this$props.getPopupContainer;
      var open = this.state.open;
      var getPrefixCls = this.context.getPrefixCls;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-columns-chooser")
      }, /*#__PURE__*/_react["default"].createElement(_SelectTrigger["default"], {
        prefixCls: getPrefixCls('select'),
        showAction: ['click'],
        options: this.getOptions(),
        value: this.getVisibleColumns(),
        getPopupContainer: getPopupContainer,
        multiple: true,
        onDropdownVisibleChange: this.onDropdownVisibleChange,
        onMenuSelect: this.onMenuSelect,
        onMenuDeselect: this.onMenuDeselect,
        visible: open,
        popupPlacement: "bottomRight",
        dropdownMatchSelectWidth: false,
        dropdownStyle: {
          minWidth: 187
        }
      }, /*#__PURE__*/_react["default"].createElement(_Button["default"], {
        shape: "circle",
        icon: "view_column",
        size: _enum.Size.small
      })));
    }
  }, {
    key: "fireChange",
    value: function fireChange(item) {
      var onColumnFilterChange = this.props.onColumnFilterChange;

      if (onColumnFilterChange) {
        onColumnFilterChange(item);
      }
    }
  }, {
    key: "getOptions",
    value: function getOptions() {
      var columns = this.props.columns;
      var options = [];
      (columns || []).forEach(function (column, i) {
        var title = column.title,
            notDisplay = column.notDisplay,
            disableClick = column.disableClick;

        if (title && !notDisplay) {
          options.push( /*#__PURE__*/_react["default"].createElement(_menu.Item, {
            disabled: disableClick,
            style: _util.UNSELECTABLE_STYLE,
            attribute: _util.UNSELECTABLE_ATTRIBUTE,
            value: column,
            key: (0, _util2.getColumnKey)(column, i)
          }, title));
        }
      });
      return options;
    }
  }, {
    key: "getVisibleColumns",
    value: function getVisibleColumns() {
      var columns = this.props.columns;
      return (columns || []).filter(function (column) {
        return !column.hidden;
      });
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _ConfigContext["default"];
    }
  }]);
  return ColumnFilter;
}(_react.Component);

exports["default"] = ColumnFilter;
ColumnFilter.displayName = 'ColumnFilter';
//# sourceMappingURL=ColumnFilter.js.map
