"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
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 _link = _interopRequireDefault(require("@atlaskit/icon/core/link"));
var _constants = require("../../../../../constants");
var _messages = require("../../../../../messages");
var _flexibleUiContext = require("../../../../../state/flexible-ui-context");
var _useInvokeClientAction = _interopRequireDefault(require("../../../../../state/hooks/use-invoke-client-action"));
var _action = _interopRequireDefault(require("../action"));
var _excluded = ["onClick"];
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var CopyLinkAction = function CopyLinkAction(_ref) {
  var _context$actions;
  var onClickCallback = _ref.onClick,
    props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var intl = (0, _reactIntl.useIntl)();
  var context = (0, _flexibleUiContext.useFlexibleUiContext)();
  var invoke = (0, _useInvokeClientAction.default)({});
  var data = context === null || context === void 0 || (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[_constants.ActionName.CopyLinkAction];
  var _useState = (0, _react.useState)(_messages.messages.copy_url_to_clipboard),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    tooltipMessage = _useState2[0],
    setTooltipMessage = _useState2[1];
  var onClick = (0, _react.useCallback)(function () {
    if (data !== null && data !== void 0 && data.invokeAction) {
      invoke(_objectSpread(_objectSpread({}, data.invokeAction), {}, {
        actionFn: function () {
          var _actionFn = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
            var _data$invokeAction;
            return _regenerator.default.wrap(function (_context) {
              while (1) switch (_context.prev = _context.next) {
                case 0:
                  _context.next = 1;
                  return (_data$invokeAction = data.invokeAction) === null || _data$invokeAction === void 0 ? void 0 : _data$invokeAction.actionFn();
                case 1:
                  setTooltipMessage(_messages.messages.copied_url_to_clipboard);
                case 2:
                case "end":
                  return _context.stop();
              }
            }, _callee);
          }));
          function actionFn() {
            return _actionFn.apply(this, arguments);
          }
          return actionFn;
        }()
      }));
      onClickCallback === null || onClickCallback === void 0 || onClickCallback();
    }
  }, [data, invoke, onClickCallback]);
  return data ? /*#__PURE__*/_react.default.createElement(_action.default, (0, _extends2.default)({
    ariaLabel: intl.formatMessage(_messages.messages.copy_url_to_clipboard),
    content: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _messages.messages.copy_url_to_clipboard),
    icon: /*#__PURE__*/_react.default.createElement(_link.default, {
      color: "currentColor",
      label: "",
      spacing: "spacious",
      size: props.iconSize
    }),
    onClick: onClick,
    testId: "smart-action-copy-link-action",
    tooltipMessage: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, tooltipMessage),
    tooltipOnHide: function tooltipOnHide() {
      return setTooltipMessage(_messages.messages.copy_url_to_clipboard);
    }
  }, data, props)) : null;
};
var _default = exports.default = CopyLinkAction;