import { t as localDateStr } from "./local-date-DAy8dS7b.js";
//#region extensions/memory-core/src/topic-types.ts
/**
* Topic file types, parsing, and serialization for the memory system.
*
* Topic files are Markdown files with YAML frontmatter and ## heading entries.
* Each topic file is named by subject (e.g. `feishu.md`, `philosophy.md`) and
* lives under memory/topics/{subject}.md.
*/
function todayIso() {
	return localDateStr();
}
const FRONTMATTER_RE = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/;
const ENTRY_HEADING_RE = /^## (.+) \((\d{4}-\d{2}-\d{2})\)$/;
/**
* Parse a topic entry heading line into title and date.
* Heading format: `## Entry Title (YYYY-MM-DD)`
*/
function parseTopicEntryHeading(line) {
	const match = line.match(ENTRY_HEADING_RE);
	if (!match) return null;
	return {
		title: match[1],
		date: match[2]
	};
}
/**
* Parse a single entry section (everything after a `## Title (date)` heading
* until the next `## ` heading or end-of-file).
*/
function parseTopicEntry(entryMarkdown) {
	const lines = entryMarkdown.split(/\r?\n/);
	const parsed = parseTopicEntryHeading(lines[0]?.trim() ?? "");
	let title = "Untitled";
	let date = todayIso();
	if (parsed) {
		title = parsed.title;
		date = parsed.date;
	}
	const contentLines = lines.slice(1);
	if (contentLines.length > 0 && contentLines[0]?.trim() === "") contentLines.shift();
	let type;
	const typeLineIdx = contentLines.findIndex((l) => l.trim().match(/^- \*\*Type\*\*: (core|active|user|feedback|project|reference)$/));
	if (typeLineIdx >= 0) {
		const typeMatch = contentLines[typeLineIdx].trim().match(/^- \*\*Type\*\*: (core|active|user|feedback|project|reference)$/);
		if (typeMatch) {
			type = typeMatch[1];
			contentLines.splice(typeLineIdx, 1);
		}
	}
	const content = contentLines.join("\n").trimEnd();
	return {
		title,
		date,
		content,
		type
	};
}
/**
* Parse raw YAML frontmatter text into a record. Minimal parser — no
* external dependencies, handles simple `key: value` pairs.
*/
function parseYamlFrontmatter(yamlText) {
	const result = {};
	for (const line of yamlText.split(/\r?\n/)) {
		const colonIdx = line.indexOf(":");
		if (colonIdx < 0) continue;
		const key = line.slice(0, colonIdx).trim();
		result[key] = line.slice(colonIdx + 1).trim();
	}
	return result;
}
/**
* Parse a complete topic file (frontmatter + entries) from raw markdown.
* Backward-compatible: gracefully handles old `type` frontmatter.
*/
function parseTopicFile(markdown) {
	const fmMatch = markdown.match(FRONTMATTER_RE);
	const raw = markdown;
	let subject = "";
	let created = todayIso();
	let updated = todayIso();
	let entriesCount = 0;
	if (fmMatch) {
		const fm = parseYamlFrontmatter(fmMatch[1]);
		subject = fm.subject ?? "";
		created = fm.created ?? todayIso();
		updated = fm.updated ?? todayIso();
		entriesCount = Number(fm.entries) || 0;
	}
	const body = fmMatch ? markdown.slice(fmMatch[0].length) : markdown;
	const entries = [];
	const sectionStarts = [];
	const bodyLines = body.split(/\r?\n/);
	for (let i = 0; i < bodyLines.length; i++) {
		const line = bodyLines[i]?.trim() ?? "";
		if (ENTRY_HEADING_RE.test(line)) sectionStarts.push(i);
	}
	for (let s = 0; s < sectionStarts.length; s++) {
		const start = sectionStarts[s];
		const end = s + 1 < sectionStarts.length ? sectionStarts[s + 1] : bodyLines.length;
		const sectionText = bodyLines.slice(start, end).join("\n");
		entries.push(parseTopicEntry(sectionText));
	}
	if (entriesCount === 0 && entries.length > 0) entriesCount = entries.length;
	return {
		frontmatter: {
			subject,
			created,
			updated,
			entries: entriesCount
		},
		entries,
		raw
	};
}
/**
* Format a single entry heading line.
*/
function formatEntryHeading(entry) {
	return `## ${entry.title} (${entry.date})`;
}
/**
* Serialize a single TopicEntry to markdown text.
*/
function serializeTopicEntry(entry) {
	const parts = [formatEntryHeading(entry)];
	if (entry.content || entry.type) parts.push("");
	if (entry.type) parts.push(`- **Type**: ${entry.type}`);
	if (entry.content) parts.push(entry.content);
	return parts.join("\n");
}
/**
* Serialize a full TopicFile back to markdown.
*/
function serializeTopicFile(topic) {
	const fm = topic.frontmatter;
	const fmBlock = [
		"---",
		`subject: ${fm.subject}`,
		`created: ${fm.created}`,
		`updated: ${fm.updated}`,
		`entries: ${topic.entries.length}`,
		"---",
		""
	].join("\n");
	const entriesText = topic.entries.map((e) => serializeTopicEntry(e)).join("\n\n");
	return entriesText ? `${fmBlock}${entriesText}\n` : `${fmBlock}\n`;
}
/**
* Create an empty TopicFile with no entries.
*/
function createEmptyTopicFile(subject, _name) {
	const today = todayIso();
	const raw = [
		"---",
		`subject: ${subject}`,
		`created: ${today}`,
		`updated: ${today}`,
		"entries: 0",
		"---",
		""
	].join("\n");
	return {
		frontmatter: {
			subject,
			created: today,
			updated: today,
			entries: 0
		},
		entries: [],
		raw
	};
}
//#endregion
export { parseTopicFile as a, parseTopicEntryHeading as i, formatEntryHeading as n, serializeTopicEntry as o, parseTopicEntry as r, serializeTopicFile as s, createEmptyTopicFile as t };
