/*
 * 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.
 */

import React from 'react';
import { EuiFilterButton } from '../../filter_group';
import { isNil } from '../../../services/predicate';
import { Query } from '../query';
import { jsx as ___EmotionJSX } from "@emotion/react";
export var IsFilter = function IsFilter(props) {
  var resolveDisplay = function resolveDisplay(clause) {
    var _props$config = props.config,
      name = _props$config.name,
      negatedName = _props$config.negatedName;
    if (isNil(clause)) {
      return {
        hasActiveFilters: false,
        name: name
      };
    }
    return 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 = !isNil(clause);
  var _resolveDisplay = resolveDisplay(clause),
    hasActiveFilters = _resolveDisplay.hasActiveFilters,
    name = _resolveDisplay.name;
  var onClick = function onClick() {
    valueChanged(config.field, checked);
  };
  return ___EmotionJSX(EuiFilterButton, {
    onClick: onClick,
    isSelected: hasActiveFilters,
    hasActiveFilters: hasActiveFilters,
    "aria-pressed": !!hasActiveFilters,
    isToggle: true
  }, name);
};