"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _reactIntl = require("react-intl");
var _new = _interopRequireDefault(require("@atlaskit/button/new"));
var _useAnalyticsEvents2 = require("../../../../common/analytics/generated/use-analytics-events");
var _messages = require("../../../../messages");
var _UnauthorisedViewContent = _interopRequireDefault(require("../../../common/UnauthorisedViewContent"));
var _unresolvedView = _interopRequireDefault(require("../unresolved-view"));
var _unauthorizedSvg = require("./unauthorized-svg");
var _excluded = ["context", "extensionKey", "isProductIntegrationSupported", "onAuthorize", "testId"];
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 UnauthorizedView = function UnauthorizedView(_ref) {
  var _context$image;
  var context = _ref.context,
    extensionKey = _ref.extensionKey,
    isProductIntegrationSupported = _ref.isProductIntegrationSupported,
    onAuthorize = _ref.onAuthorize,
    _ref$testId = _ref.testId,
    testId = _ref$testId === void 0 ? 'embed-card-unauthorized-view' : _ref$testId,
    unresolvedViewProps = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var _useAnalyticsEvents = (0, _useAnalyticsEvents2.useAnalyticsEvents)(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var handleOnAuthorizeClick = (0, _react.useCallback)(function () {
    if (onAuthorize) {
      fireEvent('track.applicationAccount.authStarted', {});
      onAuthorize();
    }
  }, [onAuthorize, fireEvent]);
  var content = (0, _react.useMemo)(function () {
    if (onAuthorize) {
      var _context$text;
      // Our title and button messages always expect the product name to be present
      // while the description support when product name is not present.
      // To be looked at https://product-fabric.atlassian.net/browse/EDM-8173
      var _values = {
        context: (_context$text = context === null || context === void 0 ? void 0 : context.text) !== null && _context$text !== void 0 ? _context$text : ''
      };
      if (_values) {
        // title: Connect your {context} account
        // button: Connect to {context}
        return {
          title: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, (0, _extends2.default)({}, _messages.messages.connect_link_account_card_name, {
            values: _values
          })),
          description: /*#__PURE__*/_react.default.createElement(_UnauthorisedViewContent.default, {
            providerName: context === null || context === void 0 ? void 0 : context.text,
            isProductIntegrationSupported: isProductIntegrationSupported,
            testId: testId
          }),
          button: /*#__PURE__*/_react.default.createElement(_new.default, {
            testId: "connect-account",
            appearance: "primary",
            onClick: handleOnAuthorizeClick
          }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, (0, _extends2.default)({}, _messages.messages.connect_unauthorised_account_action, {
            values: _values
          })))
        };
      }
    }
    var values = context !== null && context !== void 0 && context.text ? {
      context: context === null || context === void 0 ? void 0 : context.text
    } : undefined;
    if (values) {
      // title: We can't display private pages from {context}
      // description: You're trying to preview a link to a private {context} page. We recommend you review the URL or contact the page owner.
      return {
        title: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, (0, _extends2.default)({}, _messages.messages.unauthorised_account_name, {
          values: values
        })),
        description: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, (0, _extends2.default)({}, _messages.messages.unauthorised_account_description, {
          values: values
        }))
      };
    }

    // title: We can't display private pages
    // description: You're trying to preview a link to a private page. We recommend you review the URL or contact the page owner.
    return {
      title: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _messages.messages.unauthorised_account_name_no_provider),
      description: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _messages.messages.unauthorised_account_description_no_provider)
    };
  }, [context === null || context === void 0 ? void 0 : context.text, handleOnAuthorizeClick, isProductIntegrationSupported, onAuthorize, testId]);
  return /*#__PURE__*/_react.default.createElement(_unresolvedView.default, (0, _extends2.default)({}, unresolvedViewProps, content, {
    icon: context === null || context === void 0 ? void 0 : context.icon,
    image: (_context$image = context === null || context === void 0 ? void 0 : context.image) !== null && _context$image !== void 0 ? _context$image : /*#__PURE__*/_react.default.createElement(_unauthorizedSvg.UnauthorizedSVG, null),
    testId: testId,
    text: context === null || context === void 0 ? void 0 : context.text
  }));
};
var _default = exports.default = UnauthorizedView;