"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/defineProperty"),o=require("three"),t=require("../loaders/GLTFLoader.cjs.js"),n=require("../libs/MotionControllers.cjs.js");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var l=r(e);const s=(e,t)=>{t.traverse((t=>{t instanceof o.Mesh&&"envMap"in t.material&&(t.material.envMap=e,t.material.needsUpdate=!0)}))};class a extends o.Object3D{constructor(){super(),l.default(this,"envMap",void 0),l.default(this,"motionController",void 0),this.motionController=null,this.envMap=null}setEnvironmentMap(e){return this.envMap==e||(this.envMap=e,s(this.envMap,this)),this}updateMatrixWorld(e){super.updateMatrixWorld(e),this.motionController&&(this.motionController.updateFromGamepad(),Object.values(this.motionController.components).forEach((e=>{Object.values(e.visualResponses).forEach((e=>{const{valueNode:o,minNode:t,maxNode:r,value:l,valueNodeProperty:s}=e;o&&(s===n.MotionControllerConstants.VisualResponseProperty.VISIBILITY&&"boolean"==typeof l?o.visible=l:s===n.MotionControllerConstants.VisualResponseProperty.TRANSFORM&&t&&r&&"number"==typeof l&&(o.quaternion.slerpQuaternions(t.quaternion,r.quaternion,l),o.position.lerpVectors(t.position,r.position,l)))}))})))}}function i(e,t){!function(e,t){Object.values(e.components).forEach((e=>{const{type:r,touchPointNodeName:l,visualResponses:s}=e;if(r===n.MotionControllerConstants.ComponentType.TOUCHPAD&&l)if(e.touchPointNode=t.getObjectByName(l),e.touchPointNode){const t=new o.SphereGeometry(.001),n=new o.MeshBasicMaterial({color:255}),r=new o.Mesh(t,n);e.touchPointNode.add(r)}else console.warn(`Could not find touch dot, ${e.touchPointNodeName}, in touchpad component ${e.id}`);Object.values(s).forEach((e=>{const{valueNodeName:o,minNodeName:r,maxNodeName:l,valueNodeProperty:s}=e;if(s===n.MotionControllerConstants.VisualResponseProperty.TRANSFORM&&r&&l){if(e.minNode=t.getObjectByName(r),e.maxNode=t.getObjectByName(l),!e.minNode)return void console.warn(`Could not find ${r} in the model`);if(!e.maxNode)return void console.warn(`Could not find ${l} in the model`)}e.valueNode=t.getObjectByName(o),e.valueNode||console.warn(`Could not find ${o} in the model`)}))}))}(e.motionController,t),e.envMap&&s(e.envMap,t),e.add(t)}exports.XRControllerModelFactory=class{constructor(e=null,o="https://cdn.jsdelivr.net/npm/@webxr-input-profiles/assets@1.0/dist/profiles"){l.default(this,"gltfLoader",void 0),l.default(this,"path",void 0),l.default(this,"_assetCache",void 0),this.gltfLoader=e,this.path=o,this._assetCache={},this.gltfLoader||(this.gltfLoader=new t.GLTFLoader)}createControllerModel(e,o,t){const r=new a;let l=null;return e.addEventListener("connected",(e=>{const t=e.data;"tracked-pointer"===t.targetRayMode&&t.gamepad&&n.fetchProfile(t,this.path,"generic-trigger").then((({profile:e,assetPath:s})=>{if(!s)throw new Error("no asset path");r.motionController=new n.MotionController(t,e,s),null==o||o(r.motionController);const a=this._assetCache[r.motionController.assetUrl];if(a)l=a.scene.clone(),i(r,l);else{if(!this.gltfLoader)throw new Error("GLTFLoader not set.");this.gltfLoader.setPath(""),this.gltfLoader.load(r.motionController.assetUrl,(e=>{r.motionController?(this._assetCache[r.motionController.assetUrl]=e,l=e.scene.clone(),i(r,l)):console.warn("motionController gone while gltf load, bailing...")}),null,(()=>{var e;throw new Error(`Asset ${null===(e=r.motionController)||void 0===e?void 0:e.assetUrl} missing or malformed.`)}))}})).catch((e=>{console.warn(e)}))})),e.addEventListener("disconnected",(()=>{r.motionController=null,l&&r.remove(l),l=null,null==t||t()})),r}};
