"use strict";

exports.__esModule = true;
exports.default = void 0;
var _get2 = _interopRequireDefault(require("lodash/fp/get"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _novaIcons = require("@coorpacademy/nova-icons");
var _select = _interopRequireDefault(require("../../atom/select"));
var _provider = _interopRequireDefault(require("../../atom/provider"));
var _style = _interopRequireDefault(require("./style.css"));
var _filtersWrapper = _interopRequireDefault(require("./filters-wrapper"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
class Filters extends _react.default.Component {
  static contextTypes = {
    skin: _provider.default.childContextTypes.skin
  };
  constructor(props) {
    super(props);
    this.state = {
      filter: !!props.openFilters,
      sorted: !!props.openSorts
    };
    this.handleOpenFilter = this.handleOpenFilter.bind(this);
    this.handleOpenSort = this.handleOpenSort.bind(this);
    this.handleSearch = this.handleSearch.bind(this);
  }
  handleOpenFilter() {
    const {
      filter
    } = this.state;
    const {
      onToggleFilters
    } = this.props;
    const newValue = !filter;
    this.setState({
      filter: newValue,
      sorted: false
    });
    if (onToggleFilters) {
      onToggleFilters(newValue);
    }
  }
  handleOpenSort() {
    const {
      sorted
    } = this.state;
    const {
      onToggleSorts
    } = this.props;
    const newValue = !sorted;
    this.setState({
      sorted: newValue,
      filter: false
    });
    if (onToggleSorts) onToggleSorts(newValue);
  }
  handleSearch() {
    const {
      onSearch
    } = this.props;
    this.setState({
      sorted: false,
      filter: false
    });
    if (onSearch) onSearch();
  }
  render() {
    const {
      sorting,
      filterCTALabel,
      filterTabLabel,
      sortCTALabel,
      sortTabLabel,
      filters,
      moreFilterAriaLabel,
      moreSortAriaLabel,
      filterGroupAriaLabel,
      sortAriaLabel
    } = this.props;
    const {
      filter,
      sorted
    } = this.state;
    const {
      skin
    } = this.context;
    const defaultColor = (0, _get2.default)('common.primary', skin);
    const darkColor = (0, _get2.default)('common.dark', skin);
    const filtersActive = filter === true;
    const sortingActive = sorted === true;
    const sortView = sorting !== undefined ? /*#__PURE__*/_react.default.createElement("div", {
      "data-name": "choice",
      className: _style.default.select
    }, /*#__PURE__*/_react.default.createElement(_select.default, _extends({}, sorting, {
      "aria-label": sortAriaLabel
    }))) : null;
    return /*#__PURE__*/_react.default.createElement("div", {
      "data-name": "search",
      className: _style.default.search
    }, /*#__PURE__*/_react.default.createElement("div", {
      "data-name": "filter",
      "data-open": filtersActive,
      className: filtersActive ? _style.default.activeDefault : _style.default.defaultStyle
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.title,
      "data-name": "filterButton",
      onClick: this.handleOpenFilter
    }, filterTabLabel, /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.arrow
    }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionNavigationArrowDown, {
      style: {
        color: darkColor
      },
      height: 14,
      "aria-label": moreFilterAriaLabel
    })))), /*#__PURE__*/_react.default.createElement("div", {
      "data-name": "sortBy",
      "data-open": sortingActive,
      className: sortingActive ? _style.default.activeWrapperSortBy : _style.default.wrapperSortBy
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.title,
      "data-name": "sortButton",
      onClick: this.handleOpenSort
    }, sortTabLabel, /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.arrow
    }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionNavigationArrowDown, {
      style: {
        color: darkColor
      },
      height: 14,
      "aria-label": moreSortAriaLabel
    })))), /*#__PURE__*/_react.default.createElement("div", {
      "data-name": "filterWrapper",
      className: filtersActive ? _style.default.activeWrapperFilters : _style.default.wrapperFilters
    }, /*#__PURE__*/_react.default.createElement(_filtersWrapper.default, {
      className: _style.default.wrapper,
      filters: filters,
      filterGroupAriaLabel: filterGroupAriaLabel
    }), /*#__PURE__*/_react.default.createElement("div", {
      "data-name": "cta",
      className: _style.default.ctaFilter,
      style: {
        backgroundColor: defaultColor
      },
      onClick: this.handleSearch
    }, filterCTALabel)), /*#__PURE__*/_react.default.createElement("div", {
      "data-name": "sortWrapper",
      className: sortingActive ? _style.default.activeSorting : _style.default.sorting
    }, sortView, /*#__PURE__*/_react.default.createElement("div", {
      "data-name": "cta",
      className: _style.default.ctaFilter,
      style: {
        backgroundColor: defaultColor
      },
      onClick: this.handleSearch
    }, sortCTALabel)));
  }
}
Filters.propTypes = process.env.NODE_ENV !== "production" ? {
  filterCTALabel: _propTypes.default.string,
  filterTabLabel: _propTypes.default.string,
  sortCTALabel: _propTypes.default.string,
  sortTabLabel: _propTypes.default.string,
  openFilters: _propTypes.default.bool,
  openSorts: _propTypes.default.bool,
  filters: _filtersWrapper.default.propTypes.filters,
  sorting: _propTypes.default.shape(_select.default.propTypes),
  onSearch: _propTypes.default.func,
  onToggleFilters: _propTypes.default.func,
  onToggleSorts: _propTypes.default.func,
  moreSortAriaLabel: _propTypes.default.string,
  moreFilterAriaLabel: _propTypes.default.string,
  filterGroupAriaLabel: _propTypes.default.string,
  sortAriaLabel: _propTypes.default.string
} : {};
var _default = exports.default = Filters;
//# sourceMappingURL=index.js.map