"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getManifest = getManifest;

function _axios() {
  const data = _interopRequireDefault(require("axios"));

  _axios = function () {
    return data;
  };

  return data;
}

function Versions() {
  const data = _interopRequireWildcard(require("./Versions"));

  Versions = function () {
    return data;
  };

  return data;
}

function _XDLError() {
  const data = _interopRequireDefault(require("./XDLError"));

  _XDLError = function () {
    return data;
  };

  return data;
}

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

async function getManifest(publicUrl, {
  platform = 'all'
} = {}) {
  const req = {
    url: publicUrl,
    method: 'get',
    headers: {
      Accept: 'application/expo+json,application/json'
    }
  };
  let exp;

  try {
    const resp = await _axios().default.request(req);
    exp = resp.data;
  } catch (e) {
    throw new (_XDLError().default)('INVALID_MANIFEST', `Unable to fetch manifest from ${publicUrl}. ` + e.toString());
  }

  exp = await _extractManifest(exp, publicUrl);

  if (exp.platform !== platform && platform !== 'all') {
    throw new (_XDLError().default)('INVALID_MANIFEST', `Manifest from ${publicUrl} is not compatible with the ${platform} platform`);
  }

  return exp;
} // Third party publicUrls can return an array of manifests
// We need to choose the first compatible one


async function _extractManifest(expOrArray, publicUrl) {
  // if its not an array, assume it was a single manifest obj
  if (!Array.isArray(expOrArray)) {
    return expOrArray;
  }

  const {
    sdkVersions
  } = await Versions().versionsAsync();

  for (let i = 0; i < expOrArray.length; i++) {
    const manifestCandidate = expOrArray[i];
    const sdkVersion = manifestCandidate.sdkVersion;

    if (!sdkVersion) {
      continue;
    }

    const versionObj = sdkVersions[sdkVersion];

    if (!versionObj) {
      continue;
    }

    const isDeprecated = versionObj.isDeprecated || false;

    if (!isDeprecated) {
      return manifestCandidate;
    }
  }

  const supportedVersions = Object.keys(sdkVersions);
  throw new (_XDLError().default)('INVALID_MANIFEST', `No compatible manifest found at ${publicUrl}. Please use one of the SDK versions supported: ${JSON.stringify(supportedVersions)}`);
}
//# sourceMappingURL=__sourcemaps__/ThirdParty.js.map