import{c as e,n as t}from"./errors-pXKbCKbL.js";import{n,t as r}from"./editor-BmJI32_P.js";import{n as i,s as a}from"./fsStore-BzLA1cBc.js";import{t as o}from"./base-xqced3zT.js";import{p as s}from"./config-Bphz_1Sc.js";import{t as c}from"./template-Cpbt9qy8.js";import{execSync as l}from"node:child_process";import*as u from"node:path";import*as d from"node:fs";var f=class extends o{toolId=`xling`;resolvePath(t){if(!this.validateScope(t))throw new e(t);return`~/.claude/xling.json`}validateScope(e){return e===`user`}readConfig(e){let n=a(e);if(!d.existsSync(n))return c;try{let e=d.readFileSync(n,`utf-8`);return s(JSON.parse(e))}catch(e){throw new t(n,e.message)}}writeConfig(e,t,n=!0){let r=a(e);if(i(u.dirname(r)),n&&d.existsSync(r)){let e=`${r}.bak`;d.copyFileSync(r,e)}try{let e=`${r}.tmp`;d.writeFileSync(e,JSON.stringify(t,null,2),`utf-8`),this.#e(e),d.renameSync(e,r)}catch(e){throw Error(`Failed to write config to ${r}: ${e.message}`)}}#e(e){try{process.platform===`win32`?l(`icacls "${e}" /inheritance:r /grant:r "%USERNAME%:(F)"`,{stdio:`ignore`,windowsHide:!0}):d.chmodSync(e,384)}catch(t){console.warn(`Warning: Could not set secure permissions on ${e}: ${t.message}`)}}async edit(t,o){if(!this.validateScope(t))throw new e(t);let s=this.resolvePath(t),l=a(s),f=n(o.ide);return i(u.dirname(l)),d.existsSync(l)||this.writeConfig(s,c,!1),await r(f,l),{success:!0,message:`Opened ${s} in ${f}`,filePath:l}}async list(t){if(!this.validateScope(t))throw new e(t);let n=this.resolvePath(t),r=this.readConfig(n);return{type:`entries`,entries:{prompt:{defaultModel:r.prompt.defaultModel||null,retryPolicy:r.prompt.retryPolicy||null,providers:r.prompt.providers.map(e=>({name:e.name,baseUrl:e.baseUrl,models:e.models,priority:e.priority??null,timeout:e.timeout??null,apiKey:this.#t(e.apiKey)}))},shortcuts:r.shortcuts||null},filePath:a(n)}}getShortcuts(t){if(!this.validateScope(t))throw new e(t);let n=this.resolvePath(t);return this.readConfig(n).shortcuts||{}}async addProvider(e,t){let n=this.resolvePath(e),r=this.readConfig(n);return r.prompt.providers.some(e=>e.name===t.name)?{success:!1,message:`Provider "${t.name}" already exists`,filePath:a(n)}:(r.prompt.providers.push(t),this.writeConfig(n,r),{success:!0,message:`Added provider "${t.name}"`,filePath:a(n)})}async removeProvider(e,t){let n=this.resolvePath(e),r=this.readConfig(n),i=r.prompt.providers.findIndex(e=>e.name===t);return i===-1?{success:!1,message:`Provider "${t}" not found`,filePath:a(n)}:r.prompt.providers.length===1?{success:!1,message:`Cannot remove the last provider. At least one provider is required.`,filePath:a(n)}:(r.prompt.providers.splice(i,1),this.writeConfig(n,r),{success:!0,message:`Removed provider "${t}"`,filePath:a(n)})}async updateProvider(e,t,n){let r=this.resolvePath(e),i=this.readConfig(r),o=i.prompt.providers.find(e=>e.name===t);return o?(Object.assign(o,{...n,name:t}),this.writeConfig(r,i),{success:!0,message:`Updated provider "${t}"`,filePath:a(r)}):{success:!1,message:`Provider "${t}" not found`,filePath:a(r)}}async setDefaultModel(e,t){let n=this.resolvePath(e),r=this.readConfig(n);return r.prompt.defaultModel=t,this.writeConfig(n,r),{success:!0,message:`Set default model to "${t}"`,filePath:a(n)}}#t(e){return e.length<=8?`***`:`${e.substring(0,4)}...${e.substring(e.length-4)}`}checkPermissions(e){let t=a(e);if(!d.existsSync(t))return{secure:!0,permissions:`N/A`};try{let e=d.statSync(t).mode&511,n=e.toString(8);return{secure:e===384,permissions:n}}catch{return{secure:!1,permissions:`unknown`}}}};export{f as t};