"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AutomationModalFooter = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactIntlNext = require("react-intl-next");
var _analyticsNext = require("@atlaskit/analytics-next");
var _new = _interopRequireWildcard(require("@atlaskit/button/new"));
var _menuContext = require("../../menu-context");
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" != _typeof(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 i18n = (0, _reactIntlNext.defineMessages)({
  modalFooterCancelButton: {
    id: 'automation-menu.modal.footer.cancel-button',
    defaultMessage: 'Cancel',
    description: 'The text for a cancel button, which closes the active modal.'
  },
  modalFooterAutomateButton: {
    id: 'automation-menu.modal.footer.automate-button',
    defaultMessage: 'Automate',
    description: 'The text for an automate button, which executes an automation rule.'
  },
  modalFooterCreateAutomationButton: {
    id: 'automation-menu.modal.footer.get-started-button',
    defaultMessage: 'Create automation',
    description: 'The text for a button that links to the automation rule builder. This button only appears when the modal has no existing automations to display.'
  },
  modalFooterOkButton: {
    id: 'automation-menu.modal.footer.ok-button',
    defaultMessage: 'Ok',
    description: 'The text for a button that closes the active modal. This button only appears when the modal has no existing automations to display.'
  }
});
var AutomationModalFooter = exports.AutomationModalFooter = function AutomationModalFooter(_ref) {
  var selectedRule = _ref.selectedRule,
    onClose = _ref.onClose;
  var _useAnalyticsEvents = (0, _analyticsNext.useAnalyticsEvents)(),
    createAnalyticsEvent = _useAnalyticsEvents.createAnalyticsEvent;
  var _useAutomationMenu = (0, _menuContext.useAutomationMenu)(),
    canManageAutomation = _useAutomationMenu.canManageAutomation,
    objectAri = _useAutomationMenu.objectAri,
    initialised = _useAutomationMenu.initialised,
    invokeRuleOrShowDialog = _useAutomationMenu.invokeRuleOrShowDialog,
    invokingRuleId = _useAutomationMenu.invokingRuleId,
    rules = _useAutomationMenu.rules,
    fetchError = _useAutomationMenu.fetchError,
    baseAutomationUrl = _useAutomationMenu.baseAutomationUrl,
    analyticsSource = _useAutomationMenu.analyticsSource;
  var sendFooterAnalyticsEvent = function sendFooterAnalyticsEvent(actionSubjectId, ruleId) {
    var event = createAnalyticsEvent({
      type: 'sendUIEvent',
      data: {
        action: 'clicked',
        actionSubject: 'button',
        actionSubjectId: actionSubjectId,
        source: analyticsSource
      }
    });
    if (ruleId) {
      event.payload.data.attributes = {
        ruleId: ruleId
      };
    }
    event.fire();
  };
  var okButton = /*#__PURE__*/_react.default.createElement(_new.default, {
    key: "automation-modal-footer-ok",
    appearance: "primary",
    onClick: function onClick() {
      sendFooterAnalyticsEvent('ok');
      onClose();
    }
  }, /*#__PURE__*/_react.default.createElement(_reactIntlNext.FormattedMessage, i18n.modalFooterOkButton));
  var cancelButton = /*#__PURE__*/_react.default.createElement(_new.default, {
    key: "automation-modal-footer-cancel",
    appearance: "subtle",
    onClick: function onClick() {
      sendFooterAnalyticsEvent('cancel');
      onClose();
    }
  }, /*#__PURE__*/_react.default.createElement(_reactIntlNext.FormattedMessage, i18n.modalFooterCancelButton));
  var createAutomationButton = /*#__PURE__*/_react.default.createElement(_new.LinkButton, {
    key: "automation-modal-footer-create-automation",
    appearance: "primary",
    href: "".concat(baseAutomationUrl, "#rule/new"),
    onClick: function onClick() {
      sendFooterAnalyticsEvent('createAutomation');
    },
    target: "_blank"
  }, /*#__PURE__*/_react.default.createElement(_reactIntlNext.FormattedMessage, i18n.modalFooterCreateAutomationButton));
  var automateButton = /*#__PURE__*/_react.default.createElement(_new.default, {
    key: "automation-modal-footer-automate",
    appearance: "primary",
    onClick: function onClick() {
      if (selectedRule) {
        sendFooterAnalyticsEvent('executeManualTriggerAutomation', selectedRule.id.toString());
        invokeRuleOrShowDialog(selectedRule.id, [objectAri.toString()]);
      }
    },
    isDisabled: !selectedRule || !!invokingRuleId
  }, /*#__PURE__*/_react.default.createElement(_reactIntlNext.FormattedMessage, i18n.modalFooterAutomateButton));
  var modalButtons = [];
  if (!initialised) {
    return null;
  }

  // Populated rule list -> cancel and automate buttons
  // Empty admin scenario -> cancel and Create Automation buttons
  // Empty end user and error scenario -> Ok button
  if (rules.length < 1) {
    if (fetchError || !canManageAutomation) {
      modalButtons.push(okButton);
    } else {
      modalButtons.push(cancelButton, createAutomationButton);
    }
  } else {
    modalButtons.push(cancelButton, automateButton);
  }
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, modalButtons);
};