"use strict";

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

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

var _index = require("../button/index.js");

var _overrides = require("../helpers/overrides.js");

var _filter = _interopRequireDefault(require("../icon/filter.js"));

var _index2 = require("../popover/index.js");

var _styledComponents = require("./styled-components.js");

var _trapFocus = _interopRequireDefault(require("./trap-focus.js"));

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

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

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, 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) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_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; }

function Filter(props) {
  var _props$onSelectAll = props.onSelectAll,
      onSelectAll = _props$onSelectAll === void 0 ? function () {} : _props$onSelectAll,
      _props$onReset = props.onReset,
      onReset = _props$onReset === void 0 ? function () {} : _props$onReset,
      _props$overrides = props.overrides,
      overrides = _props$overrides === void 0 ? {} : _props$overrides;

  var _getOverrides = (0, _overrides.getOverrides)(overrides.MenuButton, _styledComponents.StyledFilterButton),
      _getOverrides2 = _slicedToArray(_getOverrides, 2),
      MenuButton = _getOverrides2[0],
      menuButtonProps = _getOverrides2[1];

  var _getOverrides3 = (0, _overrides.getOverrides)(overrides.Content, _styledComponents.StyledFilterContent),
      _getOverrides4 = _slicedToArray(_getOverrides3, 2),
      Content = _getOverrides4[0],
      contentProps = _getOverrides4[1];

  var _getOverrides5 = (0, _overrides.getOverrides)(overrides.Heading, _styledComponents.StyledFilterHeading),
      _getOverrides6 = _slicedToArray(_getOverrides5, 2),
      Heading = _getOverrides6[0],
      headingProps = _getOverrides6[1];

  var _getOverrides7 = (0, _overrides.getOverrides)(overrides.Footer, _styledComponents.StyledFilterFooter),
      _getOverrides8 = _slicedToArray(_getOverrides7, 2),
      Footer = _getOverrides8[0],
      footerProps = _getOverrides8[1];

  function getIconColor(theme) {
    if (props.disabled) {
      return theme.colors.mono500;
    }

    if (props.active) {
      return theme.colors.colorPrimary;
    }

    return theme.colors.tableFilter;
  }

  function getIconHoverColor(theme) {
    if (props.disabled || props.active) {
      return null;
    }

    return theme.colors.colorPrimary;
  }

  return React.createElement(_index2.StatefulPopover, {
    placement: _index2.PLACEMENT.bottom,
    stateReducer: function stateReducer(_, nextState) {
      if (props.disabled) {
        return _objectSpread({}, nextState, {
          isOpen: false
        });
      }

      return nextState;
    },
    content: React.createElement(_trapFocus.default, null, React.createElement(Heading, headingProps, "Filter Column"), React.createElement(Content, contentProps, props.children), React.createElement(Footer, footerProps, React.createElement(_index.Button, {
      kind: _index.KIND.minimal,
      size: _index.SIZE.compact,
      onClick: function onClick() {
        onSelectAll();
      }
    }, "Select All"), React.createElement(_index.Button, {
      kind: _index.KIND.minimal,
      size: _index.SIZE.compact,
      onClick: function onClick() {
        onReset();
      }
    }, "Reset")))
  }, React.createElement(MenuButton, menuButtonProps, React.createElement(_filter.default, {
    overrides: {
      Svg: {
        style: function style(_ref) {
          var $theme = _ref.$theme;
          return {
            color: getIconColor($theme),
            ':hover': {
              color: getIconHoverColor($theme),
              cursor: props.disabled ? null : 'pointer'
            }
          };
        }
      }
    },
    size: 18
  })));
}