"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ApixAdaptor = exports.getApixAdaptor = void 0;

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

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

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

var _cloneDeep = _interopRequireDefault(require("lodash/cloneDeep"));

var _adaptorUtils = require("./adaptorUtils");

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

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 getApixAdaptor = () => (0, _extensionUtils.getEnvAdaptor)();

exports.getApixAdaptor = getApixAdaptor;

class ApixAdaptor extends _extensionUtils.BrowserAdaptor {
  constructor(sdk, fallbackVersionsUrl) {
    super(sdk);
    this.fallbackVersionsUrl = fallbackVersionsUrl;
  }

  fetchSpecList(versionsUrl) {
    var _this = this;

    return _asyncToGenerator(function* () {
      var data = yield _this.localStorageGetItem(_runIt.RunItConfigKey);
      var config = data ? JSON.parse(data) : _runIt.RunItNoConfig;
      var url;

      if (versionsUrl) {
        url = versionsUrl;
      } else {
        url = config.base_url ? "".concat(config.base_url, "/versions") : "".concat(_this.fallbackVersionsUrl, "/versions.json");
      }

      var versions = yield _this.sdk.authSession.transport.rawRequest('GET', url);
      var specs = yield (0, _sdkCodegen.getSpecsFromVersions)(JSON.parse(versions.body));
      return specs;
    })();
  }

  fetchSpec(spec) {
    return _asyncToGenerator(function* () {
      var _spec = (0, _cloneDeep.default)(spec);

      _spec.specURL = (0, _adaptorUtils.fullify)(spec.specURL, origin);
      _spec.api = yield (0, _adaptorUtils.fallbackFetch)(_spec, _adaptorUtils.funFetch);
      return _spec;
    })();
  }

}

exports.ApixAdaptor = ApixAdaptor;
//# sourceMappingURL=apixAdaptor.js.map