/**
 * install/transforms/cursor.mjs — Cursor transform.
 *
 * Cursor quirks:
 *   - strip `color` and `skills` from frontmatter.
 *   - skills land under .cursor/rules/<name>.mdc (Cursor uses .mdc for
 *     rule files), agents under .cursor/commands/<name>.md.
 */

import { basename, dirname } from 'node:path'
import {
	parseFrontmatter,
	serializeFrontmatter,
	stripFields,
	substituteVersion,
} from './_shared.mjs'

const STRIP = ['color', 'skills']

/**
 * @param {import('../pipeline.mjs').TransformInput} input
 * @returns {import('../pipeline.mjs').TransformOutput[]}
 */
export function transform({ canonicalContent, canonicalRelPath, version, fileType }) {
	const subbed = substituteVersion(canonicalContent, version)
	const parsed = parseFrontmatter(subbed)
	let payload = subbed
	if (parsed.hasFrontmatter && parsed.fm) {
		stripFields(parsed.fm, STRIP)
		payload = serializeFrontmatter(parsed)
	}

	if (fileType === 'skill') {
		const folder = basename(dirname(canonicalRelPath))
		return [{ outputPath: `rules/${folder}.mdc`, content: payload }]
	}
	if (fileType === 'agent') {
		const file = basename(canonicalRelPath, '.md')
		return [{ outputPath: `commands/${file}.md`, content: payload }]
	}
	return []
}
