"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SearchResult = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactWindow = require("react-window");
var _eui = require("@elastic/eui");
var _i18nReact = require("@kbn/i18n-react");
var _mappings_state_context = require("../../../mappings_state_context");
var _search_result_item = require("./search_result_item");
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 ITEM_HEIGHT = 64;
const Row = /*#__PURE__*/_react.default.memo(({
  data,
  index,
  style
}) => {
  // Data passed to List as "itemData" is available as props.data
  const {
    fieldToEdit,
    result,
    status
  } = data;
  const item = result[index];
  return /*#__PURE__*/_react.default.createElement("div", {
    key: item.field.id,
    style: style
  }, /*#__PURE__*/_react.default.createElement(_search_result_item.SearchResultItem, {
    item: item,
    areActionButtonsVisible: status === 'idle',
    isDimmed: status === 'editingField' && fieldToEdit !== item.field.id,
    isHighlighted: status === 'editingField' && fieldToEdit === item.field.id
  }));
}, _reactWindow.areEqual);
const SearchResult = /*#__PURE__*/_react.default.memo(({
  result,
  documentFieldsState: {
    status,
    fieldToEdit
  },
  style: virtualListStyle
}) => {
  const dispatch = (0, _mappings_state_context.useDispatch)();
  const listHeight = Math.min(result.length * ITEM_HEIGHT, 600);
  const clearSearch = () => {
    dispatch({
      type: 'search:update',
      value: ''
    });
  };
  const itemData = (0, _react.useMemo)(() => ({
    result,
    status,
    fieldToEdit
  }), [fieldToEdit, result, status]);
  return result.length === 0 ? /*#__PURE__*/_react.default.createElement(_eui.EuiEmptyPrompt, {
    "data-test-subj": "mappingsEditorSearchResultEmptyPrompt",
    iconType: "search",
    title: /*#__PURE__*/_react.default.createElement("h3", null, /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, {
      id: "xpack.idxMgmt.mappingsEditor.searchResult.emptyPromptTitle",
      defaultMessage: "No fields match your search"
    })),
    actions: /*#__PURE__*/_react.default.createElement(_eui.EuiButton, {
      onClick: clearSearch
    }, /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, {
      id: "xpack.idxMgmt.mappingsEditor.searchResult.emptyPrompt.clearSearchButtonLabel",
      defaultMessage: "Clear search"
    }))
  }) : /*#__PURE__*/_react.default.createElement(_reactWindow.FixedSizeList, {
    "data-test-subj": "mappingsEditorSearchResult",
    style: {
      overflowX: 'hidden',
      ...virtualListStyle
    },
    width: "100%",
    height: listHeight,
    itemData: itemData,
    itemCount: result.length,
    itemSize: ITEM_HEIGHT
  }, Row);
});
exports.SearchResult = SearchResult;
SearchResult.displayName = 'SearchResult'; // display name required for tests to work with React.memo