"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ContextAppContent = ContextAppContent;
exports.clamp = clamp;
var _react = _interopRequireWildcard(require("react"));
var _i18nReact = require("@kbn/i18n-react");
var _eui = require("@elastic/eui");
var _react2 = require("@emotion/react");
var _public = require("@kbn/data-plugin/public");
var _cellActions = require("@kbn/cell-actions");
var _searchResponseWarnings = require("@kbn/search-response-warnings");
var _discoverUtils = require("@kbn/discover-utils");
var _unifiedDataTable = require("@kbn/unified-data-table");
var _constants = require("../../../common/constants");
var _context_query_state = require("./services/context_query_state");
var _action_bar = require("./components/action_bar/action_bar");
var _context = require("./services/context");
var _constants2 = require("./services/constants");
var _doc_table_context = require("../../components/doc_table/doc_table_context");
var _use_discover_services = require("../../hooks/use_discover_services");
var _discover_grid_flyout = require("../../components/discover_grid_flyout");
var _discover_tour = require("../../components/discover_tour");
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 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.
 */

const controlColumnIds = ['openDetails'];
function clamp(value) {
  return Math.max(Math.min(_constants2.MAX_CONTEXT_SIZE, value), _constants2.MIN_CONTEXT_SIZE);
}
const DiscoverGridMemoized = /*#__PURE__*/_react.default.memo(_unifiedDataTable.UnifiedDataTable);
const DocTableContextMemoized = /*#__PURE__*/_react.default.memo(_doc_table_context.DocTableContext);
const ActionBarMemoized = /*#__PURE__*/_react.default.memo(_action_bar.ActionBar);
function ContextAppContent({
  columns,
  onAddColumn,
  onRemoveColumn,
  onSetColumns,
  dataView,
  predecessorCount,
  successorCount,
  rows,
  predecessors,
  successors,
  anchorStatus,
  predecessorsStatus,
  successorsStatus,
  interceptedWarnings,
  useNewFieldsApi,
  isLegacy,
  setAppState,
  addFilter
}) {
  const {
    uiSettings: config,
    uiActions
  } = (0, _use_discover_services.useDiscoverServices)();
  const services = (0, _use_discover_services.useDiscoverServices)();
  const [expandedDoc, setExpandedDoc] = (0, _react.useState)();
  const isAnchorLoading = anchorStatus === _context_query_state.LoadingStatus.LOADING || anchorStatus === _context_query_state.LoadingStatus.UNINITIALIZED;
  const arePredecessorsLoading = predecessorsStatus === _context_query_state.LoadingStatus.LOADING || predecessorsStatus === _context_query_state.LoadingStatus.UNINITIALIZED;
  const areSuccessorsLoading = successorsStatus === _context_query_state.LoadingStatus.LOADING || successorsStatus === _context_query_state.LoadingStatus.UNINITIALIZED;
  const showTimeCol = (0, _react.useMemo)(() => !config.get(_discoverUtils.DOC_HIDE_TIME_COLUMN_SETTING, false) && !!dataView.timeFieldName, [config, dataView]);
  const defaultStepSize = (0, _react.useMemo)(() => parseInt(config.get(_discoverUtils.CONTEXT_STEP_SETTING), 10), [config]);
  const loadingFeedback = () => {
    if (isLegacy && isAnchorLoading) {
      return /*#__PURE__*/_react.default.createElement(_eui.EuiText, {
        textAlign: "center",
        "data-test-subj": "contextApp_loadingIndicator"
      }, /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, {
        id: "discover.context.loadingDescription",
        defaultMessage: "Loading..."
      }));
    }
    return null;
  };
  const onChangeCount = (0, _react.useCallback)((type, count) => {
    const countKey = type === _context.SurrDocType.SUCCESSORS ? 'successorCount' : 'predecessorCount';
    setAppState({
      [countKey]: clamp(count)
    });
  }, [setAppState]);
  const sort = (0, _react.useMemo)(() => {
    return [[dataView.timeFieldName, _public.SortDirection.desc]];
  }, [dataView]);
  const renderDocumentView = (0, _react.useCallback)((hit, displayedRows, displayedColumns) => /*#__PURE__*/_react.default.createElement(_discover_grid_flyout.DiscoverGridFlyout, {
    dataView: dataView,
    hit: hit,
    hits: displayedRows
    // if default columns are used, dont make them part of the URL - the context state handling will take care to restore them
    ,
    columns: displayedColumns,
    onFilter: addFilter,
    onRemoveColumn: onRemoveColumn,
    onAddColumn: onAddColumn,
    onClose: () => setExpandedDoc(undefined),
    setExpandedDoc: setExpandedDoc
  }), [addFilter, dataView, onAddColumn, onRemoveColumn]);
  return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, /*#__PURE__*/_react.default.createElement(WrapperWithPadding, null, !!(interceptedWarnings !== null && interceptedWarnings !== void 0 && interceptedWarnings.length) && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_searchResponseWarnings.SearchResponseWarnings, {
    variant: "callout",
    interceptedWarnings: interceptedWarnings,
    "data-test-subj": "dscContextInterceptedWarnings"
  }), /*#__PURE__*/_react.default.createElement(_eui.EuiSpacer, {
    size: "s"
  })), /*#__PURE__*/_react.default.createElement(ActionBarMemoized, {
    type: _context.SurrDocType.PREDECESSORS,
    defaultStepSize: defaultStepSize,
    docCount: predecessorCount,
    docCountAvailable: predecessors.length,
    onChangeCount: onChangeCount,
    isLoading: arePredecessorsLoading,
    isDisabled: isAnchorLoading
  }), loadingFeedback()), isLegacy && rows && rows.length !== 0 && /*#__PURE__*/_react.default.createElement(DocTableContextMemoized, {
    columns: columns,
    dataView: dataView,
    rows: rows,
    isLoading: isAnchorLoading,
    onFilter: addFilter,
    onAddColumn: onAddColumn,
    onRemoveColumn: onRemoveColumn,
    sort: sort,
    useNewFieldsApi: useNewFieldsApi,
    dataTestSubj: "contextDocTable"
  }), !isLegacy && /*#__PURE__*/_react.default.createElement("div", {
    className: "dscDocsGrid"
  }, /*#__PURE__*/_react.default.createElement(_cellActions.CellActionsProvider, {
    getTriggerCompatibleActions: uiActions.getTriggerCompatibleActions
  }, /*#__PURE__*/_react.default.createElement(DiscoverGridMemoized, {
    ariaLabelledBy: "surDocumentsAriaLabel",
    columns: columns,
    rows: rows,
    dataView: dataView,
    expandedDoc: expandedDoc,
    loadingState: isAnchorLoading ? _unifiedDataTable.DataLoadingState.loading : _unifiedDataTable.DataLoadingState.loaded,
    sampleSize: 0,
    sort: sort,
    isSortEnabled: false,
    showTimeCol: showTimeCol,
    useNewFieldsApi: useNewFieldsApi,
    isPaginationEnabled: false,
    rowsPerPageState: (0, _constants.getDefaultRowsPerPage)(services.uiSettings),
    controlColumnIds: controlColumnIds,
    setExpandedDoc: setExpandedDoc,
    onFilter: addFilter,
    onSetColumns: onSetColumns,
    configRowHeight: services.uiSettings.get(_discoverUtils.ROW_HEIGHT_OPTION),
    showMultiFields: services.uiSettings.get(_discoverUtils.SHOW_MULTIFIELDS),
    maxDocFieldsDisplayed: services.uiSettings.get(_discoverUtils.MAX_DOC_FIELDS_DISPLAYED),
    renderDocumentView: renderDocumentView,
    services: services,
    componentsTourSteps: {
      expandButton: _discover_tour.DISCOVER_TOUR_STEP_ANCHOR_IDS.expandDocument
    }
  }))), /*#__PURE__*/_react.default.createElement(WrapperWithPadding, null, /*#__PURE__*/_react.default.createElement(ActionBarMemoized, {
    type: _context.SurrDocType.SUCCESSORS,
    defaultStepSize: defaultStepSize,
    docCount: successorCount,
    docCountAvailable: successors.length,
    onChangeCount: onChangeCount,
    isLoading: areSuccessorsLoading,
    isDisabled: isAnchorLoading
  })));
}
const WrapperWithPadding = ({
  children
}) => {
  const padding = (0, _eui.useEuiPaddingSize)('s');
  return /*#__PURE__*/_react.default.createElement("div", {
    css: (0, _react2.css)`
        padding: 0 ${padding};
      `
  }, children);
};