import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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 var extractLinkIcon = function extractLinkIcon(response, renderers) {
  var data = response.data;
  var render = extractIconRenderer(data, renderers);
  return _objectSpread(_objectSpread({}, extractJsonldDataIcon(data)), {}, {
    render: render
  });
};
export var extractErrorIcon = function extractErrorIcon(response, status) {
  // Try to get provider icon first.
  if (response) {
    var data = response.data;
    var _ref = extractProviderIcon(data) || {},
      icon = _ref.icon,
      url = _ref.url;
    if (icon || url) {
      return {
        icon: icon,
        url: 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 var extractSmartLinkIcon = function 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)) {
      var entityIcon = extractEntityIcon(response);
      if (entityIcon) {
        return entityIcon;
      }
    }
  } else {
    if (isEntityPresent(response)) {
      return extractEntityIcon(response);
    }
  }
  return extractLinkIcon(response, renderers);
};