import { loadConfig as loadConfig$1, resolveConfig, transform } from "./node_modules/.pnpm/automd@0.4.3_patch_hash_5b5d5334735dce5e1ae79d8924a5aa49e4139453056613d618786b961793a42b_magicast@0.5.2/node_modules/automd/dist/index.mjs";
import { toArray } from "@stryke/convert/to-array";
import { isDirectory } from "@stryke/fs/is-file";
import { listFiles } from "@stryke/fs/list-files";
import { appendPath } from "@stryke/path/append";
import { isAbsolutePath } from "@stryke/path/is-type";
import { joinPaths } from "@stryke/path/join-paths";
import { replacePath } from "@stryke/path/replace";
import defu from "defu";
import { loadConfig } from "c12";
import toc from "markdown-toc";

//#region src/index.ts
/**
* AutoMD Plugin
*
* @remarks
* A Powerlines plugin to use the AutoMD markdown transformer during the prepare task.
*
* @see https://automd.unjs.io/
*
* @param options - The plugin options.
* @returns A Powerlines plugin instance.
*/
const plugin = (options = {}) => {
	return {
		name: "automd",
		async config() {
			const config = await loadConfig$1(joinPaths(this.workspaceConfig.workspaceRoot, this.config.root), options);
			if (!config.prefix || !Array.isArray(config.prefix)) config.prefix = toArray(config.prefix ?? []);
			if (!config.prefix.includes("automd")) config.prefix.push("automd");
			if (!config.prefix.includes("powerlines")) config.prefix.push("powerlines");
			return { automd: defu(config ?? {}, {
				configFile: options.configFile,
				allowIssues: true,
				dir: this.config.root,
				watch: false,
				input: "README.md",
				toc: {
					maxDepth: 6,
					bullets: "-"
				}
			}) };
		},
		async configResolved() {
			if (this.config.framework && !toArray(this.config.automd.prefix).includes(this.config.framework)) this.config.automd.prefix = toArray(this.config.automd.prefix).concat(this.config.framework);
			if (this.config.automd.configFile) {
				const { config } = await loadConfig({
					cwd: this.config.automd.dir,
					configFile: this.config.automd.configFile,
					defaults: {
						ignore: [
							"**/node_modules",
							"**/dist",
							"**/.*"
						],
						dir: this.config.automd.dir
					}
				});
				this.config.automd = resolveConfig(defu(this.config.automd, {
					...config,
					prefix: toArray(config.prefix ?? [])
				}));
			}
			this.config.automd.input = (await Promise.all(toArray(this.config.automd.input).map(async (input) => {
				if (input.includes("*")) return listFiles(isAbsolutePath(input) ? input : appendPath(input, this.config.root), { ignore: this.config.automd.ignore });
				return isAbsolutePath(input) ? input : appendPath(input, this.config.root);
			}))).flat();
			if (this.config.automd.output && !isAbsolutePath(this.config.automd.output)) this.config.automd.output = appendPath(this.config.automd.output, this.config.root);
			this.config.automd.generators ??= {};
			if (this.config.automd.toc !== false) this.config.automd.generators.toc ??= {
				name: "toc",
				generate: (ctx) => {
					const opts = this.config.automd.toc ?? {};
					return { contents: toc(ctx.block.contents, {
						...opts,
						maxdepth: opts.maxDepth,
						first1: opts.firstH1
					}).content };
				}
			};
		},
		async docs() {
			await Promise.all(toArray(this.config.automd.input).map(async (input) => {
				const contents = await this.fs.read(input);
				if (contents) {
					const result = await transform(contents, this.config.automd);
					if (result.hasIssues && this.config.automd.allowIssues === false) throw new Error(`AutoMD found issues in file "${input}". Please resolve the issues or set \`allowIssues\` to true in the plugin configuration to ignore them.`);
					if (result.hasChanged) await this.fs.write(appendPath(this.config.automd.output ? isDirectory(this.config.automd.output) ? replacePath(input, this.config.automd.output) : this.config.automd.output : input, this.config.root), result.contents);
				}
			}));
		}
	};
};
var src_default = plugin;

//#endregion
export { src_default as default, plugin };
//# sourceMappingURL=index.mjs.map