/* Socket Lib - Built with esbuild */
var w=Object.defineProperty;var j=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var R=Object.prototype.hasOwnProperty;var W=(o,a)=>{for(var r in a)w(o,r,{get:a[r],enumerable:!0})},$=(o,a,r,l)=>{if(a&&typeof a=="object"||typeof a=="function")for(let c of I(a))!R.call(o,c)&&c!==r&&w(o,c,{get:()=>a[c],enumerable:!(l=j(a,c))||l.enumerable});return o};var D=o=>$(w({},"__esModule",{value:!0}),o);var M={};W(M,{getEditablePackageJsonClass:()=>P,pkgJsonToEditable:()=>O,toEditablePackageJson:()=>T,toEditablePackageJsonSync:()=>B});module.exports=D(M);var y=require("../path"),k=require("./normalize"),E=require("./paths");const F=Symbol.for("indent"),z=Symbol.for("newline");let f,J;function b(){return J===void 0&&(J=require("node:fs")),J}let _;function N(){return _===void 0&&(_=require("node:path")),_}let S;function v(){return S===void 0&&(S=require("node:util")),S}function P(){if(f===void 0){const o=require("../external/@npmcli/package-json"),{parse:a,read:r}=require("../external/@npmcli/package-json/lib/read-package"),{packageSort:l}=require("../external/@npmcli/package-json/lib/sort");f=class extends o{static fixSteps=o.fixSteps;static normalizeSteps=o.normalizeSteps;static prepareSteps=o.prepareSteps;_canSave=!0;_path=void 0;_readFileContent="";_readFileJson=void 0;get content(){return super.content}get filename(){const e=this._path;return e?e.endsWith("package.json")?e:N().join(e,"package.json"):""}static async create(e,t={}){const n=new f;return await n.create(e),t.data?n.update(t.data):n}static async fix(e,t){const n=new f;return await n.load(e,!0),await n.fix(t)}static async load(e,t={}){const n=new f;if(!t.create)return await n.load(e);try{return await n.load(e)}catch(i){if(!i.message.startsWith("Could not read package.json"))throw i;return n.create(e)}}static async normalize(e,t){const n=new f;return await n.load(e),await n.normalize(t)}static async prepare(e,t){const n=new f;return await n.load(e,!0),await n.prepare(t)}create(e){return super.create(e),this._path=e,this}async fix(e={}){return await super.fix(e),this}fromContent(e){return super.fromContent(e),this._canSave=!1,this}fromJSON(e){return super.fromJSON(e),this}async load(e,t){this._path=e;const{promises:n}=b();let i;try{this._readFileContent=await r(this.filename)}catch(s){if(!t)throw s;i=s}if(i){const u=N().resolve(this.path||"","index.js");let d;try{d=await n.readFile(u,"utf8")}catch{throw i}try{this.fromContent(d)}catch{throw i}return this._canSave=!1,this}return this.fromJSON(this._readFileContent),this._readFileJson=a(this._readFileContent),this}async normalize(e={}){return await super.normalize(e),this}get path(){return this._path}async prepare(e={}){return await super.prepare(e),this}async save(e){if(!this._canSave||this.content===void 0)throw new Error("No package.json to save to");const{ignoreWhitespace:t=!1,sort:n=!1}={__proto__:null,...e},{[F]:i,[z]:s,...u}=this.content,d=n?l(u):u,{[F]:g,[z]:h,...p}=this._readFileJson||{};if(t&&v().isDeepStrictEqual(d,p))return!1;const C=i??"  ",x=s??`
`,m=`${JSON.stringify(d,void 0,C)}
`.replace(/\n/g,x);if(!t&&m.trim()===this._readFileContent.trim())return!1;const{promises:q}=b();return await q.writeFile(this.filename,m),this._readFileContent=m,this._readFileJson=a(m),!0}saveSync(e){if(!this._canSave||this.content===void 0)throw new Error("No package.json to save to");const{ignoreWhitespace:t=!1,sort:n=!1}={__proto__:null,...e},{[Symbol.for("indent")]:i,[Symbol.for("newline")]:s,...u}=this.content,d=n?l(u):u;if(t&&v().isDeepStrictEqual(d,this._readFileJson))return!1;const g=i??"  ",h=s??`
`,p=`${JSON.stringify(d,void 0,g)}
`.replace(/\n/g,h);return!t&&p.trim()===this._readFileContent.trim()?!1:(b().writeFileSync(this.filename,p),this._readFileContent=p,this._readFileJson=a(p),!0)}update(e){return super.update(e),this}willSave(e){const{ignoreWhitespace:t=!1,sort:n=!1}={__proto__:null,...e};if(!this._canSave||this.content===void 0)return!1;const{[Symbol.for("indent")]:i,[Symbol.for("newline")]:s,...u}=this.content,d=n?l(u):u;if(t&&v().isDeepStrictEqual(d,this._readFileJson))return!1;const g=i??"  ",h=s??`
`,p=`${JSON.stringify(d,void 0,g)}
`.replace(/\n/g,h);return!(!t&&p.trim()===this._readFileContent.trim())}}}return f}function O(o,a){const{normalize:r,...l}={__proto__:null,...a},c=P();return new c().fromContent(r?(0,k.normalizePackageJson)(o,l):o)}async function T(o,a){const{path:r,...l}={__proto__:null,...a},{normalize:c,...e}=l;if(typeof r!="string")return O(o,l);const t=P(),n=(0,E.resolvePackageJsonDirname)(r);return(await t.load(n,{create:!0})).fromJSON(`${JSON.stringify(c?(0,k.normalizePackageJson)(o,{...(0,y.isNodeModules)(n)?{}:{preserve:["repository"]},...e}):o,null,2)}
`)}function B(o,a){const{path:r,...l}={__proto__:null,...a},{normalize:c,...e}=l;if(typeof r!="string")return O(o,l);const t=P(),n=(0,E.resolvePackageJsonDirname)(r);return new t().create(n).fromJSON(`${JSON.stringify(c?(0,k.normalizePackageJson)(o,{...(0,y.isNodeModules)(n)?{}:{preserve:["repository"]},...e}):o,null,2)}
`)}0&&(module.exports={getEditablePackageJsonClass,pkgJsonToEditable,toEditablePackageJson,toEditablePackageJsonSync});
//# sourceMappingURL=editable.js.map
