"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");exports.__esModule=!0,exports.createUnityConnector=createUnityConnector,exports.createUnityDefaultConfig=createUnityDefaultConfig,exports.unityCreator=unityCreator;var _component=_interopRequireWildcard(require("../core/component")),_util=require("../common/util"),_scene=_interopRequireDefault(require("./scene")),_renderer=_interopRequireDefault(require("./renderer")),_camera=_interopRequireDefault(require("./camera")),_eventEmitter=_interopRequireWildcard(require("../common/event-emitter")),_model=_interopRequireDefault(require("./model")),_mapping=_interopRequireDefault(require("../core/mapping")),_socket=require("./socket"),_modelApi=require("./model-api"),_modelGroup=_interopRequireDefault(require("../core/model-group")),_constant=require("../common/constant"),_executer=require("../core/executer");function _getRequireWildcardCache(a){if("function"!=typeof WeakMap)return null;var b=new WeakMap,c=new WeakMap;return(_getRequireWildcardCache=function(a){return a?c:b})(a)}function _interopRequireWildcard(b,c){if(!c&&b&&b.__esModule)return b;if(null===b||"object"!=typeof b&&"function"!=typeof b)return{default:b};var d=_getRequireWildcardCache(c);if(d&&d.has(b))return d.get(b);var e={__proto__:null},f=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var a in b)if("default"!=a&&{}.hasOwnProperty.call(b,a)){var g=f?Object.getOwnPropertyDescriptor(b,a):null;g&&(g.get||g.set)?Object.defineProperty(e,a,g):e[a]=b[a]}return e.default=b,d&&d.set(b,e),e}function createUnityDefaultConfig(){return{mode:"client",displayUrl:"",plugins:[]}}function createUnityConnector(a){return new Promise((b,c)=>{const d=new _socket.UnitySocket(a);d.AllowEmpty=!0;let e=0;const f=a.repeatLimit||5,g=a=>{e+=1,e>f&&(d.close(),c(a))};d.addListener("error",g),d.once("loaded",async()=>{d.removeListener("error",g),b(d)}),d.connect()})}async function unityCreator(a){let b,c=await createUnityConnector(a);const d=new _eventEmitter.default,e=(0,_renderer.default)(),f=(0,_scene.default)(c),g=(0,_camera.default)(c),h=(0,_mapping.default)("unity"),i=(0,_model.default)(c);d.once(_constant.RenderEvent.Change,async()=>{await g.update()}),(0,_executer.startRequestAnimationFrame)();let j=[g.dispose,_executer.stopRequestAnimationFrame];const k=async()=>{const a=await c.invoke(_modelApi.REQUEST_TYPE.GetSceneMeta);try{const b=h("string"==typeof a?JSON.parse(a):a);return b}catch(a){throw Error("load mapping parse error")}};let l,m;const n=async(a,e=!1)=>{var f,g,h;let j;if(null!==(f=m)&&void 0!==f&&f.modelId&&((null===(g=m)||void 0===g?void 0:g.modelId)===a||(null===(h=m)||void 0===h?void 0:h.id)===a))a=m.modelId;else{var n;j=null===(n=m)||void 0===n?void 0:n.get(a,!1)}if(j&&j.modelId&&(a=j.modelId),b&&b.modelId===a)return Promise.resolve(b);d.emit(new _eventEmitter.DTEvent(_constant.RenderEvent.BeforeChange,{oldId:b&&b.modelId,currentId:a}));let o=!l;if(!l||e){l=await k();let a=await i([l]),b=(0,_component.default)(a,_modelGroup.default);m=await b(l)}return o&&d.emit(new _eventEmitter.DTEvent(_constant.RenderEvent.Change,{oldId:b&&b.modelId,currentId:a})),a?await c.invoke(_modelApi.REQUEST_TYPE.GoIntoSpace,{SpaceId:a,Duration:1}).then(()=>(b=m.modelId===a?m:m.get(a,!1),b)):(b=m,Promise.resolve(b))};let o=document.body,p=!1;let q=c.addNotificationListener(_modelApi.NOTIFICATION_TYPE.CurrentSpaceChanged,a=>{var c;let{OldSpaceId:e,NewSpaceId:f}=a,g=new _eventEmitter.DTEvent(_constant.RenderEvent.Change,{oldId:e,currentId:f});(null===(c=b)||void 0===c?void 0:c.modelId)!==f&&(m.modelId===f?b=m:b=m.get(f,!1)),d.emit(g)});j.push(q);return(0,_util.extend)({get type(){return"server"===a.mode?_constant.EngineType.UnityCloud:_constant.EngineType.UnityExe},get container(){return o},get unity(){return c},get component(){return b},get global(){return m},get model(){var a;return null===(a=b)||void 0===a?void 0:a.model},clear:async()=>{},renderer:e,scene:f,camera:g,render:n,amount:b=>{if(p)throw Error("\u4E0D\u53EF\u91CD\u590D\u6302\u8F7D.");p=!0;let c=(0,_util.getElement)(b);if(!c)throw Error("el \u4E0D\u5B58\u5728");if(o=c,"server"===a.mode){if(!a.displayUrl)throw Error("from dt-engine: display url can not be empty.");let b=document.createElement("iframe");b.src=a.displayUrl,b.style.cssText="border: 0px; margin: 0px; padding: 0px; width: 100%; height: 100%; left: 0; top: 0; position: absolute;",b.frameBorder="no",c.appendChild(b)}d.emit(_constant.RenderEvent.Mounted)},dispose:()=>{var e;d.emit(_constant.RenderEvent.Dispose),j.forEach(a=>{a()}),j=[],null===(e=b)||void 0===e?void 0:e.dispose(),c.close(),"server"===a.mode&&o!==document.body&&(o.innerHTML=""),d.removeAllListener()},reset:async()=>{await c.invoke(_modelApi.REQUEST_TYPE.ResetScene),await n(void 0,!0)},createComponent:async()=>{}},d)}