"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.IsFilter = void 0;
var _react = _interopRequireDefault(require("react"));
var _filter_group = require("../../filter_group");
var _predicate = require("../../../services/predicate");
var _query = require("../query");
var _react2 = require("@emotion/react");
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

var IsFilter = exports.IsFilter = function IsFilter(props) {
  var resolveDisplay = function resolveDisplay(clause) {
    var _props$config = props.config,
      name = _props$config.name,
      negatedName = _props$config.negatedName;
    if ((0, _predicate.isNil)(clause)) {
      return {
        hasActiveFilters: false,
        name: name
      };
    }
    return _query.Query.isMust(clause) ? {
      hasActiveFilters: true,
      name: name
    } : {
      hasActiveFilters: true,
      name: negatedName ? negatedName : "Not ".concat(name)
    };
  };
  var valueChanged = function valueChanged(field, checked) {
    var query = checked ? props.query.removeIsClause(field) : props.query.addMustIsClause(field);
    props.onChange(query);
  };
  var query = props.query,
    config = props.config;
  var clause = query.getIsClause(config.field);
  var checked = !(0, _predicate.isNil)(clause);
  var _resolveDisplay = resolveDisplay(clause),
    hasActiveFilters = _resolveDisplay.hasActiveFilters,
    name = _resolveDisplay.name;
  var onClick = function onClick() {
    valueChanged(config.field, checked);
  };
  return (0, _react2.jsx)(_filter_group.EuiFilterButton, {
    onClick: onClick,
    isSelected: hasActiveFilters,
    hasActiveFilters: hasActiveFilters,
    "aria-pressed": !!hasActiveFilters,
    isToggle: true
  }, name);
};