"use strict";

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

var _react = _interopRequireWildcard(require("react"));

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

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

var _reactRedux = require("react-redux");

var _reactRouter = require("react-router");

var _ApiExplorer = require("./ApiExplorer");

var _state = require("./state");

var _utils = require("./utils");

var _components = require("./components");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

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

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 StandaloneApiExplorer = _ref => {
  var {
    headless = false
  } = _ref;
  var [browserAdaptor] = (0, _react.useState)(new _utils.ApixAdaptor((0, _runIt.initRunItSdk)(), window.origin));
  var location = (0, _reactRouter.useLocation)();
  var oauthReturn = location.pathname === "/".concat(_utils.oAuthPath);
  var sdk = browserAdaptor.sdk;
  var canLogin = sdk.authSession.settings.authIsConfigured() && !sdk.authSession.isAuthenticated() && !oauthReturn;
  (0, _react.useEffect)(() => {
    var login = function () {
      var _ref2 = _asyncToGenerator(function* () {
        return yield browserAdaptor.login();
      });

      return function login() {
        return _ref2.apply(this, arguments);
      };
    }();

    if (canLogin) {
      login();
    }
  }, []);
  var {
    looker_url
  } = sdk.authSession.settings.getStoredConfig();
  return _react.default.createElement(_reactRedux.Provider, {
    store: _state.store
  }, _react.default.createElement(_runIt.RunItProvider, {
    basePath: "/api/4.0"
  }, canLogin ? _react.default.createElement(_components.Loader, {
    themeOverrides: browserAdaptor.themeOverrides(),
    message: "Configuration found. Logging into ".concat(looker_url)
  }) : _react.default.createElement(_react.default.Fragment, null, oauthReturn ? _react.default.createElement(_extensionUtils.OAuthScene, {
    adaptor: browserAdaptor
  }) : _react.default.createElement(_ApiExplorer.ApiExplorer, {
    adaptor: browserAdaptor,
    headless: headless
  }))));
};

exports.StandaloneApiExplorer = StandaloneApiExplorer;
//# sourceMappingURL=StandaloneApiExplorer.js.map