import { extractEntityIcon, isEntityPresent } from '@atlaskit/link-extractors';
import { fg } from '@atlaskit/platform-feature-flags';
import { IconType, SmartLinkStatus } from '../../../constants';
import extractIconRenderer from './extract-icon-renderer';
import extractJsonldDataIcon from './extract-jsonld-data-icon';
import extractProviderIcon from './extract-provider-icon';
export const extractLinkIcon = (response, renderers) => {
  const data = response.data;
  const render = extractIconRenderer(data, renderers);
  return {
    ...extractJsonldDataIcon(data),
    render
  };
};
export const extractErrorIcon = (response, status) => {
  // Try to get provider icon first.
  if (response) {
    const data = response.data;
    const {
      icon,
      url
    } = extractProviderIcon(data) || {};
    if (icon || url) {
      return {
        icon,
        url
      };
    }
  }

  // Otherwise, use fallback icon for each status.
  switch (status) {
    case SmartLinkStatus.Forbidden:
    case SmartLinkStatus.Unauthorized:
      return {
        icon: IconType.Forbidden
      };
    case SmartLinkStatus.NotFound:
      return {
        icon: IconType.Error
      };
    case SmartLinkStatus.Errored:
    case SmartLinkStatus.Fallback:
    default:
      return {
        icon: IconType.Default
      };
  }
};

/**
 * Should be moved to link-extractors when jsonLd is deprecated
 */
export const extractSmartLinkIcon = (response, renderers) => {
  if (!response || !(response !== null && response !== void 0 && response.data)) {
    return undefined;
  }
  if (fg('platform_lp_use_entity_icon_url_for_icon')) {
    if (isEntityPresent(response)) {
      const entityIcon = extractEntityIcon(response);
      if (entityIcon) {
        return entityIcon;
      }
    }
  } else {
    if (isEntityPresent(response)) {
      return extractEntityIcon(response);
    }
  }
  return extractLinkIcon(response, renderers);
};