(()=>{const e=require("pfs"),r=require("StanzServer"),a=require("os"),{stanz:t}=require("stanz"),s=require("url"),o=require("querystring"),c=()=>"$"+Math.random().toString(36).substr(2);let n="";const i=a.networkInterfaces();Object.values(i).forEach(e=>{e.forEach(function(e){"IPv4"==e.family&&/^192\./.test(e.address)&&(n=e.address)})});const l=(e,{sourceServerName:r})=>{e.runAnime=0,e.runOutAnime=0;let a=e.picUrl,t=e["background-image"];a&&(e.picUrl=a.replace("/$",`/${r}`)),t&&(e["background-image"]=t.replace("/$",`/${r}`))},u=(e,{sourceServerName:r})=>{delete e.runAnime,delete e.runOutAnime;let a=e.picUrl,t=e["background-image"];a&&(e.picUrl=a.replace(`/${r}`,"/$")),t&&(e["background-image"]=t.replace(`/${r}`,"/$"))};window.initPageServer=(async({projectDir:a})=>{let i=c(),p=c(),m=await e.readFile(a+"/page.json"),d=(m=t(JSON.parse(m.toString()))).string;m.mainPage.forEach(e=>{l(e,{sourceServerName:i}),e.forEach(e=>{l(e,{sourceServerName:i})})});let g=await e.readFile(a+"/source.json"),h=(g=t(JSON.parse(g.toString()))).string;g.pics.forEach(e=>{e.path=e.path.replace("/$",`/${i}`)});let v=new r;v.commonData={projectDir:a,project:m,sourceData:g,splits:[],splitAddress:`http://${n}:${listen}/pageCreator/split.html`,animationData:"",sourceUpload:p,sourceServerName:i,pptData:{isRunPreview:!1,cid:0,count:1}};const f=async()=>{let r=v.commonData.project.clone();r.mainPage.forEach(e=>{u(e,{sourceServerName:i}),delete e.thumbnail,e.forEach(e=>{u(e,{sourceServerName:i})})});let t="",s=r.string;s!==d&&(d=s,await e.writeFile(a+"/page.json",s,"utf-8"),t+="project-save");let o=v.commonData.sourceData.clone();o.pics.forEach(e=>{e.path=e.path.replace(`/${i}`,"/$")});let c=o.string;return c!==h&&(h=c,e.writeFile(a+"/source.json",c,"utf-8"),t+=" source-save"),console.log("hasChange? => ",t||"no"),t&&v.send({type:"cmd",data:{stat:"saveok"}}),t};let S,w=e=>{clearTimeout(S),S=setTimeout(()=>{f()},2500)};v.commonData.watch("project",w),v.commonData.watch("sourceData",w);let D=0;v.canAccess=(e=>{let r=s.parse(e.url),a=o.parse(r.query);if(D){switch(a.kid){case"after":return{type:"onlypull",filter:["sourceUpload"],allow:["splits"]};case"debug":return!0}return!1}if("start"===a.kid)return D=1,!0}),v.onmsg=((e,r)=>{switch(e){case"startInit":var{request:a,tarObj:t}=r;let n=s.parse(a.url),i=o.parse(n.query);if(i){let{aid:e}=i;t.aid=e}break;case"close":var{event:c,tarObj:t}=r;let l=v.commonData.splits.findIndex(e=>e.aid===t.aid);l>-1&&v.commonData.splits.splice(l,1)}});let $=Math.round(1e4+55534*Math.random());return v.listen($),{port:$,saveData:f,pServer:v,sourceServerName:i,sourceUploadName:p}})})();