import { extractEntity, extractImage, isEntityPresent } from '@atlaskit/link-extractors';
import { MediaType } from '../../constants';
const extractPreview = data => {
  if (!data) {
    return undefined;
  }
  const url = extractImage(data);
  return url ? {
    type: MediaType.Image,
    url
  } : undefined;
};
export default extractPreview;

/**
 * Should be moved to link-extractors when jsonLd is deprecated
 */
export const extractSmartLinkPreviewImage = response => {
  if (!response || !(response !== null && response !== void 0 && response.data)) {
    return undefined;
  }
  if (isEntityPresent(response)) {
    var _entity$thumbnail;
    const entity = extractEntity(response);
    let url = entity === null || entity === void 0 ? void 0 : (_entity$thumbnail = entity.thumbnail) === null || _entity$thumbnail === void 0 ? void 0 : _entity$thumbnail.externalUrl;
    return url ? {
      type: MediaType.Image,
      url
    } : undefined;
  }
  return extractPreview(response.data);
};