var x=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var C=(o,e)=>{for(var t in e)x(o,t,{get:e[t],enumerable:!0})},_=(o,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of T(e))!j.call(o,n)&&n!==t&&x(o,n,{get:()=>e[n],enumerable:!(r=S(e,n))||r.enumerable});return o};var R=o=>_(x({},"__esModule",{value:!0}),o);var U=(o,e,t)=>new Promise((r,n)=>{var f=c=>{try{s(t.next(c))}catch(u){n(u)}},a=c=>{try{s(t.throw(c))}catch(u){n(u)}},s=c=>c.done?r(c.value):Promise.resolve(c.value).then(f,a);s((t=t.apply(o,e)).next())});var v={};C(v,{ad4mConnect:()=>P});module.exports=R(v);var{ipcMain:w,BrowserWindow:A}=require("electron"),i=require("path"),l=require("fs"),{homedir:N}=require("os"),h=i.join(N(),".ad4m"),b="capability-token",F="executor-url";function k(o){l.existsSync(o)===!1&&l.mkdirSync(o,777)}function q(o,e){k(e),console.log("url in writeFile",o),l.writeFileSync(i.join(e,F),o)}function I(o){let e;try{e=l.readFileSync(i.join(o,F),{encoding:"utf8",flag:"r"}),console.log("Found executor URL in config file:",e)}catch(t){try{let r=i.join(h,"executor-port");console.log("No executor URL config file found. Trying to read local ad4m executor port from file:",r);let n=l.readFileSync(r,{flag:"r"}).toString();console.log("Found port:",n),e=`ws://localhost:${n}/graphql`,console.log("Using executor URL:",e)}catch(r){console.error(r),console.log("Couldn't read executor port from file. User has to enter URL manually"),e=""}}return e}function O(o,e){k(e),l.writeFileSync(i.join(e,b),o)}function D(o){try{let e=l.readFileSync(i.join(o,b),{encoding:"utf8",flag:"r"});return console.log("Found capability token in config file."),e}catch(e){return console.log("No capability token found."),""}}function P(o){let{appName:e,appDesc:t,appDomain:r,appIconPath:n,capabilities:f,dataPath:a=h}=o;return new Promise((s,c)=>U(this,null,function*(){let u=I(a),E=D(a),p;w.on("get",(g,d)=>{g.returnValue={appName:e,appDesc:t,appIconPath:n,appDomain:r,executorUrl:u,capabilityToken:E,capabilities:f}}),w.on("resolve",(g,d)=>{let{executorUrl:m,capabilityToken:y,client:L}=d;console.log({executorUrl:m,capabilityToken:y}),q(m,a),O(y,a),p.close(),s({executorUrl:m,capabilityToken:y,client:L})}),w.on("reject",(g,d)=>{p.close(),c()}),p=new A({width:1200,height:800,webPreferences:{nodeIntegration:!0,contextIsolation:!1,webSecurity:!1},minimizable:!0,alwaysOnTop:!1,frame:!0,transparent:!1,icon:i.join(__dirname,"../public","Ad4mLogo.png")}),p.loadURL(`file://${__dirname}/../public/dialog.html`)}))}0&&(module.exports={ad4mConnect});
