import _extends from "@babel/runtime/helpers/extends";
import React, { lazy, useCallback } from 'react';
import { FormattedMessage, useIntl } from 'react-intl-next';
import AutomationIcon from '@atlaskit/icon/core/automation';
import { fg } from '@atlaskit/platform-feature-flags';
import { Text } from '@atlaskit/primitives/compiled';
import { useAnalyticsEvents } from '../../../../../common/analytics/generated/use-analytics-events';
import { ActionName } from '../../../../../constants';
import { messages } from '../../../../../messages';
import { useFlexibleUiContext } from '../../../../../state/flexible-ui-context';
import { useSmartLinkModal } from '../../../../../state/modal';
import Action from '../action';
import { getModalContent } from './utils';
var AutomationModal = /*#__PURE__*/lazy(function () {
  return import( /* webpackChunkName: "@atlaskit-internal_smart-card-automation-modal" */'./automation-manual-triggers/manual-triggers-modal');
});
var AutomationAction = function AutomationAction(props) {
  var _context$actions;
  var _useIntl = useIntl(),
    formatMessage = _useIntl.formatMessage;
  var modal = useSmartLinkModal();
  var onClickCallback = props.onClick;
  var context = useFlexibleUiContext();
  var _useAnalyticsEvents = useAnalyticsEvents(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var automationActionData = context === null || context === void 0 || (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[ActionName.AutomationAction];
  var automationActionOnClick = useCallback(function (automationActionData) {
    var product = automationActionData.product,
      resourceType = automationActionData.resourceType,
      baseAutomationUrl = automationActionData.baseAutomationUrl,
      objectAri = automationActionData.objectAri,
      siteAri = automationActionData.siteAri,
      canManageAutomation = automationActionData.canManageAutomation,
      analyticsSource = automationActionData.analyticsSource,
      objectName = automationActionData.objectName;
    fireEvent('ui.button.clicked.automationAction', {});
    var _ref = getModalContent(product, resourceType) || {},
      modalTitle = _ref.modalTitle,
      modalDescription = _ref.modalDescription;
    var automationModalTitle = modalTitle ? /*#__PURE__*/React.createElement(FormattedMessage, modalTitle) : undefined;
    var automationModalDescription = modalDescription ? /*#__PURE__*/React.createElement(FormattedMessage, _extends({}, modalDescription, {
      values: {
        name: objectName,
        b: function b(chunks) {
          return /*#__PURE__*/React.createElement(Text, {
            as: "strong"
          }, chunks);
        },
        br: /*#__PURE__*/React.createElement("br", null)
      }
    })) : undefined;
    modal.open( /*#__PURE__*/React.createElement(AutomationModal, {
      baseAutomationUrl: baseAutomationUrl,
      objectAri: objectAri,
      siteAri: siteAri,
      canManageAutomation: canManageAutomation,
      analyticsSource: analyticsSource,
      modalTitle: automationModalTitle,
      modalDescription: automationModalDescription,
      onClose: function onClose() {
        return modal.close();
      }
    }));
    onClickCallback === null || onClickCallback === void 0 || onClickCallback();
  }, [modal, onClickCallback, fireEvent]);
  if (!automationActionData) {
    return null;
  }
  var automationActionTitle = /*#__PURE__*/React.createElement(FormattedMessage, messages.automation_action_title);
  var automationActionTooltip = /*#__PURE__*/React.createElement(FormattedMessage, messages.automation_action_tooltip);
  var automationActionIconLabel = formatMessage(messages.automation_action_icon_label);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Action, _extends({
    content: automationActionTitle,
    icon: /*#__PURE__*/React.createElement(AutomationIcon, _extends({
      spacing: "spacious",
      color: "currentColor",
      label: automationActionIconLabel
    }, fg('platform_sl_3p_auth_rovo_action_kill_switch') ? {
      size: props.iconSize
    } : {})),
    testId: "smart-action-automation-action",
    tooltipMessage: automationActionTooltip
  }, props, {
    onClick: function onClick() {
      return automationActionOnClick(automationActionData);
    }
  })));
};
export default AutomationAction;