// src/lib/initCwd.ts
function INIT_CWD() {
  return process.env.INIT_CWD ?? process.cwd();
}

// src/lib/generateReadmeFiles.ts
import { execFile } from "child_process";
import FS, { readFileSync } from "fs";
import {
  mkdir,
  readFile,
  writeFile
} from "fs/promises";
import { createRequire } from "module";
import PATH from "path";
import { createInterface } from "readline";
import { promisify } from "util";
import chalk from "chalk";
var execFileAsync = promisify(execFile);
var require2 = createRequire(import.meta.url);
var packageRoot = PATH.dirname(require2.resolve("@xylabs/toolchain/package.json"));
var readmeTemplatesDir = PATH.resolve(packageRoot, "templates", "readme");
var DEFAULT_README_TEMPLATE = readFileSync(PATH.resolve(readmeTemplatesDir, "README.template.md"), "utf8");
var DEFAULT_README_BODY = readFileSync(PATH.resolve(readmeTemplatesDir, "README.body.md"), "utf8");
function applyLogoConfig(template, logoUrl, logoLinkUrl) {
  let result = template;
  if (logoUrl) {
    result = result.replace(/\[logo]: .+/, `[logo]: ${logoUrl}`);
    if (logoLinkUrl) {
      result = result.replace(/\[!\[logo]\[]][^)]*\)/, `[![logo][]](${logoLinkUrl})`);
    }
  } else {
    result = result.replace(/\[!\[logo]\[]][^\n]*\n*/, "");
    result = result.replace(/\[logo]: [^\n]*\n?/, "");
  }
  return result;
}
function resolveTemplatePath(templatePath) {
  const cwd = INIT_CWD();
  return templatePath ?? PATH.join(cwd, ".xy", "README.template.md");
}
async function scaffoldTemplate(resolvedTemplatePath, template) {
  const xyDir = PATH.dirname(resolvedTemplatePath);
  await mkdir(xyDir, { recursive: true });
  await writeFile(resolvedTemplatePath, template);
  console.log(chalk.green(`Created template: ${resolvedTemplatePath}`));
  const bodyPath = PATH.join(xyDir, "README.body.md");
  await writeFile(bodyPath, DEFAULT_README_BODY);
  console.log(chalk.green(`Created body template: ${bodyPath}`));
}

// src/lib/loadConfig.ts
import chalk2 from "chalk";
import { cosmiconfig } from "cosmiconfig";
import { TypeScriptLoader } from "cosmiconfig-typescript-loader";
import deepmerge from "deepmerge";
var config;
var rootConfigPath;
function createExplorer() {
  return cosmiconfig("xy", { cache: true, loaders: { ".ts": TypeScriptLoader() } });
}
var loadConfig = async (params) => {
  if (config === void 0) {
    const cosmicConfigResult = await createExplorer().search();
    config = cosmicConfigResult?.config ?? {};
    rootConfigPath = cosmicConfigResult?.filepath;
    const configFilePath = cosmicConfigResult?.filepath;
    if (configFilePath !== void 0) {
      console.log(chalk2.green(`Loaded config from ${configFilePath}`));
      if (config.verbose) {
        console.log(chalk2.gray(`${JSON.stringify(config, null, 2)}`));
      }
    }
  }
  return deepmerge(config, params ?? {});
};

// src/actions/readme-init.ts
async function readmeInit({ templatePath }) {
  const config2 = await loadConfig();
  const template = applyLogoConfig(DEFAULT_README_TEMPLATE, config2.readme?.logoUrl, config2.readme?.logoLinkUrl);
  const resolvedTemplatePath = resolveTemplatePath(templatePath);
  await scaffoldTemplate(resolvedTemplatePath, template);
  return 0;
}
export {
  readmeInit
};
//# sourceMappingURL=readme-init.mjs.map