"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var THREE = _interopRequireWildcard(require("three"));
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 shutdownXR() {
  return _shutdownXR.apply(this, arguments);
}
function _shutdownXR() {
  _shutdownXR = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(session) {
    return _regenerator["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (!session) {
            _context.next = 3;
            break;
          }
          _context.next = 3;
          return session.end();
        case 3:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return _shutdownXR.apply(this, arguments);
}
var initializeWebXR = function (view, options) {
  var scale = options.scale || 1.0;
  var xr = view.mainLoop.gfxEngine.renderer.xr;
  xr.addEventListener('sessionstart', function () {
    var camera = view.camera.camera3D;
    var exitXRSession = function (event) {
      if (event.key === 'Escape') {
        document.removeEventListener('keydown', exitXRSession);
        xr.enabled = false;
        view.camera.camera3D = camera;
        view.scene.scale.multiplyScalar(1 / scale);
        view.scene.updateMatrixWorld();
        shutdownXR(xr.getSession());
        view.notifyChange(view.camera.camera3D, true);
      }
    };
    view.scene.scale.multiplyScalar(scale);
    view.scene.updateMatrixWorld();
    xr.enabled = true;
    xr.getReferenceSpace('local');
    var position = view.camera.position();
    var geodesicNormal = new THREE.Quaternion().setFromUnitVectors(new THREE.Vector3(0, 0, 1), position.geodesicNormal).invert();
    var quat = new THREE.Quaternion(-1, 0, 0, 1).normalize().multiply(geodesicNormal);
    var trans = camera.position.clone().multiplyScalar(-scale).applyQuaternion(quat);
    var transform = new XRRigidTransform(trans, quat);
    var baseReferenceSpace = xr.getReferenceSpace();
    var teleportSpaceOffset = baseReferenceSpace.getOffsetReferenceSpace(transform);
    xr.setReferenceSpace(teleportSpaceOffset);
    view.camera.camera3D = xr.getCamera();
    view.camera.resize(view.camera.width, view.camera.height);
    document.addEventListener('keydown', exitXRSession, false);

    // TODO Fix asynchronization between xr and MainLoop render loops.
    // (see MainLoop#scheduleViewUpdate).
    xr.setAnimationLoop(function (timestamp) {
      if (xr.isPresenting && view.camera.camera3D.cameras[0]) {
        view.camera.camera3D.updateMatrix();
        view.camera.camera3D.updateMatrixWorld(true);
        view.notifyChange(view.camera.camera3D, true);
      }
      view.mainLoop.step(view, timestamp);
    });
  });
};
var _default = initializeWebXR;
exports["default"] = _default;