"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ShareMenu = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _eui = require("@elastic/eui");
var _i18n = require("@kbn/i18n");
var _constants = require("../../../../i18n/constants");
var _flatten_panel_tree = require("../../../lib/flatten_panel_tree");
var _popover = require("../../popover");
var _flyout = require("./flyout");
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 strings = {
  getShareDownloadJSONTitle: () => _i18n.i18n.translate('xpack.canvas.workpadHeaderShareMenu.shareDownloadJSONTitle', {
    defaultMessage: 'Download as {JSON}',
    values: {
      JSON: _constants.JSON
    }
  }),
  getShareDownloadPDFTitle: () => _i18n.i18n.translate('xpack.canvas.workpadHeaderShareMenu.shareDownloadPDFTitle', {
    defaultMessage: '{PDF} reports',
    values: {
      PDF: _constants.PDF
    }
  }),
  getShareMenuButtonLabel: () => _i18n.i18n.translate('xpack.canvas.workpadHeaderShareMenu.shareMenuButtonLabel', {
    defaultMessage: 'Share'
  }),
  getShareWebsiteTitle: () => _i18n.i18n.translate('xpack.canvas.workpadHeaderShareMenu.shareWebsiteTitle', {
    defaultMessage: 'Share on a website'
  }),
  getShareWorkpadMessage: () => _i18n.i18n.translate('xpack.canvas.workpadHeaderShareMenu.shareWorkpadMessage', {
    defaultMessage: 'Share this workpad'
  })
};
/**
 * The Menu for Exporting a Workpad from Canvas.
 */
const ShareMenu = ({
  ReportingComponent,
  onExport
}) => {
  const [showFlyout, setShowFlyout] = (0, _react.useState)(false);
  const onFlyoutClose = () => {
    setShowFlyout(false);
  };
  const getPanelTree = closePopover => ({
    id: 0,
    items: [{
      name: strings.getShareDownloadJSONTitle(),
      icon: /*#__PURE__*/_react.default.createElement(_eui.EuiIcon, {
        type: "exportAction",
        size: "m"
      }),
      onClick: () => {
        onExport('json');
        closePopover();
      }
    }, ReportingComponent !== null ? {
      name: strings.getShareDownloadPDFTitle(),
      icon: 'document',
      panel: {
        id: 1,
        title: strings.getShareDownloadPDFTitle(),
        content: /*#__PURE__*/_react.default.createElement(ReportingComponent, {
          onClose: closePopover
        })
      },
      'data-test-subj': 'sharePanel-PDFReports'
    } : false, {
      name: strings.getShareWebsiteTitle(),
      icon: /*#__PURE__*/_react.default.createElement(_eui.EuiIcon, {
        type: "globe",
        size: "m"
      }),
      onClick: () => {
        setShowFlyout(true);
        closePopover();
      }
    }].filter(Boolean)
  });
  const shareControl = togglePopover => /*#__PURE__*/_react.default.createElement(_eui.EuiButtonEmpty, {
    size: "s",
    "aria-label": strings.getShareWorkpadMessage(),
    onClick: togglePopover,
    "data-test-subj": "shareTopNavButton"
  }, strings.getShareMenuButtonLabel());
  const flyout = showFlyout ? /*#__PURE__*/_react.default.createElement(_flyout.ShareWebsiteFlyout, {
    onClose: onFlyoutClose
  }) : null;
  return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_popover.Popover, {
    button: shareControl,
    panelPaddingSize: "none",
    anchorPosition: "downLeft"
  }, ({
    closePopover
  }) => /*#__PURE__*/_react.default.createElement(_eui.EuiContextMenu, {
    initialPanelId: 0,
    panels: (0, _flatten_panel_tree.flattenPanelTree)(getPanelTree(closePopover))
  })), flyout);
};
exports.ShareMenu = ShareMenu;
ShareMenu.propTypes = {
  onExport: _propTypes.default.func.isRequired
};