import _extends from "@babel/runtime/helpers/extends";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["onClick"];
import _regeneratorRuntime from "@babel/runtime/regenerator";
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) { _defineProperty(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; }
import React, { useCallback, useState } from 'react';
import { FormattedMessage } from 'react-intl-next';
import LinkIcon from '@atlaskit/icon/core/link';
import { fg } from '@atlaskit/platform-feature-flags';
import { ActionName } from '../../../../../constants';
import { messages } from '../../../../../messages';
import { useFlexibleUiContext } from '../../../../../state/flexible-ui-context';
import useInvokeClientAction from '../../../../../state/hooks/use-invoke-client-action';
import Action from '../action';
var CopyLinkAction = function CopyLinkAction(_ref) {
  var _context$actions;
  var onClickCallback = _ref.onClick,
    props = _objectWithoutProperties(_ref, _excluded);
  var context = useFlexibleUiContext();
  var invoke = useInvokeClientAction({});
  var data = context === null || context === void 0 || (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[ActionName.CopyLinkAction];
  var _useState = useState(messages.copy_url_to_clipboard),
    _useState2 = _slicedToArray(_useState, 2),
    tooltipMessage = _useState2[0],
    setTooltipMessage = _useState2[1];
  var onClick = useCallback(function () {
    if (data !== null && data !== void 0 && data.invokeAction) {
      invoke(_objectSpread(_objectSpread({}, data.invokeAction), {}, {
        actionFn: function () {
          var _actionFn = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
            var _data$invokeAction;
            return _regeneratorRuntime.wrap(function _callee$(_context) {
              while (1) switch (_context.prev = _context.next) {
                case 0:
                  _context.next = 2;
                  return (_data$invokeAction = data.invokeAction) === null || _data$invokeAction === void 0 ? void 0 : _data$invokeAction.actionFn();
                case 2:
                  setTooltipMessage(messages.copied_url_to_clipboard);
                case 3:
                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.createElement(Action, _extends({
    content: /*#__PURE__*/React.createElement(FormattedMessage, messages.copy_url_to_clipboard),
    icon: /*#__PURE__*/React.createElement(LinkIcon, _extends({
      color: "currentColor",
      label: fg('navx-3698-flexible-card-a11y-fix') ? '' : 'copy url',
      spacing: "spacious"
    }, fg('platform_sl_3p_auth_rovo_action_kill_switch') ? {
      size: props.iconSize
    } : {})),
    onClick: onClick,
    testId: "smart-action-copy-link-action",
    tooltipMessage: /*#__PURE__*/React.createElement(FormattedMessage, tooltipMessage),
    tooltipOnHide: function tooltipOnHide() {
      return setTooltipMessage(messages.copy_url_to_clipboard);
    }
  }, data, props)) : null;
};
export default CopyLinkAction;