import yaml from "js-yaml";
//#region src/markdown/frontmatter.ts
function stripQuotes(value) {
	if (value.startsWith("\"") && value.endsWith("\"") || value.startsWith("'") && value.endsWith("'")) return value.slice(1, -1);
	return value;
}
function coerceFrontmatterValue(value) {
	if (value === null || value === void 0) return;
	if (typeof value === "string") return value.trim();
	if (typeof value === "number" || typeof value === "boolean") return String(value);
	if (typeof value === "object") try {
		return JSON.stringify(value);
	} catch {
		return;
	}
}
function parseYamlFrontmatter(block) {
	try {
		const parsed = yaml.load(block);
		if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null;
		const result = {};
		for (const [rawKey, value] of Object.entries(parsed)) {
			const key = rawKey.trim();
			if (!key) continue;
			const coerced = coerceFrontmatterValue(value);
			if (coerced === void 0) continue;
			result[key] = coerced;
		}
		return result;
	} catch {
		return null;
	}
}
function extractMultiLineValue(lines, startIndex) {
	const match = lines[startIndex].match(/^([\w-]+):\s*(.*)$/);
	if (!match) return {
		value: "",
		linesConsumed: 1
	};
	const inlineValue = match[2].trim();
	if (inlineValue) return {
		value: inlineValue,
		linesConsumed: 1
	};
	const valueLines = [];
	let i = startIndex + 1;
	while (i < lines.length) {
		const line = lines[i];
		if (line.length > 0 && !line.startsWith(" ") && !line.startsWith("	")) break;
		valueLines.push(line);
		i++;
	}
	return {
		value: valueLines.join("\n").trim(),
		linesConsumed: i - startIndex
	};
}
function parseLineFrontmatter(block) {
	const frontmatter = {};
	const lines = block.split("\n");
	let i = 0;
	while (i < lines.length) {
		const match = lines[i].match(/^([\w-]+):\s*(.*)$/);
		if (!match) {
			i++;
			continue;
		}
		const key = match[1];
		const inlineValue = match[2].trim();
		if (!key) {
			i++;
			continue;
		}
		if (!inlineValue && i + 1 < lines.length) {
			const nextLine = lines[i + 1];
			if (nextLine.startsWith(" ") || nextLine.startsWith("	")) {
				const { value, linesConsumed } = extractMultiLineValue(lines, i);
				if (value) frontmatter[key] = value;
				i += linesConsumed;
				continue;
			}
		}
		const value = stripQuotes(inlineValue);
		if (value) frontmatter[key] = value;
		i++;
	}
	return frontmatter;
}
function parseFrontmatterBlock(content) {
	const normalized = content.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
	if (!normalized.startsWith("---")) return {};
	const endIndex = normalized.indexOf("\n---", 3);
	if (endIndex === -1) return {};
	const block = normalized.slice(4, endIndex);
	const lineParsed = parseLineFrontmatter(block);
	const yamlParsed = parseYamlFrontmatter(block);
	if (yamlParsed === null) return lineParsed;
	const merged = { ...yamlParsed };
	for (const [key, value] of Object.entries(lineParsed)) if (value.startsWith("{") || value.startsWith("[")) merged[key] = value;
	return merged;
}
/**
* Parse YAML frontmatter from markdown: returns metadata plus body after the closing `---`.
*/
function parseFrontmatter(content) {
	const frontmatterMatch = content.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n([\s\S]*)$/);
	if (!frontmatterMatch) return {
		frontmatter: {},
		content: content.trim()
	};
	const [, frontmatterRaw, bodyContent] = frontmatterMatch;
	try {
		return {
			frontmatter: yaml.load(frontmatterRaw) || {},
			content: bodyContent.trim()
		};
	} catch (error) {
		console.warn("Failed to parse frontmatter:", error);
		return {
			frontmatter: {},
			content: bodyContent.trim()
		};
	}
}
/**
* Serialize frontmatter to YAML format (with closing --- only; opening --- included in dump by yaml.dump pattern from original util).
*/
function serializeFrontmatter(frontmatter) {
	return "---\n" + yaml.dump(frontmatter) + "---";
}
//#endregion
export { parseFrontmatter, parseFrontmatterBlock, serializeFrontmatter };

//# sourceMappingURL=frontmatter.js.map