import _extends from "@babel/runtime/helpers/extends";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["url", "onClick", "testId", "content"];
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React, { useCallback, useState } from 'react';
import { FormattedMessage } from 'react-intl-next';
import CopyIcon from '@atlaskit/icon/core/copy';
import { useAnalyticsEvents } from '../../../../../common/analytics/generated/use-analytics-events';
import { messages } from '../../../../../messages';
import Action from '../action';
export function CopySummaryAction(_ref) {
  var url = _ref.url,
    onClickCallback = _ref.onClick,
    testId = _ref.testId,
    content = _ref.content,
    props = _objectWithoutProperties(_ref, _excluded);
  var _useAnalyticsEvents = useAnalyticsEvents(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var _useState = useState(messages.copy_summary_action_description),
    _useState2 = _slicedToArray(_useState, 2),
    tooltipMessage = _useState2[0],
    setTooltipMessage = _useState2[1];
  var handleCopySummaryClick = useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          fireEvent('ui.button.clicked.copySummary', {});
          _context.next = 3;
          return navigator.clipboard.writeText(content !== null && content !== void 0 ? content : '');
        case 3:
          setTooltipMessage(messages.copied_summary_action_description);
          onClickCallback === null || onClickCallback === void 0 || onClickCallback();
        case 5:
        case "end":
          return _context.stop();
      }
    }, _callee);
  })), [fireEvent, onClickCallback, content]);
  return /*#__PURE__*/React.createElement(Action, _extends({
    content: /*#__PURE__*/React.createElement(FormattedMessage, messages.copy_summary_action),
    icon: /*#__PURE__*/React.createElement(CopyIcon, {
      color: "currentColor",
      spacing: "spacious",
      label: "Copy Summary"
    }),
    onClick: handleCopySummaryClick,
    testId: "".concat(testId, "-copy-summary-action"),
    tooltipMessage: /*#__PURE__*/React.createElement(FormattedMessage, tooltipMessage),
    tooltipOnHide: function tooltipOnHide() {
      return setTooltipMessage(messages.copy_summary_action_description);
    }
  }, props));
}