/*
 * 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 { Query } from '../query';
import { jsx as ___EmotionJSX } from "@emotion/react";
export var FieldValueToggleGroupFilter = function FieldValueToggleGroupFilter(props) {
  var resolveDisplay = function resolveDisplay(config, query, item) {
    var clause = query.getSimpleFieldClause(config.field, item.value);
    if (clause) {
      if (Query.isMust(clause)) {
        return {
          active: true,
          name: item.name
        };
      }
      return {
        active: true,
        name: item.negatedName ? item.negatedName : "Not ".concat(item.name)
      };
    }
    return {
      active: false,
      name: item.name
    };
  };
  var valueChanged = function valueChanged(item, active) {
    var field = props.config.field;
    var value = item.value,
      operator = item.operator;
    var query = active ? props.query.removeSimpleFieldClauses(field) : props.query.removeSimpleFieldClauses(field).addSimpleFieldValue(field, value, true, operator);
    props.onChange(query);
  };
  var config = props.config,
    query = props.query;
  return config.items.map(function (item, index) {
    var _resolveDisplay = resolveDisplay(config, query, item),
      active = _resolveDisplay.active,
      name = _resolveDisplay.name;
    var onClick = function onClick() {
      valueChanged(item, active);
    };
    var key = "field_value_toggle_filter_item_".concat(index);
    var isLastItem = index === config.items.length - 1;
    return ___EmotionJSX(EuiFilterButton, {
      key: key,
      onClick: onClick,
      isSelected: active,
      hasActiveFilters: active,
      "aria-pressed": !!active,
      withNext: !isLastItem,
      isToggle: true
    }, name);
  });
};