import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { useEffect } from "react";
import useActiveDatasetManager from "../use-active-dataset-manager";
import CollaborativeDatasetManager from "udt-dataset-managers/dist/CollaborationServerDatasetManager";
import useAppConfig from "../use-app-config";
import qs from "qs";
export default (function () {
  var _useAppConfig = useAppConfig(),
      fromConfig = _useAppConfig.fromConfig;

  var _useActiveDatasetMana = useActiveDatasetManager(),
      _useActiveDatasetMana2 = _slicedToArray(_useActiveDatasetMana, 2),
      dm = _useActiveDatasetMana2[0],
      setActiveDatasetManager = _useActiveDatasetMana2[1];

  useEffect(function () {
    if (dm) return;
    var queryParams = qs.parse(window.location.search.substr(1));

    function goIntoCollaborativeSession(_x) {
      return _goIntoCollaborativeSession.apply(this, arguments);
    }

    function _goIntoCollaborativeSession() {
      _goIntoCollaborativeSession = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(sessionId) {
        var dm;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                dm = new CollaborativeDatasetManager({
                  serverUrl: fromConfig("collaborationServer.url")
                });
                _context.next = 3;
                return dm.loadSession(sessionId);

              case 3:
                setActiveDatasetManager(dm);

              case 4:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));
      return _goIntoCollaborativeSession.apply(this, arguments);
    }

    if (queryParams.s) {
      goIntoCollaborativeSession(queryParams.s);
    } // Only run on first call (or when the dataset manager can be sent)
    // eslint-disable-next-line

  }, [setActiveDatasetManager]);
});