"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");exports.__esModule=!0,exports.NodeServer=void 0,exports.createCloudEngine=createCloudEngine,exports.default=createNodeServerInstance;var _socket=require("../unity/socket"),_unity=require("../unity"),_meta=_interopRequireDefault(require("../core/meta")),_eventEmitter=_interopRequireWildcard(require("../common/event-emitter")),_util=require("../common/util"),_unity2=_interopRequireDefault(require("../event/unity")),_constant=require("../common/constant");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 parserParams(a,b){try{return"string"==typeof a?JSON.parse(a):a}catch(a){}return b||a}class NodeServer extends _eventEmitter.default{constructor(a,b){super(),this.socket=void 0,this.projects=[],this.socket=a,this.projects=b}start(a,b,c){let d=(0,_util.getElement)(a);if(!d)throw Error("from dt-engine: el is not found.");let e;"string"==typeof b?e={packageUrl:b,Width:d.offsetWidth||document.body.offsetWidth,Height:d.offsetHeight||document.body.offsetHeight}:(e={packageUrl:b.url,Width:b.width||d.offsetWidth||document.body.offsetWidth,Height:b.height||d.offsetHeight||document.body.offsetHeight},b.modelsFolderName&&(e.ModelsFolderName=b.modelsFolderName),b.minio&&(e.MinioServiceUrl=b.minio.serviceUrl,e.MinioUser=b.minio.user,e.MinioPassword=b.minio.password)),this.socket.send({RequestType:"SetupRequest",Params:e});let f=e.packageUrl;return new Promise((a,b)=>{const e=({RequestType:g,Token:h,Status:i,ErrorMessage:j,Params:k})=>{if("SetupRequest_Success"===g){k=parserParams(k);let g=parserParams(k.Result);if(g.packageUrl===f){let h=new _eventEmitter.DTEvent("success",{key:f,data:{ws:g.modelAPIServer,url:g.displayURL}});this.emit(h),this.socket.removeListener("message",e),createCloudEngine(a=>{a.mode="server",a.url=g.modelAPIServer,a.displayUrl=g.displayURL;let b=c?c(a):a;if(!b.displayUrl)throw Error("from dt-engine: display url can not be empty.");let e=document.createElement("iframe");return e.src=b.displayUrl,e.style.cssText="border: 0px; margin: 0px; padding: 0px; width: 100%; height: 100%; left: 0; top: 0; position: absolute;",e.frameBorder="no",d.appendChild(e),b}).then(b=>{b.amount(d),b.once(_constant.RenderEvent.Dispose,()=>{this.close(f)}),a(b)}).catch(b)}}else if("LoadProgress"===g){if(k=parserParams(k),k.packageUrl===f){let a=new _eventEmitter.DTEvent("progress",{key:f,data:{process:(+(100*(k.progress||0))).toFixed(2)}});this.emit(a)}}else if("SetupRequest_Failed"===g){let a=new _eventEmitter.DTEvent("failed",{data:k});this.emit(a),this.socket.removeListener("message",e),b(a)}};this.socket.addListener("message",e)})}close(a){this.socket.send(JSON.stringify({RequestType:"CloseClient",Params:{displayURL:a}}))}dispose(){this.socket&&this.socket.close()}}exports.NodeServer=NodeServer;async function createCloudEngine(a){let b=(0,_unity.createUnityDefaultConfig)(),c={...b,plugins:[]},d=await(0,_unity.unityCreator)(a?a(c):c),e=(0,_meta.default)(d);await e.render();let f=e.amount;return Object.defineProperty(e,"amount",{value:a=>{f.call(e,a),c.plugins.forEach(a=>{e.plugin.use(a)}),_unity2.default.Click({})(e),_unity2.default.Dblclick({})(e),_unity2.default.Hover({})(e)}})}function createNodeServerInstance(a){return new Promise((b,c)=>{let 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({message:"\u8FDE\u63A5\u5931\u8D25 \uFF01"}))},h=a=>{d.removeListener("message",i),b(new NodeServer(d,a))},i=a=>{if("error"===a.RequestType)return void g(a.Params);if("ProjectConfigUrl"===a.RequestType){let b=parserParams(a.Params,[]),c=b;h(c)}else if("GetInitCommunicatorData"===a.RequestType){let a={RequestType:"InitCommunicator",Params:"{\"groupId\":\"Web\"}"};d.send(JSON.stringify(a))}};d.addListener("message",i),d.connect()})}