"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Timeline = void 0;
var _i18n = require("@kbn/i18n");
var _i18nReact = require("@kbn/i18n-react");
var _react = _interopRequireWildcard(require("react"));
var _eui = require("@elastic/eui");
var _shared_imports = require("../../../../../shared_imports");
var _lib = require("../../lib");
var _ = require("..");
var _components = require("./components");
require("./timeline.scss");
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 exists = v => v != null;
const toPercent = (n, total) => n / total * 100;
const msTimeToOverallPercent = (ms, totalMs) => {
  if (!isFinite(ms)) {
    return 100;
  }
  if (totalMs === 0) {
    return 100;
  }
  return toPercent(ms, totalMs);
};

/**
 * Each phase, if active, should have a minimum width it occupies. The higher this
 * base amount, the smaller the variance in phase size in the timeline. This functions
 * as a min-width constraint.
 */
const SCORE_BUFFER_AMOUNT = 50;
const i18nTexts = {
  title: _i18n.i18n.translate('xpack.indexLifecycleMgmt.timeline.title', {
    defaultMessage: 'Policy summary'
  }),
  description: _i18n.i18n.translate('xpack.indexLifecycleMgmt.timeline.description', {
    defaultMessage: 'This policy moves data through the following phases.'
  }),
  hotPhase: _i18n.i18n.translate('xpack.indexLifecycleMgmt.timeline.hotPhaseSectionTitle', {
    defaultMessage: 'Hot phase'
  }),
  warmPhase: _i18n.i18n.translate('xpack.indexLifecycleMgmt.timeline.warmPhaseSectionTitle', {
    defaultMessage: 'Warm phase'
  }),
  coldPhase: _i18n.i18n.translate('xpack.indexLifecycleMgmt.timeline.coldPhaseSectionTitle', {
    defaultMessage: 'Cold phase'
  }),
  frozenPhase: _i18n.i18n.translate('xpack.indexLifecycleMgmt.timeline.frozenPhaseSectionTitle', {
    defaultMessage: 'Frozen phase'
  }),
  deleteIcon: {
    toolTipContent: _i18n.i18n.translate('xpack.indexLifecycleMgmt.timeline.deleteIconToolTipContent', {
      defaultMessage: 'Policy deletes the index after lifecycle phases complete.'
    })
  },
  foreverIcon: {
    ariaLabel: _i18n.i18n.translate('xpack.indexLifecycleMgmt.timeline.foreverIconToolTipContent', {
      defaultMessage: 'Forever'
    })
  }
};
const calculateWidths = inputs => {
  const hotScore = msTimeToOverallPercent(inputs.phases.hot, inputs.total) + SCORE_BUFFER_AMOUNT;
  const warmScore = inputs.phases.warm != null ? msTimeToOverallPercent(inputs.phases.warm, inputs.total) + SCORE_BUFFER_AMOUNT : 0;
  const coldScore = inputs.phases.cold != null ? msTimeToOverallPercent(inputs.phases.cold, inputs.total) + SCORE_BUFFER_AMOUNT : 0;
  const frozenScore = inputs.phases.frozen != null ? msTimeToOverallPercent(inputs.phases.frozen, inputs.total) + SCORE_BUFFER_AMOUNT : 0;
  const totalScore = hotScore + warmScore + coldScore + frozenScore;
  return {
    hot: `${toPercent(hotScore, totalScore)}%`,
    warm: `${toPercent(warmScore, totalScore)}%`,
    cold: `${toPercent(coldScore, totalScore)}%`,
    frozen: `${toPercent(frozenScore, totalScore)}%`
  };
};
/**
 * Display a timeline given ILM policy phase information. This component is re-usable and memo-ized
 * and should not rely directly on any application-specific context.
 */
