import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["context", "extensionKey", "isProductIntegrationSupported", "onAuthorize", "testId"];
import React, { useCallback, useMemo } from 'react';
import { FormattedMessage } from 'react-intl-next';
import Button from '@atlaskit/button/new';
import { useAnalyticsEvents } from '../../../../common/analytics/generated/use-analytics-events';
import { messages } from '../../../../messages';
import UnauthorisedViewContent from '../../../common/UnauthorisedViewContent';
import UnresolvedView from '../unresolved-view';
import { UnauthorizedSVG } from './unauthorized-svg';
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 = _objectWithoutProperties(_ref, _excluded);
  var _useAnalyticsEvents = useAnalyticsEvents(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var handleOnAuthorizeClick = useCallback(function () {
    if (onAuthorize) {
      fireEvent('track.applicationAccount.authStarted', {});
      onAuthorize();
    }
  }, [onAuthorize, fireEvent]);
  var content = 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.createElement(FormattedMessage, _extends({}, messages.connect_link_account_card_name, {
            values: _values
          })),
          description: /*#__PURE__*/React.createElement(UnauthorisedViewContent, {
            providerName: context === null || context === void 0 ? void 0 : context.text,
            isProductIntegrationSupported: isProductIntegrationSupported,
            testId: testId
          }),
          button: /*#__PURE__*/React.createElement(Button, {
            testId: "connect-account",
            appearance: "primary",
            onClick: handleOnAuthorizeClick
          }, /*#__PURE__*/React.createElement(FormattedMessage, _extends({}, 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.createElement(FormattedMessage, _extends({}, messages.unauthorised_account_name, {
          values: values
        })),
        description: /*#__PURE__*/React.createElement(FormattedMessage, _extends({}, 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.createElement(FormattedMessage, messages.unauthorised_account_name_no_provider),
      description: /*#__PURE__*/React.createElement(FormattedMessage, messages.unauthorised_account_description_no_provider)
    };
  }, [context === null || context === void 0 ? void 0 : context.text, handleOnAuthorizeClick, isProductIntegrationSupported, onAuthorize, testId]);
  return /*#__PURE__*/React.createElement(UnresolvedView, _extends({}, 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.createElement(UnauthorizedSVG, null),
    testId: testId,
    text: context === null || context === void 0 ? void 0 : context.text
  }));
};
export default UnauthorizedView;