"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "extractRequestAccessContextImproved", {
  enumerable: true,
  get: function get() {
    return _extractAccessContext.extractRequestAccessContextImproved;
  }
});
exports.extractSmartLinkContext = extractSmartLinkContext;
exports.generateContext = generateContext;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _linkExtractors = require("@atlaskit/link-extractors");
var _jsonld = require("../../../utils/jsonld");
var _prioritiseIcon = require("../icon/prioritiseIcon");
var _extractAccessContext = require("./extractAccessContext");
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) { (0, _defineProperty2.default)(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; }
function generateContext(jsonLd) {
  var provider = (0, _linkExtractors.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 = (0, _prioritiseIcon.prioritiseIcon)({
    fileFormatIcon: undefined,
    documentTypeIcon: undefined,
    urlIcon: (0, _linkExtractors.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' && (0, _linkExtractors.extractProviderIcon)(generator.icon)
  });
  return _objectSpread(_objectSpread({}, provider), {}, {
    icon: icon ? icon : provider.icon
  });
}

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