"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FieldValueToggleGroupFilter = void 0;
var _react = _interopRequireDefault(require("react"));
var _filter_group = require("../../filter_group");
var _query = require("../query");
var _react2 = require("@emotion/react");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*
 * 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 FieldValueToggleGroupFilter = exports.FieldValueToggleGroupFilter = function FieldValueToggleGroupFilter(props) {
  var resolveDisplay = function resolveDisplay(config, query, item) {
    var clause = query.getSimpleFieldClause(config.field, item.value);
    if (clause) {
      if (_query.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 (0, _react2.jsx)(_filter_group.EuiFilterButton, {
      key: key,
      onClick: onClick,
      isSelected: active,
      hasActiveFilters: active,
      "aria-pressed": !!active,
      withNext: !isLastItem,
      isToggle: true
    }, name);
  });
};