"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");exports.__esModule=!0,exports.default=void 0;var _GLTFLoader=require("three/examples/jsm/loaders/GLTFLoader"),_DRACOLoader=require("three/examples/jsm/loaders/DRACOLoader"),_three=require("three"),_decrypt=_interopRequireDefault(require("./decrypt")),_cache=_interopRequireDefault(require("./cache")),_util=require("../common/util");class ModelLoader{constructor(a,b){this.loading={},this.$dracoLoader=void 0,this.queue=[],this.maxParallel=6,this.loadingParallel=0,this.store=void 0,this.decoderPath=void 0,this.decoderType=void 0,this.decrypt=void 0,this.expires=void 0,this.cacheEnabled=!0;let{decoderPath:c,cache:d,expires:e,decoderType:f="wasm"}=a;this.expires=e,this.decoderPath=c,this.decoderType=f,this.decrypt=new _decrypt.default(b),this.cacheEnabled=d,this.store=(0,_cache.default)({prefix:"loader",name:"dt-engine",storeName:"dt-engine-model",description:"tsl"})}dispose(){var a;this.loading={},null===(a=this.$dracoLoader)||void 0===a?void 0:a.dispose(),this.$dracoLoader=void 0,this.queue=[]}clear(a){if(a){let b=[];return(0,_util.toArray)(a).forEach(a=>{b.push(this.store.removeItem(a))}),Promise.all(b)}return this.store.clear()}get dracoLoader(){return this.$dracoLoader?this.$dracoLoader:(this.$dracoLoader=new _DRACOLoader.DRACOLoader().setDecoderPath(this.decoderPath),this.$dracoLoader.setDecoderConfig({type:this.decoderType}),this.$dracoLoader.preload(),this.$dracoLoader)}invokeLoaded(a,b,c){if(this.loading[a].forEach(a=>{a[c](b)}),delete this.loading[a],0<this.queue.length){const a=this.queue.splice(0,1);a[0]()}else this.loadingParallel--}loadModel(a,b){if(this.loading[a])return new Promise((b,c)=>{this.loading[a].push([b,c])});const c=b=>{const c=new _GLTFLoader.GLTFLoader().setDRACOLoader(this.dracoLoader);return c.parseAsync(b,a).then(c=>{this.invokeLoaded(a,c,0),this.store.setItem(a,b,this.expires)}).catch(b=>{this.invokeLoaded(a,b,1)})},d=()=>{this.cacheEnabled||e(),this.store.getItem(a).then(a=>{a?c(a):e()}).catch(e)},e=()=>{const d=new _three.FileLoader;d.setResponseType("arraybuffer"),d.load(a,b=>{this.decrypt.enable?this.decrypt.parser(new Uint8Array(b)).then(a=>{c(a)}).catch(b=>{this.invokeLoaded(a,b,1)}):c(b)},a=>{b&&b(100*(a.loaded/(a.total||1)))},b=>{this.invokeLoaded(a,b,1)})};return new Promise((b,c)=>{this.loading[a]=[[b,c]],this.loadingParallel>=this.maxParallel?this.queue.push(d):(this.loadingParallel++,d())})}loadMapping(a){return this.load(a).then(a=>JSON.parse(a)).catch(()=>[])}load(a,b,c){if(this.loading[a])return new Promise((b,c)=>{this.loading[a].push([b,c])});const d=()=>{const d=new _three.FileLoader;c&&d.setResponseType(c),d.load(a,b=>{this.invokeLoaded(a,b,0)},a=>{b&&b(100*(a.loaded/(a.total||1)))},b=>{this.invokeLoaded(a,b,1)})};return new Promise((b,c)=>{this.loading[a]=[[b,c]],this.loadingParallel>=this.maxParallel?this.queue.push(d):(this.loadingParallel++,d())})}}var _default=exports.default=ModelLoader;