Object.defineProperty(exports, '__esModule', { value: true });
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
const require_index = require('./node_modules/.pnpm/automd@0.4.3_patch_hash_0fe864a2d6fa1461e144747e5f0fbc0a5fb51435ec6b3217ffb18ff2d7058778_magicast@0.5.1/node_modules/automd/dist/index.cjs');
let __stryke_convert_to_array = require("@stryke/convert/to-array");
let __stryke_fs_is_file = require("@stryke/fs/is-file");
let __stryke_fs_list_files = require("@stryke/fs/list-files");
let __stryke_path_append = require("@stryke/path/append");
let __stryke_path_is_type = require("@stryke/path/is-type");
let __stryke_path_join_paths = require("@stryke/path/join-paths");
let __stryke_path_replace = require("@stryke/path/replace");
let defu = require("defu");
defu = require_rolldown_runtime.__toESM(defu);
let c12 = require("c12");
let markdown_toc = require("markdown-toc");
markdown_toc = require_rolldown_runtime.__toESM(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 require_index.loadConfig((0, __stryke_path_join_paths.joinPaths)(this.workspaceConfig.workspaceRoot, this.config.projectRoot), options);
			if (!config.prefix || !Array.isArray(config.prefix)) config.prefix = (0, __stryke_convert_to_array.toArray)(config.prefix ?? []);
			if (!config.prefix.includes("automd")) config.prefix.push("automd");
			if (!config.prefix.includes("powerlines")) config.prefix.push("powerlines");
			return { automd: (0, defu.default)(config ?? {}, {
				configFile: options.configFile,
				allowIssues: true,
				dir: this.config.projectRoot,
				watch: false,
				input: "README.md",
				toc: {
					maxDepth: 6,
					bullets: "-"
				}
			}) };
		},
		async configResolved() {
			if (this.config.framework && !(0, __stryke_convert_to_array.toArray)(this.config.automd.prefix).includes(this.config.framework)) this.config.automd.prefix = (0, __stryke_convert_to_array.toArray)(this.config.automd.prefix).concat(this.config.framework);
			if (this.config.automd.configFile) {
				const { config } = await (0, c12.loadConfig)({
					cwd: this.config.automd.dir,
					configFile: this.config.automd.configFile,
					defaults: {
						ignore: [
							"**/node_modules",
							"**/dist",
							"**/.*"
						],
						dir: this.config.automd.dir
					}
				});
				this.config.automd = require_index.resolveConfig((0, defu.default)(this.config.automd, {
					...config,
					prefix: (0, __stryke_convert_to_array.toArray)(config.prefix ?? [])
				}));
			}
			this.config.automd.input = (await Promise.all((0, __stryke_convert_to_array.toArray)(this.config.automd.input).map(async (input) => {
				if (input.includes("*")) return (0, __stryke_fs_list_files.listFiles)((0, __stryke_path_is_type.isAbsolutePath)(input) ? input : (0, __stryke_path_append.appendPath)(input, this.config.projectRoot), { ignore: this.config.automd.ignore });
				return (0, __stryke_path_is_type.isAbsolutePath)(input) ? input : (0, __stryke_path_append.appendPath)(input, this.config.projectRoot);
			}))).flat();
			if (this.config.automd.output && !(0, __stryke_path_is_type.isAbsolutePath)(this.config.automd.output)) this.config.automd.output = (0, __stryke_path_append.appendPath)(this.config.automd.output, this.config.projectRoot);
			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: (0, markdown_toc.default)(ctx.block.md, {
						...opts,
						maxdepth: opts.maxDepth,
						first1: opts.firstH1
					}).content };
				}
			};
		},
		async docs() {
			await Promise.all((0, __stryke_convert_to_array.toArray)(this.config.automd.input).map(async (input) => {
				const contents = await this.fs.read(input);
				if (contents) {
					const result = await require_index.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((0, __stryke_path_append.appendPath)(this.config.automd.output ? (0, __stryke_fs_is_file.isDirectory)(this.config.automd.output) ? (0, __stryke_path_replace.replacePath)(input, this.config.automd.output) : this.config.automd.output : input, this.config.projectRoot), result.contents);
				}
			}));
		}
	};
};
var src_default = plugin;

//#endregion
exports.default = src_default;
exports.plugin = plugin;