"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getRetryOptions = exports.getContextByStatus = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _linkExtractors = require("@atlaskit/link-extractors");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _constants = require("../../constants");
var _context = require("../../extractors/common/context");
var _flexible = _interopRequireDefault(require("../../extractors/flexible"));
var _extractLinkTitle = _interopRequireDefault(require("../../extractors/flexible/extract-link-title"));
var _extractPreview = require("../../extractors/flexible/extract-preview");
var _extractProvider = _interopRequireDefault(require("../../extractors/flexible/extract-provider"));
var _icon = require("../../extractors/flexible/icon");
var _extractProviderIcon = require("../../extractors/flexible/icon/extract-provider-icon");
var _utils = require("../../extractors/flexible/utils");
var _messages = require("../../messages");
var _utils2 = require("../../utils");
var _jsonld = require("../../utils/jsonld");
var getContextByStatus = exports.getContextByStatus = function getContextByStatus(params) {
  var _response$meta;
  var _ref = params !== null && params !== void 0 ? params : {},
    onClick = _ref.onClick,
    response = _ref.response,
    status = _ref.status,
    url = _ref.url;
  switch (status) {
    case _constants.SmartLinkStatus.Pending:
    case _constants.SmartLinkStatus.Resolving:
      return {
        linkTitle: (0, _extractLinkTitle.default)(status, url, response, onClick),
        url: url
      };
    case _constants.SmartLinkStatus.Resolved:
      return (0, _flexible.default)(params);
    case _constants.SmartLinkStatus.Unauthorized:
    case _constants.SmartLinkStatus.Forbidden:
    case _constants.SmartLinkStatus.NotFound:
    case _constants.SmartLinkStatus.Errored:
    case _constants.SmartLinkStatus.Fallback:
    default:
      return {
        url: url,
        linkIcon: (0, _icon.extractErrorIcon)(response, status),
        linkTitle: (0, _extractLinkTitle.default)(status, url, response, onClick),
        preview: (0, _extractPreview.extractSmartLinkPreviewImage)(response),
        provider: (0, _platformFeatureFlags.fg)('platform_sl_google_rebrand') ? (0, _extractProvider.default)(response) : (0, _extractProviderIcon.extractSmartLinkProviderIcon)(response),
        actions: (0, _defineProperty2.default)({}, _constants.InternalActionName.UnresolvedAction, getRetryOptions(url, status, response, params.onAuthorize)),
        meta: {
          accessType: response === null || response === void 0 || (_response$meta = response.meta) === null || _response$meta === void 0 || (_response$meta = _response$meta.requestAccess) === null || _response$meta === void 0 ? void 0 : _response$meta.accessType
        },
        hostName: (0, _utils.extractHostName)(response)
      };
  }
};
var getForbiddenMessageKey = function getForbiddenMessageKey(meta) {
  var _meta$requestAccess;
  var accessType = meta === null || meta === void 0 || (_meta$requestAccess = meta.requestAccess) === null || _meta$requestAccess === void 0 ? void 0 : _meta$requestAccess.accessType;
  switch (accessType) {
    case 'DIRECT_ACCESS':
      return (0, _platformFeatureFlags.fg)('confluence-issue-terminology-refresh') ? 'join_to_viewIssueTermRefresh' : 'join_to_view';
    case 'REQUEST_ACCESS':
      return (0, _platformFeatureFlags.fg)('confluence-issue-terminology-refresh') ? 'request_access_to_viewIssueTermRefresh' : 'request_access_to_view';
    case 'PENDING_REQUEST_EXISTS':
      return 'pending_request';
    case 'FORBIDDEN':
      return 'forbidden_access';
    case 'DENIED_REQUEST_EXISTS':
      return 'request_denied';
    default:
      return 'restricted_link';
  }
};
var getRetryOptions = exports.getRetryOptions = function getRetryOptions(url, status, response, onAuthorize) {
  var _response$meta2, _access$action, _access$buttonDisable;
  var provider = (0, _linkExtractors.extractSmartLinkProvider)(response);
  var context = provider === null || provider === void 0 ? void 0 : provider.text;
  var values = context ? {
    context: context
  } : undefined;
  switch (status) {
    case _constants.SmartLinkStatus.Forbidden:
      var meta = (_response$meta2 = response === null || response === void 0 ? void 0 : response.meta) !== null && _response$meta2 !== void 0 ? _response$meta2 : (0, _jsonld.getForbiddenJsonLd)().meta;
      var access = (0, _context.extractRequestAccessContextImproved)({
        jsonLd: meta,
        url: url,
        product: context !== null && context !== void 0 ? context : ''
      });
      var messageKey = getForbiddenMessageKey(meta);
      var descriptor = _messages.messages[messageKey];
      var retry = onAuthorize || (access === null || access === void 0 || (_access$action = access.action) === null || _access$action === void 0 ? void 0 : _access$action.promise);
      var onClick = retry && !((_access$buttonDisable = access === null || access === void 0 ? void 0 : access.buttonDisabled) !== null && _access$buttonDisable !== void 0 ? _access$buttonDisable : false) ? (0, _utils2.handleOnClick)(retry) : undefined;
      return {
        descriptor: descriptor,
        onClick: onClick,
        values: values
      };
    case _constants.SmartLinkStatus.Unauthorized:
      return onAuthorize ? {
        descriptor: _messages.messages.connect_link_account_card_name,
        onClick: (0, _utils2.handleOnClick)(onAuthorize),
        values: values
      } : undefined;
    case _constants.SmartLinkStatus.NotFound:
      return {
        descriptor: _messages.messages.cannot_find_link
      };
  }
};