"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
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 (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; }
async function shutdownXR(session) {
  if (session) {
    await session.end();
  }
}
const initializeWebXR = (view, options) => {
  const scale = options.scale || 1.0;
  const xr = view.mainLoop.gfxEngine.renderer.xr;
  xr.addEventListener('sessionstart', () => {
    const camera = view.camera.camera3D;
    const exitXRSession = 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');
    const position = view.camera.position();
    const geodesicNormal = new THREE.Quaternion().setFromUnitVectors(new THREE.Vector3(0, 0, 1), position.geodesicNormal).invert();
    const quat = new THREE.Quaternion(-1, 0, 0, 1).normalize().multiply(geodesicNormal);
    const trans = camera.position.clone().multiplyScalar(-scale).applyQuaternion(quat);
    const transform = new XRRigidTransform(trans, quat);
    const baseReferenceSpace = xr.getReferenceSpace();
    const 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(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;