#!/usr/bin/env node

/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *--------------------------------------------------------------------------------------------*/

import V from"node:module";import{spawn as K}from"node:child_process";import{readFileSync as X,rmSync as w}from"node:fs";import{basename as A,dirname as Y,join as p,resolve as G,sep as $}from"node:path";import*as j from"node:sea";import{fileURLToPath as y,pathToFileURL as P}from"node:url";var k=new Set;function v(r,i){let t=Object.create(null);if(r)for(let[o,e]of Object.entries(r))t[o]=e;let n=i??k;return new Proxy(process.env,{get(o,e){if(typeof e=="string"){if(Object.hasOwn(t,e))return t[e];if(!n.has(e))return process.env[e]}},set(o,e,s){return t[e]=s,!0},has(o,e){return typeof e!="string"?!1:Object.hasOwn(t,e)?t[e]!==void 0:n.has(e)?!1:e in process.env},ownKeys(o){let e=new Set(Object.keys(process.env));for(let s of n)e.delete(s);for(let s of Object.keys(t))t[s]!==void 0?e.add(s):e.delete(s);return[...e]},getOwnPropertyDescriptor(o,e){if(typeof e!="string")return;let s;if(Object.hasOwn(t,e))s=t[e];else{if(n.has(e))return;s=process.env[e]}if(s!==void 0)return{value:s,writable:!0,enumerable:!0,configurable:!0}},deleteProperty(o,e){return t[e]=void 0,!0}})}import{readdir as F,access as Q,constants as W}from"node:fs/promises";import{join as c,basename as T}from"node:path";import{homedir as d}from"node:os";var b=process.env.XDG_CACHE_HOME||c(d(),".cache");function E(){if(process.argv.includes("--no-auto-update"))return!1;let r=process.env.COPILOT_AUTO_UPDATE;return!(r&&r.toLowerCase()==="false")}function J(){if(process.platform==="darwin")return c(d(),"Library","Caches","copilot");if(process.platform==="win32"){let r=process.env.LOCALAPPDATA||c(d(),".cache");return c(r,"copilot")}return c(b,"copilot")}function x(){let r=[];return process.env.COPILOT_CACHE_HOME&&r.push(c(process.env.COPILOT_CACHE_HOME,"pkg")),r.push(c(J(),"pkg")),r.push(c(b,"copilot","pkg")),process.env.COPILOT_HOME&&r.push(c(process.env.COPILOT_HOME,"pkg")),r.push(c(d(),".copilot","pkg")),[...new Set(r)]}function _(r){let i=r.match(/^(\d+)\.(\d+)\.(\d+)/);if(i)return[Number(i[1]),Number(i[2]),Number(i[3])]}function B(r,i){let t=_(r),n=_(i);if(!t&&!n)return 0;if(!t)return-1;if(!n)return 1;for(let s=0;s<3;s++)if(t[s]!==n[s])return t[s]-n[s];let o=r.includes("-"),e=i.includes("-");return o!==e?o?-1:1:r.localeCompare(i)}async function C(r,...i){let t=[];for(let n of i){let o;try{o=await F(n)}catch{continue}for(let e of o){let s=c(n,e);try{await Q(c(s,r),W.R_OK),t.push(s)}catch{continue}}}return t.sort((n,o)=>{let e=B(T(o),T(n));return e!==0?e:n.localeCompare(o)}),t}try{V.enableCompileCache?.()}catch{}var q=75;function z(){if(process.stdout.isTTY)try{process.stdout.write("\x1B[<u\x1B[?1049l\x1B[?1006l\x1B[?1003l\x1B[?1002l\x1B[?2004l\x1B[?1004l\x1B[?25h\x1B[?2026l")}catch{}}var N=Y(y(import.meta.url)),H=j.isSea();process.report.reportOnFatalError=!0;process.report.excludeEnv=!0;var l=process.argv.find(r=>A(r).startsWith("conpty_console_list_agent")),R=process.argv.find(r=>A(r)==="extension_bootstrap.mjs"),L=R?G(R):void 0,U=L?.startsWith(G(N,"preloads")+$)?L:void 0;if(l){let r=l.endsWith(".js")?l:l+".js";await import(P(r).href)}else if(U)await import(P(U).href);else if(process.env.COPILOT_SHUTDOWN_FLUSH){try{let{url:r,headers:i,body:t}=JSON.parse(process.env.COPILOT_SHUTDOWN_FLUSH);await fetch(r,{method:"POST",headers:i,body:t,signal:AbortSignal.timeout(3e4)})}catch{}process.exit(0)}else if(process.env.COPILOT_RUN_APP==="1"){let r=p(N,"app.js");if(H&&E()){let i=x().map(n=>p(n,"universal")),t=await C("app.js",...i);t.length>0&&(r=p(t[0],"app.js"))}await import(P(r).href)}else{let i=function(){let e=process.env.COPILOT_HOME||p(process.env.HOME||process.env.USERPROFILE||"",".copilot");return p(e,"restart",`${r}.json`)},t=function(){let e=i();try{let s=X(e,"utf-8");return w(e,{force:!0}),JSON.parse(s)}catch{return}},n=function(){try{w(i(),{force:!0})}catch{}},o=function(e,s){let D=H?e:[...process.execArgv,y(import.meta.url),...e],g=K(process.execPath,D,{stdio:"inherit",cwd:s,env:v({COPILOT_RUN_APP:"1",COPILOT_LOADER_PID:r})}),f=a=>{try{g.kill(a)}catch{}},m=()=>f("SIGINT"),O=()=>f("SIGTERM"),h=()=>f("SIGHUP"),I=()=>f("SIGQUIT");process.on("SIGINT",m),process.on("SIGTERM",O),process.on("SIGHUP",h),process.on("SIGQUIT",I),g.on("error",a=>{process.off("SIGINT",m),process.off("SIGTERM",O),process.off("SIGHUP",h),process.off("SIGQUIT",I),n(),process.stderr.write(`Failed to start: ${a.message}
`),process.exit(1)}),g.on("exit",(a,S)=>{if(process.off("SIGINT",m),process.off("SIGTERM",O),process.off("SIGHUP",h),process.off("SIGQUIT",I),a===q){let u=t();if(u){let M=[...u.argv,"--resume",u.sessionId];o(M,u.cwd||s)}else o(e,s);return}n(),S?(z(),process.kill(process.pid,S)):a!==0?process.exit(a??1):process.exit(0)})};Z=i,ee=t,re=n,te=o;let r=String(process.pid);o(process.argv.slice(2),process.cwd())}var Z,ee,re,te;
