"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CopySummaryAction = CopySummaryAction;
exports.CopySummaryActionNew = CopySummaryActionNew;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _reactIntl = require("react-intl");
var _copy = _interopRequireDefault(require("@atlaskit/icon/core/copy"));
var _useAnalyticsEvents3 = require("../../../../../common/analytics/generated/use-analytics-events");
var _messages = require("../../../../../messages");
var _action = _interopRequireDefault(require("../action"));
var _excluded = ["url", "onClick", "testId", "content"],
  _excluded2 = ["url", "onClick", "summary", "testId"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function CopySummaryAction(_ref) {
  var url = _ref.url,
    onClickCallback = _ref.onClick,
    testId = _ref.testId,
    content = _ref.content,
    props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var _useAnalyticsEvents = (0, _useAnalyticsEvents3.useAnalyticsEvents)(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var _useState = (0, _react.useState)(_messages.messages.copy_summary_action_description),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    tooltipMessage = _useState2[0],
    setTooltipMessage = _useState2[1];
  var handleCopySummaryClick = (0, _react.useCallback)( /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
    return _regenerator.default.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.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.default.createElement(_action.default, (0, _extends2.default)({
    content: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _messages.messages.copy_summary_action),
    icon: /*#__PURE__*/_react.default.createElement(_copy.default, {
      color: "currentColor",
      spacing: "spacious",
      label: "Copy Summary"
    }),
    onClick: handleCopySummaryClick,
    testId: "".concat(testId, "-copy-summary-action"),
    tooltipMessage: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, tooltipMessage),
    tooltipOnHide: function tooltipOnHide() {
      return setTooltipMessage(_messages.messages.copy_summary_action_description);
    }
  }, props));
}
function CopySummaryActionNew(_ref3) {
  var url = _ref3.url,
    onClickCallback = _ref3.onClick,
    _ref3$summary = _ref3.summary,
    summary = _ref3$summary === void 0 ? '' : _ref3$summary,
    testId = _ref3.testId,
    props = (0, _objectWithoutProperties2.default)(_ref3, _excluded2);
  var _useAnalyticsEvents2 = (0, _useAnalyticsEvents3.useAnalyticsEvents)(),
    fireEvent = _useAnalyticsEvents2.fireEvent;
  var _useState3 = (0, _react.useState)(_messages.messages.copy_summary_action_description),
    _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
    tooltipMessage = _useState4[0],
    setTooltipMessage = _useState4[1];
  var handleCopySummaryClick = (0, _react.useCallback)( /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
    return _regenerator.default.wrap(function _callee2$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          fireEvent('ui.button.clicked.copySummary', {});
          _context2.next = 3;
          return navigator.clipboard.writeText(summary);
        case 3:
          setTooltipMessage(_messages.messages.copied_summary_action_description);
          onClickCallback === null || onClickCallback === void 0 || onClickCallback();
        case 5:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  })), [fireEvent, onClickCallback, summary]);
  return /*#__PURE__*/_react.default.createElement(_action.default, (0, _extends2.default)({
    content: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _messages.messages.copy_summary_action),
    icon: /*#__PURE__*/_react.default.createElement(_copy.default, {
      color: "currentColor",
      spacing: "spacious",
      label: "Copy Summary",
      size: props.iconSize
    }),
    onClick: handleCopySummaryClick,
    testId: "".concat(testId, "-copy-summary-action"),
    tooltipMessage: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, tooltipMessage),
    tooltipOnHide: function tooltipOnHide() {
      return setTooltipMessage(_messages.messages.copy_summary_action_description);
    }
  }, props));
}