const Timeline = /*#__PURE__*/(0, _react.memo)(({
  hasDeletePhase,
  isUsingRollover,
  ...phasesMinAge
}) => {
  const absoluteTimings = {
    hot: {
      min_age: phasesMinAge.hotPhaseMinAge
    },
    warm: phasesMinAge.warmPhaseMinAge ? {
      min_age: phasesMinAge.warmPhaseMinAge
    } : undefined,
    cold: phasesMinAge.coldPhaseMinAge ? {
      min_age: phasesMinAge.coldPhaseMinAge
    } : undefined,
    frozen: phasesMinAge.frozenPhaseMinAge ? {
      min_age: phasesMinAge.frozenPhaseMinAge
    } : undefined,
    delete: phasesMinAge.deletePhaseMinAge ? {
      min_age: phasesMinAge.deletePhaseMinAge
    } : undefined
  };
  const phaseAgeInMilliseconds = (0, _lib.calculateRelativeFromAbsoluteMilliseconds)(absoluteTimings);
  const widths = calculateWidths(phaseAgeInMilliseconds);
  const getDurationInPhaseContent = phase => phaseAgeInMilliseconds.phases[phase] === Infinity ? /*#__PURE__*/_react.default.createElement(_.InfinityIcon, {
    color: "subdued",
    "aria-label": i18nTexts.foreverIcon.ariaLabel
  }) : null;
  const {
    docLinks
  } = (0, _shared_imports.useKibana)().services;
  return /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    gutterSize: "s",
    direction: "column",
    responsive: false
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_eui.EuiTitle, {
    size: "s"
  }, /*#__PURE__*/_react.default.createElement("h2", null, i18nTexts.title)), /*#__PURE__*/_react.default.createElement(_eui.EuiText, {
    size: "s",
    color: "subdued"
  }, i18nTexts.description, "\xA0", /*#__PURE__*/_react.default.createElement(_.LearnMoreLink, {
    docPath: docLinks.links.elasticsearch.ilmPhaseTransitions,
    text: /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, {
      id: "xpack.indexLifecycleMgmt.editPolicy.learnAboutTimingText",
      defaultMessage: "Learn about timing"
    })
  }))), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement("div", {
    className: "ilmTimeline",
    ref: el => {
      if (el) {
        var _widths$warm, _widths$cold, _widths$frozen;
        el.style.setProperty('--ilm-timeline-hot-phase-width', widths.hot);
        el.style.setProperty('--ilm-timeline-warm-phase-width', (_widths$warm = widths.warm) !== null && _widths$warm !== void 0 ? _widths$warm : null);
        el.style.setProperty('--ilm-timeline-cold-phase-width', (_widths$cold = widths.cold) !== null && _widths$cold !== void 0 ? _widths$cold : null);
        el.style.setProperty('--ilm-timeline-frozen-phase-width', (_widths$frozen = widths.frozen) !== null && _widths$frozen !== void 0 ? _widths$frozen : null);
      }
    }
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    gutterSize: "none",
    alignItems: "flexStart",
    responsive: false
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement("div", {
    className: "ilmTimeline__phasesContainer"
  }, /*#__PURE__*/_react.default.createElement("div", {
    "data-test-subj": "ilmTimelinePhase-hot",
    className: "ilmTimeline__phasesContainer__phase ilmTimeline__hotPhase"
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "ilmTimeline__colorBar ilmTimeline__hotPhase__colorBar"
  }), /*#__PURE__*/_react.default.createElement(_components.TimelinePhaseText, {
    phaseName: i18nTexts.hotPhase,
    durationInPhase: getDurationInPhaseContent('hot')
  })), exists(phaseAgeInMilliseconds.phases.warm) && /*#__PURE__*/_react.default.createElement("div", {
    "data-test-subj": "ilmTimelinePhase-warm",
    className: "ilmTimeline__phasesContainer__phase ilmTimeline__warmPhase"
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "ilmTimeline__colorBar ilmTimeline__warmPhase__colorBar"
  }), /*#__PURE__*/_react.default.createElement(_components.TimelinePhaseText, {
    phaseName: i18nTexts.warmPhase,
    durationInPhase: getDurationInPhaseContent('warm')
  })), exists(phaseAgeInMilliseconds.phases.cold) && /*#__PURE__*/_react.default.createElement("div", {
    "data-test-subj": "ilmTimelinePhase-cold",
    className: "ilmTimeline__phasesContainer__phase ilmTimeline__coldPhase"
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "ilmTimeline__colorBar ilmTimeline__coldPhase__colorBar"
  }), /*#__PURE__*/_react.default.createElement(_components.TimelinePhaseText, {
    phaseName: i18nTexts.coldPhase,
    durationInPhase: getDurationInPhaseContent('cold')
  })), exists(phaseAgeInMilliseconds.phases.frozen) && /*#__PURE__*/_react.default.createElement("div", {
    "data-test-subj": "ilmTimelinePhase-frozen",
    className: "ilmTimeline__phasesContainer__phase ilmTimeline__frozenPhase"
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "ilmTimeline__colorBar ilmTimeline__frozenPhase__colorBar"
  }), /*#__PURE__*/_react.default.createElement(_components.TimelinePhaseText, {
    phaseName: i18nTexts.frozenPhase,
    durationInPhase: getDurationInPhaseContent('frozen')
  })))), hasDeletePhase && /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    grow: false
  }, /*#__PURE__*/_react.default.createElement("div", {
    "data-test-subj": "ilmTimelinePhase-delete",
    className: "ilmTimeline__deleteIconContainer"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiIconTip, {
    type: "trash",
    content: i18nTexts.deleteIcon.toolTipContent
  })))))));
});
exports.Timeline = Timeline;