#!/usr/bin/env node

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

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