"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DataStreamDetailPanel = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _i18n = require("@kbn/i18n");
var _i18nReact = require("@kbn/i18n-react");
var _eui = require("@elastic/eui");
var _discover_link = require("../../../../lib/discover_link");
var _data_streams = require("../../../../lib/data_streams");
var _shared_imports = require("../../../../../shared_imports");
var _components = require("../../../../components");
var _api = require("../../../../services/api");
var _delete_data_stream_confirmation_modal = require("../delete_data_stream_confirmation_modal");
var _edit_data_retention_modal = require("../edit_data_retention_modal");
var _humanize_time_stamp = require("../humanize_time_stamp");
var _routing = require("../../../../services/routing");
var _constants = require("../../../../constants");
var _app_context = require("../../../../app_context");
var _data_stream_badges = require("../data_stream_badges");
var _use_ilm_locator = require("../../../../services/use_ilm_locator");
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 DetailsList = ({
  details
}) => {
  const descriptionListItems = details.map((detail, index) => {
    const {
      name,
      toolTip,
      content,
      dataTestSubj
    } = detail;
    return /*#__PURE__*/_react.default.createElement(_react.Fragment, {
      key: `${name}-${index}`
    }, /*#__PURE__*/_react.default.createElement(_eui.EuiDescriptionListTitle, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
      alignItems: "center",
      gutterSize: "s"
    }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
      grow: false
    }, name), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
      grow: false
    }, toolTip && /*#__PURE__*/_react.default.createElement(_eui.EuiIconTip, {
      content: toolTip,
      position: "top"
    })))), /*#__PURE__*/_react.default.createElement(_eui.EuiDescriptionListDescription, {
      "data-test-subj": dataTestSubj
    }, content));
  });
  const midpoint = Math.ceil(descriptionListItems.length / 2);
  const descriptionListColumnOne = descriptionListItems.slice(0, midpoint);
  const descriptionListColumnTwo = descriptionListItems.slice(midpoint, descriptionListItems.length);
  return /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_eui.EuiDescriptionList, {
    textStyle: "reverse"
  }, descriptionListColumnOne)), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_eui.EuiDescriptionList, {
    textStyle: "reverse"
  }, descriptionListColumnTwo)));
};
const DataStreamDetailPanel = ({
  dataStreamName,
  onClose
}) => {
  var _dataStream$privilege, _dataStream$privilege2, _panels$0$items;
  const [isManagePopOverOpen, setManagePopOver] = (0, _react.useState)(false);
  const [isDeleting, setIsDeleting] = (0, _react.useState)(false);
  const [isEditingDataRetention, setIsEditingDataRetention] = (0, _react.useState)(false);
  const {
    error,
    data: dataStream,
    isLoading
  } = (0, _api.useLoadDataStream)(dataStreamName);
  const ilmPolicyLink = (0, _use_ilm_locator.useIlmLocator)(_constants.ILM_PAGES_POLICY_EDIT, dataStream === null || dataStream === void 0 ? void 0 : dataStream.ilmPolicyName);
  const {
    history
  } = (0, _app_context.useAppContext)();
  let content;
  if (isLoading) {
    content = /*#__PURE__*/_react.default.createElement(_shared_imports.SectionLoading, null, _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.loadingDataStreamDescription', {
      defaultMessage: 'Loading data stream'
    }));
  } else if (error) {
    content = /*#__PURE__*/_react.default.createElement(_components.SectionError, {
      title: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.loadingDataStreamErrorMessage', {
        defaultMessage: 'Error loading data stream'
      }),
      error: error,
      "data-test-subj": "sectionError"
    });
  } else if (dataStream) {
    const {
      health,
      indices,
      timeStampField,
      generation,
      indexTemplateName,
      ilmPolicyName,
      storageSize,
      maxTimeStamp,
      lifecycle
    } = dataStream;
    const getManagementDetails = () => {
      const managementDetails = [];
      if (ilmPolicyName) {
        managementDetails.push({
          name: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.ilmPolicyTitle', {
            defaultMessage: 'Index lifecycle policy'
          }),
          toolTip: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.ilmPolicyToolTip', {
            defaultMessage: `The index lifecycle policy that manages the data in the data stream.`
          }),
          content: ilmPolicyLink ? /*#__PURE__*/_react.default.createElement(_eui.EuiLink, {
            "data-test-subj": 'ilmPolicyLink',
            href: ilmPolicyLink
          }, ilmPolicyName) : ilmPolicyName,
          dataTestSubj: 'ilmPolicyDetail'
        });
      }
      return managementDetails;
    };
    const defaultDetails = [{
      name: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.healthTitle', {
        defaultMessage: 'Health'
      }),
      toolTip: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.healthToolTip', {
        defaultMessage: `The health of the data stream's current backing indices.`
      }),
      content: /*#__PURE__*/_react.default.createElement(_components.DataHealth, {
        health: health
      }),
      dataTestSubj: 'healthDetail'
    }, {
      name: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.maxTimeStampTitle', {
        defaultMessage: 'Last updated'
      }),
      toolTip: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.maxTimeStampToolTip', {
        defaultMessage: 'The most recent document to be added to the data stream.'
      }),
      content: maxTimeStamp ? (0, _humanize_time_stamp.humanizeTimeStamp)(maxTimeStamp) : /*#__PURE__*/_react.default.createElement("em", null, _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.maxTimeStampNoneMessage', {
        defaultMessage: `Never`
      })),
      dataTestSubj: 'lastUpdatedDetail'
    }, {
      name: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.storageSizeTitle', {
        defaultMessage: 'Storage size'
      }),
      toolTip: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.storageSizeToolTip', {
        defaultMessage: `The total size of all shards in the data stream’s backing indices.`
      }),
      content: storageSize,
      dataTestSubj: 'storageSizeDetail'
    }, {
      name: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.indicesTitle', {
        defaultMessage: 'Indices'
      }),
      toolTip: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.indicesToolTip', {
        defaultMessage: `The data stream's current backing indices.`
      }),
      content: /*#__PURE__*/_react.default.createElement(_eui.EuiLink, (0, _shared_imports.reactRouterNavigate)(history, (0, _routing.getIndexListUri)(`data_stream="${dataStreamName}"`, true)), indices.length),
      dataTestSubj: 'indicesDetail'
    }, {
      name: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.timestampFieldTitle', {
        defaultMessage: 'Timestamp field'
      }),
      toolTip: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.timestampFieldToolTip', {
        defaultMessage: 'The timestamp field shared by all documents in the data stream.'
      }),
      content: timeStampField.name,
      dataTestSubj: 'timestampDetail'
    }, {
      name: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.generationTitle', {
        defaultMessage: 'Generation'
      }),
      toolTip: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.generationToolTip', {
        defaultMessage: 'The number of backing indices generated for the data stream.'
      }),
      content: generation,
      dataTestSubj: 'generationDetail'
    }, {
      name: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.indexTemplateTitle', {
        defaultMessage: 'Index template'
      }),
      toolTip: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.indexTemplateToolTip', {
        defaultMessage: 'The index template that configured the data stream and configures its backing indices.'
      }),
      content: /*#__PURE__*/_react.default.createElement(_eui.EuiLink, (0, _extends2.default)({
        "data-test-subj": 'indexTemplateLink'
      }, (0, _shared_imports.reactRouterNavigate)(history, (0, _routing.getTemplateDetailsLink)(indexTemplateName))), indexTemplateName),
      dataTestSubj: 'indexTemplateDetail'
    }, {
      name: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.dataRetentionTitle', {
        defaultMessage: 'Data retention'
      }),
      toolTip: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.dataRetentionToolTip', {
        defaultMessage: 'The amount of time to retain the data in the data stream.'
      }),
      content: (0, _data_streams.getLifecycleValue)(lifecycle),
      dataTestSubj: 'dataRetentionDetail'
    }];
    const managementDetails = getManagementDetails();
    const details = [...defaultDetails, ...managementDetails];
    content = /*#__PURE__*/_react.default.createElement(DetailsList, {
      details: details
    });
  }
  const closePopover = () => {
    setManagePopOver(false);
  };
  const button = /*#__PURE__*/_react.default.createElement(_eui.EuiButton, {
    fill: true,
    iconType: "arrowDown",
    iconSide: "right",
    "data-test-subj": "manageDataStreamButton",
    onClick: () => setManagePopOver(!isManagePopOverOpen)
  }, /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, {
    id: "xpack.idxMgmt.dataStreamsDetailsPanel.manageButtonLabel",
    defaultMessage: "Manage"
  }));
  const panels = [{
    id: 0,
    title: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.managePanelTitle', {
      defaultMessage: 'Data stream options'
    }),
    items: [...(!(dataStream !== null && dataStream !== void 0 && dataStream.ilmPolicyName) && dataStream !== null && dataStream !== void 0 && (_dataStream$privilege = dataStream.privileges) !== null && _dataStream$privilege !== void 0 && _dataStream$privilege.manage_data_stream_lifecycle ? [{
      key: 'editDataRetention',
      name: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.managePanelEditDataRetention', {
        defaultMessage: 'Edit data retention'
      }),
      'data-test-subj': 'editDataRetentionButton',
      icon: /*#__PURE__*/_react.default.createElement(_eui.EuiIcon, {
        type: "pencil",
        size: "m"
      }),
      onClick: () => {
        closePopover();
        setIsEditingDataRetention(true);
      }
    }] : []), ...(dataStream !== null && dataStream !== void 0 && (_dataStream$privilege2 = dataStream.privileges) !== null && _dataStream$privilege2 !== void 0 && _dataStream$privilege2.delete_index ? [{
      key: 'deleteDataStream',
      name: _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.managePanelDelete', {
        defaultMessage: 'Delete'
      }),
      'data-test-subj': 'deleteDataStreamButton',
      icon: /*#__PURE__*/_react.default.createElement(_eui.EuiIcon, {
        type: "trash",
        size: "m",
        color: "danger"
      }),
      onClick: () => {
        closePopover();
        setIsDeleting(true);
      }
    }] : [])]
  }];
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, isDeleting && /*#__PURE__*/_react.default.createElement(_delete_data_stream_confirmation_modal.DeleteDataStreamConfirmationModal, {
    onClose: data => {
      if (data && data.hasDeletedDataStreams) {
        onClose(true);
      } else {
        setIsDeleting(false);
      }
    },
    dataStreams: [dataStreamName]
  }), isEditingDataRetention && /*#__PURE__*/_react.default.createElement(_edit_data_retention_modal.EditDataRetentionModal, {
    onClose: data => {
      if (data && data !== null && data !== void 0 && data.hasUpdatedDataRetention) {
        onClose(true);
      } else {
        setIsEditingDataRetention(false);
      }
    },
    dataStreamName: dataStreamName,
    lifecycle: dataStream === null || dataStream === void 0 ? void 0 : dataStream.lifecycle
  }), /*#__PURE__*/_react.default.createElement(_eui.EuiFlyout, {
    onClose: () => onClose(),
    "data-test-subj": "dataStreamDetailPanel",
    "aria-labelledby": "dataStreamDetailPanelTitle",
    size: "m",
    maxWidth: 500
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlyoutHeader, null, /*#__PURE__*/_react.default.createElement(_eui.EuiTitle, {
    size: "m"
  }, /*#__PURE__*/_react.default.createElement("h2", {
    id: "dataStreamDetailPanelTitle",
    "data-test-subj": "dataStreamDetailPanelTitle"
  }, dataStreamName, /*#__PURE__*/_react.default.createElement(_discover_link.DiscoverLink, {
    indexName: dataStreamName
  }), dataStream && /*#__PURE__*/_react.default.createElement(_data_stream_badges.DataStreamsBadges, {
    dataStream: dataStream
  })))), /*#__PURE__*/_react.default.createElement(_eui.EuiFlyoutBody, {
    "data-test-subj": "content"
  }, content), /*#__PURE__*/_react.default.createElement(_eui.EuiFlyoutFooter, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    justifyContent: "spaceBetween",
    alignItems: "center"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    grow: false
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiButtonEmpty, {
    iconType: "cross",
    flush: "left",
    onClick: () => onClose(),
    "data-test-subj": "closeDetailsButton"
  }, _i18n.i18n.translate('xpack.idxMgmt.dataStreamDetailPanel.closeButtonLabel', {
    defaultMessage: 'Close'
  }))), !isLoading && !error && ((_panels$0$items = panels[0].items) === null || _panels$0$items === void 0 ? void 0 : _panels$0$items.length) && /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    grow: false
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiPopover, {
    button: button,
    isOpen: isManagePopOverOpen,
    closePopover: closePopover,
    panelPaddingSize: "none",
    anchorPosition: "downLeft"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiContextMenu, {
    initialPanelId: 0,
    panels: panels
  })))))));
};
exports.DataStreamDetailPanel = DataStreamDetailPanel;