import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["onClick"];
import React, { lazy, useCallback } from 'react';
import { FormattedMessage } from 'react-intl-next';
import { useAnalyticsEvents } from '../../../../../common/analytics/generated/use-analytics-events';
import { messages } from '../../../../../messages';
import { useFlexibleUiContext } from '../../../../../state/flexible-ui-context';
import { useSmartLinkModal } from '../../../../../state/modal';
import Action from '../action';
import RelatedLinksActionIcon from './related-links-action-icon';
var RelatedLinksModal = /*#__PURE__*/lazy(function () {
  return import( /* webpackChunkName: "@atlaskit-internal_smartcard-RelatedLinksModal" */'../../../../RelatedLinksModal');
});
var ViewRelatedLinksAction = function ViewRelatedLinksAction(_ref) {
  var _context$actions;
  var onClickCallback = _ref.onClick,
    props = _objectWithoutProperties(_ref, _excluded);
  var modal = useSmartLinkModal();
  var context = useFlexibleUiContext();
  var actionData = context === null || context === void 0 || (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions.ViewRelatedLinksAction;
  var _useAnalyticsEvents = useAnalyticsEvents(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var onClick = useCallback(function () {
    var ari = actionData === null || actionData === void 0 ? void 0 : actionData.ari;
    if (ari) {
      modal.open( /*#__PURE__*/React.createElement(RelatedLinksModal, {
        ari: ari,
        showModal: true,
        onClose: function onClose() {
          return modal.close();
        }
      }));
    }
    fireEvent('ui.button.clicked.relatedLinks', {});
    onClickCallback === null || onClickCallback === void 0 || onClickCallback();
  }, [actionData === null || actionData === void 0 ? void 0 : actionData.ari, fireEvent, modal, onClickCallback]);
  return actionData ? /*#__PURE__*/React.createElement(Action, _extends({
    content: /*#__PURE__*/React.createElement(FormattedMessage, messages.related_links_view_related_links),
    icon: /*#__PURE__*/React.createElement(RelatedLinksActionIcon, null),
    onClick: onClick,
    testId: "smart-action-view-related-links-action",
    ariaLabel: "View most recent pages or content types coming from or found on this link"
  }, props)) : null;
};
export default ViewRelatedLinksAction;