"use strict";var e=require("yargs"),t=require("yargs/helpers"),o=require("@repodog/cli-utils"),r=require("ansi-colors"),s=require("glob"),i=require("lodash-es"),n=require("node:fs"),c=require("node:path"),p=require("shelljs");const u=e=>e.startsWith("./")?e.replace(/^\.\//,""):e,l=({input:e,output:t,verbose:l=!1})=>{o.setVerbose(l,"cts-types"),o.verboseLog(">>>> USER CONFIG START <<<<"),o.verboseLog(`input: ${e}`),o.verboseLog(`output: ${t??"None"}`),o.verboseLog(">>>> USER CONFIG END <<<<\n"),o.log(`Building cts types for: ${e}`);try{const r=u(e);o.verboseLog(`standardisedInput: ${r}`);const i=s.glob.sync([`${r}/**/*.d.ts`,`${r}/**/*.d.ts.map`]);if(o.verboseLog(`Files to convert:${i.length>0?`\n${i.join("\n")}`:" None"}\n`),0===i.length)throw new Error("No files to convert. Please check the input is correct.");for(const e of i){o.verboseLog(`Converting: ${e}`);const{dir:s,ext:i,name:p}=c.parse(e),l=".ts"===i?`${s}/${p}.cts`:`${s}/${p.replace(/\.ts$/,".cts")}.map`;o.verboseLog(`renamedFile: ${l}`);const d=t?l.replace(r,u(t)):l;o.verboseLog(`outputFile: ${d}`);const a=n.readFileSync(e,{encoding:"utf8"}).replaceAll(".ts",".cts");o.verboseLog(`Outputing ${e} to: ${d}\n`);const g=c.resolve(process.cwd(),c.dirname(d));n.existsSync(g)||n.mkdirSync(g,{recursive:!0}),n.writeFileSync(d,a)}return o.log(`Finished building cts types for: ${e}`),p.exit(0)}catch(e){const t=i.isError(e)?e:new Error("An unexpected error occured.");return p.echo(`${r.magenta("cts-types")} ${r.dim("=>")} ${r.red(`Error: ${t.message}`)}`),p.exit(1)}};exports.cli=()=>{e(t.hideBin(process.argv)).command("build