"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AutoFollowPatternActionMenu = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _eui = require("@elastic/eui");
var _i18n = require("@kbn/i18n");
var _auto_follow_pattern_delete_provider = require("../auto_follow_pattern_delete_provider");
var _routing = require("../../services/routing");
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.
 */

// @ts-ignore

const actionsAriaLabel = _i18n.i18n.translate('xpack.crossClusterReplication.autoFollowActionMenu.autoFollowPatternActionMenuButtonAriaLabel', {
  defaultMessage: 'Auto-follow pattern options'
});
const allValuesSame = values => {
  if (!values.length) {
    return false;
  }
  const [firstValue, ...restValues] = values;
  for (const value of restValues) {
    if (firstValue !== value) {
      return false;
    }
  }
  return true;
};
const AutoFollowPatternActionMenuUI = ({
  patterns,
  deleteAutoFollowPattern,
  pauseAutoFollowPattern,
  resumeAutoFollowPattern,
  arrowDirection,
  edit
}) => {
  const [showPopover, setShowPopover] = (0, _react.useState)(false);
  const allActiveValuesSame = allValuesSame(patterns.filter(Boolean).map(({
    active
  }) => active));
  const closePopoverViaAction = () => {
    setShowPopover(false);
  };
  const panelItems = [
  /**
   * Resume or pause pattern
   */
  allActiveValuesSame ? patterns[0].active ? {
    name: _i18n.i18n.translate('xpack.crossClusterReplication.pauseAutoFollowPatternsLabel', {
      defaultMessage: 'Pause {total, plural, one {replication} other {replications}}',
      values: {
        total: patterns.length
      }
    }),
    icon: /*#__PURE__*/_react.default.createElement(_eui.EuiIcon, {
      type: "pause"
    }),
    onClick: () => {
      pauseAutoFollowPattern(patterns.map(({
        name
      }) => name));
      closePopoverViaAction();
    }
  } : {
    name: _i18n.i18n.translate('xpack.crossClusterReplication.resumeAutoFollowPatternsLabel', {
      defaultMessage: 'Resume {total, plural, one {replication} other {replications}}',
      values: {
        total: patterns.length
      }
    }),
    icon: /*#__PURE__*/_react.default.createElement(_eui.EuiIcon, {
      type: "play"
    }),
    onClick: () => {
      resumeAutoFollowPattern(patterns.map(({
        name
      }) => name));
      closePopoverViaAction();
    }
  } : null,
  /**
   * Navigate to edit a pattern
   */
  edit && patterns.length === 1 ? {
    name: _i18n.i18n.translate('xpack.crossClusterReplication.editAutoFollowPatternButtonLabel', {
      defaultMessage: 'Edit pattern'
    }),
    icon: /*#__PURE__*/_react.default.createElement(_eui.EuiIcon, {
      type: "pencil"
    }),
    onClick: () => {
      _routing.routing.navigate(_routing.routing.getAutoFollowPatternPath(patterns[0].name));
    }
  } : null,
  /**
   * Delete a pattern
   */
  {
    name: _i18n.i18n.translate('xpack.crossClusterReplication.deleteAutoFollowPatternButtonLabel', {
      defaultMessage: 'Delete {total, plural, one {pattern} other {patterns}}',
      values: {
        total: patterns.length
      }
    }),
    icon: /*#__PURE__*/_react.default.createElement(_eui.EuiIcon, {
      type: "trash"
    }),
    onClick: () => {
      deleteAutoFollowPattern(patterns.map(({
        name
      }) => name));
      closePopoverViaAction();
    }
  }].filter(Boolean);
  const button = /*#__PURE__*/_react.default.createElement(_eui.EuiButton, {
    "data-test-subj": "autoFollowPatternActionMenuButton",
    "aria-label": actionsAriaLabel,
    onClick: () => setShowPopover(!showPopover),
    iconType: arrowDirection === 'up' ? 'arrowUp' : 'arrowDown',
    iconSide: "right",
    fill: true
  }, _i18n.i18n.translate('xpack.crossClusterReplication.autoFollowPatternActionMenu.buttonLabel', {
    defaultMessage: 'Manage {patterns, plural, one {pattern} other {patterns}}',
    values: {
      patterns: patterns.length
    }
  }));
  return /*#__PURE__*/_react.default.createElement(_eui.EuiPopover, {
    isOpen: showPopover,
    closePopover: () => setShowPopover(false),
    button: button,
    panelPaddingSize: "none",
    repositionOnScroll: true
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiContextMenu, {
    initialPanelId: 0,
    "data-test-subj": "autoFollowPatternActionContextMenu",
    panels: [{
      id: 0,
      title: _i18n.i18n.translate('xpack.crossClusterReplication.autoFollowPatternActionMenu.panelTitle', {
        defaultMessage: 'Pattern options'
      }),
      items: panelItems
    }]
  }));
};
const AutoFollowPatternActionMenu = props => /*#__PURE__*/_react.default.createElement(_auto_follow_pattern_delete_provider.AutoFollowPatternDeleteProvider, null, deleteAutoFollowPattern => /*#__PURE__*/_react.default.createElement(AutoFollowPatternActionMenuUI, (0, _extends2.default)({}, props, {
  deleteAutoFollowPattern: deleteAutoFollowPattern
})));
exports.AutoFollowPatternActionMenu = AutoFollowPatternActionMenu;