import{UnitySocket}from"../unity/socket";import{createUnityDefaultConfig,unityCreator}from"../unity";import createMetaMethods from"../core/meta";import EventEmitter,{DTEvent}from"../common/event-emitter";import{getElement}from"../common/util";import UnityEvent from"../event/unity";import{RenderEvent}from"../common/constant";function parserParams(a,b){try{return"string"==typeof a?JSON.parse(a):a}catch(a){}return b||a}export class NodeServer extends EventEmitter{constructor(a,b){super(),this.socket=void 0,this.projects=[],this.socket=a,this.projects=b}start(a,b,c){let d=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 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(RenderEvent.Dispose,()=>{this.close(f)}),a(b)}).catch(b)}}else if("LoadProgress"===g){if(k=parserParams(k),k.packageUrl===f){let a=new DTEvent("progress",{key:f,data:{process:(+(100*(k.progress||0))).toFixed(2)}});this.emit(a)}}else if("SetupRequest_Failed"===g){let a=new 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()}}export async function createCloudEngine(a){let b=createUnityDefaultConfig(),c={...b,plugins:[]},d=await unityCreator(a?a(c):c),e=createMetaMethods(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)}),UnityEvent.Click({})(e),UnityEvent.Dblclick({})(e),UnityEvent.Hover({})(e)}})}export default function createNodeServerInstance(a){return new Promise((b,c)=>{let d=new 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()})}