"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SearchPanel = void 0;
var _react = _interopRequireWildcard(require("react"));
var _eui = require("@elastic/eui");
var _i18n = require("@kbn/i18n");
var _use_aiops_app_context = require("../../hooks/use_aiops_app_context");
var _search_utils = require("../../application/utils/search_utils");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/*
 * 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; you may not use this file except in compliance with the Elastic License
 * 2.0.
 */

const SearchPanel = ({
  dataView,
  searchString,
  searchQueryLanguage,
  setSearchParams
}) => {
  const {
    uiSettings,
    unifiedSearch: {
      ui: {
        SearchBar
      }
    },
    notifications: {
      toasts
    },
    data: {
      query: queryManager
    }
  } = (0, _use_aiops_app_context.useAiopsAppContext)();

  // The internal state of the input query bar updated on every key stroke.
  const [searchInput, setSearchInput] = (0, _react.useState)({
    query: searchString || '',
    language: searchQueryLanguage
  });
  (0, _react.useEffect)(() => {
    setSearchInput({
      query: searchString || '',
      language: searchQueryLanguage
    });
  }, [searchQueryLanguage, searchString, queryManager.filterManager]);
  const searchHandler = ({
    query,
    filters
  }) => {
    const mergedQuery = query !== null && query !== void 0 ? query : searchInput;
    const mergedFilters = filters !== null && filters !== void 0 ? filters : queryManager.filterManager.getFilters();
    try {
      var _queryManager$filterM;
      if (mergedFilters) {
        queryManager.filterManager.setFilters(mergedFilters);
      }
      const combinedQuery = (0, _search_utils.createMergedEsQuery)(mergedQuery, (_queryManager$filterM = queryManager.filterManager.getFilters()) !== null && _queryManager$filterM !== void 0 ? _queryManager$filterM : [], dataView, uiSettings);
      setSearchParams({
        searchQuery: combinedQuery,
        searchString: mergedQuery.query,
        queryLanguage: mergedQuery.language,
        filters: mergedFilters
      });
    } catch (e) {
      console.log('Invalid syntax', JSON.stringify(e, null, 2)); // eslint-disable-line no-console
      toasts.addError(e, {
        title: _i18n.i18n.translate('xpack.aiops.searchPanel.invalidSyntax', {
          defaultMessage: 'Invalid syntax'
        })
      });
    }
  };
  return /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    gutterSize: "s",
    "data-test-subj": "aiopsSearchPanel",
    responsive: false
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    grow: 9
  }, /*#__PURE__*/_react.default.createElement(SearchBar, {
    dataTestSubj: "aiopsQueryInput",
    appName: 'aiops',
    showFilterBar: true,
    showDatePicker: false,
    showQueryInput: true,
    query: searchInput,
    onQuerySubmit: params => searchHandler({
      query: params.query
    }),
    indexPatterns: [dataView],
    placeholder: _i18n.i18n.translate('xpack.aiops.searchPanel.queryBarPlaceholderText', {
      defaultMessage: 'Search… (e.g. status:200 AND extension:"PHP")'
    }),
    displayStyle: 'inPage',
    isClearable: true,
    showSubmitButton: false,
    onFiltersUpdated: filters => searchHandler({
      filters
    })
  })));
};
exports.SearchPanel = SearchPanel;