"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.fallbackFetch = exports.sdkSpecFetch = exports.specUrlFetch = exports.funFetch = exports.fullify = void 0;

var _sdkCodegen = require("@looker/sdk-codegen");

var _sdk = require("@looker/sdk");

var _extensionUtils = require("@looker/extension-utils");

var _runIt = require("@looker/run-it");

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

var makeApi = content => {
  var json;

  if (typeof content === 'string') {
    json = JSON.parse(content);
  } else {
    json = content;
  }

  json = (0, _sdkCodegen.upgradeSpecObject)(json);
  return _sdkCodegen.ApiModel.fromJson(json);
};

var fullify = (uri, baseUrl) => {
  if (uri.match(/^https?:/)) {
    return uri;
  }

  var url = new URL(uri, baseUrl);
  return url.toString();
};

exports.fullify = fullify;

var apiSpecBits = spec => {
  var _spec$specURL;

  return ((_spec$specURL = spec.specURL) === null || _spec$specURL === void 0 ? void 0 : _spec$specURL.split('/').slice(-2)) || [];
};

var funFetch = (version, name) => {
  var sdk = (0, _extensionUtils.getEnvAdaptor)().sdk;
  return sdk.ok((0, _sdk.api_spec)(sdk, version, name));
};

exports.funFetch = funFetch;

var specUrlFetch = function () {
  var _ref = _asyncToGenerator(function* (url) {
    try {
      var content = yield (0, _runIt.getUrl)(url);
      return makeApi(content);
    } catch (error) {
      return undefined;
    }
  });

  return function specUrlFetch(_x) {
    return _ref.apply(this, arguments);
  };
}();

exports.specUrlFetch = specUrlFetch;

var sdkSpecFetch = function () {
  var _ref2 = _asyncToGenerator(function* (spec, fetcher) {
    if (spec.api) return spec.api;
    if (!spec.specURL) return undefined;
    var [version, name] = apiSpecBits(spec);
    var content = yield fetcher(version, name);
    return makeApi(content);
  });

  return function sdkSpecFetch(_x2, _x3) {
    return _ref2.apply(this, arguments);
  };
}();

exports.sdkSpecFetch = sdkSpecFetch;

var fallbackFetch = function () {
  var _ref3 = _asyncToGenerator(function* (spec, fetcher) {
    if (spec.api) return spec.api;
    if (!spec.specURL) return undefined;
    var api;

    try {
      api = yield specUrlFetch(spec.specURL);
    } catch (error) {
      console.error({
        error
      });
    }

    if (!api) {
      var sdk = (0, _extensionUtils.getEnvAdaptor)().sdk;
      var authed = sdk.authSession.isAuthenticated();

      if (!authed) {
        yield sdk.authSession.login();
      }

      api = yield sdkSpecFetch(spec, fetcher);
    }

    return api;
  });

  return function fallbackFetch(_x4, _x5) {
    return _ref3.apply(this, arguments);
  };
}();

exports.fallbackFetch = fallbackFetch;
//# sourceMappingURL=adaptorUtils.js.map