"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _reactIntl = require("react-intl");
var _automation = _interopRequireDefault(require("@atlaskit/icon/core/automation"));
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _compiled = require("@atlaskit/primitives/compiled");
var _expValEqualsNoExposure = require("@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure");
var _useAnalyticsEvents2 = require("../../../../../common/analytics/generated/use-analytics-events");
var _constants = require("../../../../../constants");
var _messages = require("../../../../../messages");
var _flexibleUiContext = require("../../../../../state/flexible-ui-context");
var _useBlockCardRovoActionExperiment = require("../../../../../state/hooks/use-block-card-rovo-action-experiment");
var _modal = require("../../../../../state/modal");
var _action = _interopRequireDefault(require("../action"));
var _utils = require("./utils");
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" != (0, _typeof2.default)(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); }
var AutomationModal = /*#__PURE__*/(0, _react.lazy)(function () {
  return Promise.resolve().then(function () {
    return _interopRequireWildcard(require( /* webpackChunkName: "@atlaskit-internal_smart-card-automation-modal" */'./automation-manual-triggers/manual-triggers-modal'));
  });
});
var AutomationAction = function AutomationAction(props) {
  var _context$actions;
  var _useIntl = (0, _reactIntl.useIntl)(),
    formatMessage = _useIntl.formatMessage;
  var modal = (0, _modal.useSmartLinkModal)();
  var onClickCallback = props.onClick;
  var context = (0, _flexibleUiContext.useFlexibleUiContext)();
  var _useAnalyticsEvents = (0, _useAnalyticsEvents2.useAnalyticsEvents)(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var automationActionData = context === null || context === void 0 || (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[_constants.ActionName.AutomationAction];
  var isRovoBlockCardExperimentEnabled = (0, _useBlockCardRovoActionExperiment.useBlockCardRovoActionExperimentNoExposure)();
  var automationActionOnClick = (0, _react.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 = (0, _utils.getModalContent)(product, resourceType) || {},
      modalTitle = _ref.modalTitle,
      modalDescription = _ref.modalDescription;
    var automationModalTitle = modalTitle ? /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, modalTitle) : undefined;
    var automationModalDescription = modalDescription ? /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, (0, _extends2.default)({}, modalDescription, {
      values: {
        name: objectName,
        b: function b(chunks) {
          return /*#__PURE__*/_react.default.createElement(_compiled.Text, {
            as: "strong"
          }, chunks);
        },
        br: /*#__PURE__*/_react.default.createElement("br", null)
      }
    })) : undefined;
    modal.open( /*#__PURE__*/_react.default.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.default.createElement(_reactIntl.FormattedMessage, _messages.messages.automation_action_title);
  var automationActionTooltip = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _messages.messages.automation_action_tooltip);
  var automationActionIconLabel = formatMessage(_messages.messages.automation_action_icon_label);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_action.default, (0, _extends2.default)({
    content: automationActionTitle,
    icon: /*#__PURE__*/_react.default.createElement(_automation.default, (0, _extends2.default)({
      spacing: "spacious",
      color: "currentColor",
      label: automationActionIconLabel
    }, (0, _platformFeatureFlags.fg)('platform_sl_3p_auth_rovo_action_kill_switch') || isRovoBlockCardExperimentEnabled || (0, _expValEqualsNoExposure.expValEqualsNoExposure)('rovogrowth-640-inline-action-nudge-exp', 'isEnabled', true) ? {
      size: props.iconSize
    } : {})),
    testId: "smart-action-automation-action",
    tooltipMessage: automationActionTooltip
  }, props, {
    onClick: function onClick() {
      return automationActionOnClick(automationActionData);
    }
  })));
};
var _default = exports.default = AutomationAction;