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 { extractEntityProvider, extractProvider, extractProviderIcon, extractUrlFromIconJsonLd, isEntityPresent } from '@atlaskit/link-extractors';
import { getEmptyJsonLd } from '../../../utils/jsonld';
import { prioritiseIcon } from '../icon/prioritiseIcon';
export { extractRequestAccessContextImproved } from './extractAccessContext';
export function generateContext(jsonLd) {
  var provider = extractProvider(jsonLd);
  if (!provider) {
    return undefined;
  }

  // If no icon is supplied, return existing context
  if (!jsonLd.icon) {
    return provider;
  }
  var generator = jsonLd.generator;
  var icon = prioritiseIcon({
    fileFormatIcon: undefined,
    documentTypeIcon: undefined,
    urlIcon: extractUrlFromIconJsonLd(jsonLd.icon),
    // We still attempt to follow the icon priority function even if no generator (and therefore provider icon) is defined
    providerIcon: generator && typeof generator !== 'string' && generator['@type'] !== 'Link' && extractProviderIcon(generator.icon)
  });
  return _objectSpread(_objectSpread({}, provider), {}, {
    icon: icon ? icon : provider.icon
  });
}

/**
 * Should be moved to link-extractors when jsonLd is deprecated
 */
export function extractSmartLinkContext(response) {
  if (isEntityPresent(response)) {
    return extractEntityProvider(response);
  }
  return generateContext((response === null || response === void 0 ? void 0 : response.data) || getEmptyJsonLd());
}