import _defineProperty from "@babel/runtime/helpers/defineProperty";
import { extractSmartLinkProvider } from '@atlaskit/link-extractors';
import { fg } from '@atlaskit/platform-feature-flags';
import { InternalActionName, SmartLinkStatus } from '../../constants';
import { extractRequestAccessContextImproved } from '../../extractors/common/context';
import extractFlexibleUiContext from '../../extractors/flexible';
import extractLinkTitle from '../../extractors/flexible/extract-link-title';
import { extractSmartLinkPreviewImage } from '../../extractors/flexible/extract-preview';
import extractProvider from '../../extractors/flexible/extract-provider';
import { extractErrorIcon } from '../../extractors/flexible/icon';
import { extractSmartLinkProviderIcon } from '../../extractors/flexible/icon/extract-provider-icon';
import { extractHostName } from '../../extractors/flexible/utils';
import { messages } from '../../messages';
import { handleOnClick } from '../../utils';
import { getForbiddenJsonLd } from '../../utils/jsonld';
export var 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 SmartLinkStatus.Pending:
    case SmartLinkStatus.Resolving:
      return {
        linkTitle: extractLinkTitle(status, url, response, onClick),
        url: url
      };
    case SmartLinkStatus.Resolved:
      return extractFlexibleUiContext(params);
    case SmartLinkStatus.Unauthorized:
    case SmartLinkStatus.Forbidden:
    case SmartLinkStatus.NotFound:
    case SmartLinkStatus.Errored:
    case SmartLinkStatus.Fallback:
    default:
      return {
        url: url,
        linkIcon: extractErrorIcon(response, status),
        linkTitle: extractLinkTitle(status, url, response, onClick),
        preview: extractSmartLinkPreviewImage(response),
        provider: fg('platform_sl_google_rebrand') ? extractProvider(response) : extractSmartLinkProviderIcon(response),
        actions: _defineProperty({}, 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: 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 fg('confluence-issue-terminology-refresh') ? 'join_to_viewIssueTermRefresh' : 'join_to_view';
    case 'REQUEST_ACCESS':
      return 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';
  }
};
export var getRetryOptions = function getRetryOptions(url, status, response, onAuthorize) {
  var _response$meta2, _access$action, _access$buttonDisable;
  var provider = extractSmartLinkProvider(response);
  var context = provider === null || provider === void 0 ? void 0 : provider.text;
  var values = context ? {
    context: context
  } : undefined;
  switch (status) {
    case SmartLinkStatus.Forbidden:
      var meta = (_response$meta2 = response === null || response === void 0 ? void 0 : response.meta) !== null && _response$meta2 !== void 0 ? _response$meta2 : getForbiddenJsonLd().meta;
      var access = extractRequestAccessContextImproved({
        jsonLd: meta,
        url: url,
        product: context !== null && context !== void 0 ? context : ''
      });
      var messageKey = getForbiddenMessageKey(meta);
      var descriptor = 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) ? handleOnClick(retry) : undefined;
      return {
        descriptor: descriptor,
        onClick: onClick,
        values: values
      };
    case SmartLinkStatus.Unauthorized:
      return onAuthorize ? {
        descriptor: messages.connect_link_account_card_name,
        onClick: handleOnClick(onAuthorize),
        values: values
      } : undefined;
    case SmartLinkStatus.NotFound:
      return {
        descriptor: messages.cannot_find_link
      };
  }
};