"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SearchApplicationIndices = void 0;
var _react = _interopRequireWildcard(require("react"));
var _kea = require("kea");
var _eui = require("@elastic/eui");
var _i18n = require("@kbn/i18n");
var _constants = require("../../../../../common/constants");
var _routes = require("../../../enterprise_search_content/routes");
var _constants2 = require("../../../shared/constants");
var _health_colors = require("../../../shared/constants/health_colors");
var _encode_path_params = require("../../../shared/encode_path_params");
var _kibana = require("../../../shared/kibana");
var _react_router_helpers = require("../../../shared/react_router_helpers");
var _telemetry_logic = require("../../../shared/telemetry/telemetry_logic");
var _search_application_indices_logic = require("./search_application_indices_logic");
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 SearchApplicationIndices = () => {
  const subduedBackground = (0, _eui.useEuiBackgroundColor)('subdued');
  const {
    sendEnterpriseSearchTelemetry
  } = (0, _kea.useActions)(_telemetry_logic.TelemetryLogic);
  const {
    searchApplicationData
  } = (0, _kea.useValues)(_search_application_indices_logic.SearchApplicationIndicesLogic);
  const {
    removeIndexFromSearchApplication
  } = (0, _kea.useActions)(_search_application_indices_logic.SearchApplicationIndicesLogic);
  const {
    navigateToUrl
  } = (0, _kea.useValues)(_kibana.KibanaLogic);
  const [removeIndexConfirm, setConfirmRemoveIndex] = (0, _react.useState)(null);
  if (!searchApplicationData) return null;
  const {
    indices
  } = searchApplicationData;
  const hasAllUnreachableIndices = indices.every(({
    health
  }) => health === 'unknown');
  const hasUnknownIndices = indices.some(({
    health
  }) => health === 'unknown');
  const removeIndexAction = {
    color: 'danger',
    'data-test-subj': 'search-application-remove-index-btn',
    description: _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.actions.removeIndex.title', {
      defaultMessage: 'Remove this index from search application'
    }),
    icon: 'minusInCircle',
    isPrimary: false,
    name: index => _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.actions.removeIndex.caption', {
      defaultMessage: 'Remove index {indexName}',
      values: {
        indexName: index.name
      }
    }),
    onClick: index => {
      setConfirmRemoveIndex(index.name);
      sendEnterpriseSearchTelemetry({
        action: 'clicked',
        metric: 'entSearchApplications-indices-removeIndex'
      });
    },
    type: 'icon'
  };
  const columns = [{
    name: _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.name.columnTitle', {
      defaultMessage: 'Index name'
    }),
    render: ({
      health,
      name
    }) => health === 'unknown' ? name : /*#__PURE__*/_react.default.createElement(_react_router_helpers.EuiLinkTo, {
      "data-test-subj": "search-application-index-link",
      to: `${_constants.ENTERPRISE_SEARCH_CONTENT_PLUGIN.URL}${(0, _encode_path_params.generateEncodedPath)(_routes.SEARCH_INDEX_PATH, {
        indexName: name
      })}`,
      shouldNotCreateHref: true
    }, name),
    sortable: ({
      name
    }) => name,
    truncateText: true,
    width: '40%'
  }, {
    field: 'health',
    name: _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.health.columnTitle', {
      defaultMessage: 'Index health'
    }),
    render: health => /*#__PURE__*/_react.default.createElement("span", null, /*#__PURE__*/_react.default.createElement(_eui.EuiIcon, {
      type: "dot",
      color: (0, _health_colors.indexHealthToHealthColor)(health)
    }), "\xA0", health !== null && health !== void 0 ? health : '-'),
    sortable: true,
    truncateText: true,
    width: '15%'
  }, {
    field: 'count',
    name: _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.docsCount.columnTitle', {
      defaultMessage: 'Docs count'
    }),
    render: count => count === null ? _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.docsCount.notAvailableLabel', {
      defaultMessage: 'N/A'
    }) : count,
    sortable: true,
    truncateText: true,
    width: '15%'
  }, {
    actions: [{
      available: index => index.health !== 'unknown',
      'data-test-subj': 'search-application-view-index-btn',
      description: _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.actions.viewIndex.title', {
        defaultMessage: 'View this index'
      }),
      icon: 'eye',
      isPrimary: false,
      name: index => _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.actions.viewIndex.caption', {
        defaultMessage: 'View index {indexName}',
        values: {
          indexName: index.name
        }
      }),
      onClick: index => navigateToUrl(`${_constants.ENTERPRISE_SEARCH_CONTENT_PLUGIN.URL}/${(0, _encode_path_params.generateEncodedPath)(_routes.SEARCH_INDEX_PATH, {
        indexName: index.name
      })}`, {
        shouldNotCreateHref: true
      }),
      type: 'icon'
    }, ...(indices.length > 1 ? [removeIndexAction] : [])],
    name: _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.actions.columnTitle', {
      defaultMessage: 'Actions'
    }),
    width: '10%'
  }];
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, (hasAllUnreachableIndices || hasUnknownIndices) && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiCallOut, {
    color: "warning",
    iconType: "warning",
    title: hasAllUnreachableIndices ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.allUnknownIndicesCallout.title', {
      defaultMessage: 'All of your indices are unavailable.'
    })) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.someUnknownIndicesCallout.title', {
      defaultMessage: 'Some of your indices are unavailable.'
    }))
  }, /*#__PURE__*/_react.default.createElement("p", null, hasAllUnreachableIndices ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.allUnknownIndicesCallout.description', {
    defaultMessage: 'Your search application has no reachable indices. Add some indices and check for any pending operations or errors on affected indices, or remove indices that should no longer be used by this search application.'
  })) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.someUnknownIndicesCallout.description', {
    defaultMessage: 'Some data might be unreachable from this search application. Check for any pending operations or errors on affected indices, or remove indices that should no longer be used by this search application.'
  })))), /*#__PURE__*/_react.default.createElement(_eui.EuiSpacer, null)), /*#__PURE__*/_react.default.createElement(_eui.EuiInMemoryTable, {
    items: indices,
    columns: columns,
    rowProps: index => {
      if (index.health === 'unknown') {
        return {
          style: {
            backgroundColor: subduedBackground
          }
        };
      }
      return {};
    },
    search: {
      box: {
        incremental: true,
        placeholder: _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.searchPlaceholder', {
          defaultMessage: 'Filter indices'
        }),
        schema: true
      }
    },
    pagination: true,
    sorting: true
  }), removeIndexConfirm !== null && /*#__PURE__*/_react.default.createElement(_eui.EuiConfirmModal, {
    onCancel: () => setConfirmRemoveIndex(null),
    onConfirm: () => {
      removeIndexFromSearchApplication(removeIndexConfirm);
      setConfirmRemoveIndex(null);
      sendEnterpriseSearchTelemetry({
        action: 'clicked',
        metric: 'entSearchApplications-indices-removeIndexConfirm'
      });
    },
    title: _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.removeIndexConfirm.title', {
      defaultMessage: 'Remove this index from the search application'
    }),
    buttonColor: "danger",
    cancelButtonText: _constants2.CANCEL_BUTTON_LABEL,
    confirmButtonText: _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.removeIndexConfirm.text', {
      defaultMessage: 'Yes, Remove This Index'
    }),
    defaultFocusedButton: "confirm",
    maxWidth: true
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiText, null, /*#__PURE__*/_react.default.createElement("p", null, _i18n.i18n.translate('xpack.enterpriseSearch.searchApplications.searchApplication.indices.removeIndexConfirm.description', {
    defaultMessage: "This won't delete the index. You may add it back to this search application at a later time."
  })))));
};
exports.SearchApplicationIndices = SearchApplicationIndices;