import _extends from "@babel/runtime/helpers/extends";
import React, { lazy, useCallback } from 'react';
import { FormattedMessage, useIntl } from 'react-intl';
import AutomationIcon from '@atlaskit/icon/core/automation';
import { fg } from '@atlaskit/platform-feature-flags';
import { Text } from '@atlaskit/primitives/compiled';
import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure';
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';
const AutomationModal = /*#__PURE__*/lazy(() => import( /* webpackChunkName: "@atlaskit-internal_smart-card-automation-modal" */'./automation-manual-triggers/manual-triggers-modal'));
const AutomationAction = props => {
  var _context$actions;
  const {
    formatMessage
  } = useIntl();
  const modal = useSmartLinkModal();
  const {
    onClick: onClickCallback
  } = props;
  const context = useFlexibleUiContext();
  const {
    fireEvent
  } = useAnalyticsEvents();
  const automationActionData = context === null || context === void 0 ? void 0 : (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[ActionName.AutomationAction];
  const automationActionOnClick = useCallback(automationActionData => {
    const {
      product,
      resourceType,
      baseAutomationUrl,
      objectAri,
      siteAri,
      canManageAutomation,
      analyticsSource,
      objectName
    } = automationActionData;
    fireEvent('ui.button.clicked.automationAction', {});
    const {
      modalTitle,
      modalDescription
    } = getModalContent(product, resourceType) || {};
    const automationModalTitle = modalTitle ? /*#__PURE__*/React.createElement(FormattedMessage, modalTitle) : undefined;
    const automationModalDescription = modalDescription ? /*#__PURE__*/React.createElement(FormattedMessage, _extends({}, modalDescription, {
      values: {
        name: objectName,
        b: chunks => /*#__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: () => modal.close()
    }));
    onClickCallback === null || onClickCallback === void 0 ? void 0 : onClickCallback();
  }, [modal, onClickCallback, fireEvent]);
  if (!automationActionData) {
    return null;
  }
  const automationActionTitle = /*#__PURE__*/React.createElement(FormattedMessage, messages.automation_action_title);
  const automationActionTooltip = /*#__PURE__*/React.createElement(FormattedMessage, messages.automation_action_tooltip);
  const 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') || fg('platform_sl_3p_auth_rovo_block_card_kill_switch') || expValEqualsNoExposure('rovogrowth-640-inline-action-nudge-exp', 'isEnabled', true) ? {
      size: props.iconSize
    } : {})),
    testId: "smart-action-automation-action",
    tooltipMessage: automationActionTooltip
  }, props, {
    onClick: () => automationActionOnClick(automationActionData)
  })));
};
export default AutomationAction;