import { defineTemplate } from "../metadata-types.js";
import { firstOf } from "../utilities/template-helpers.js";
import { codemetaJson } from "./codemeta-json.js";
//#region src/lib/templates/metadata.ts
/**
* Strip `git+` prefix and `.git` suffix from a URL.
*/
function normalizeGitUrl(url) {
	if (url === void 0) return void 0;
	let normalized = url;
	if (normalized.startsWith("git+")) normalized = normalized.slice(4);
	if (normalized.endsWith(".git")) normalized = normalized.slice(0, -4);
	return normalized;
}
/**
* A minimal metadata template for populating a GitHub repository's
* description, homepage, and topics via metadata.json / metadata.yaml.
*
* Builds on the codemeta template for baseline values, then lets any
* metadata.json source fields override the result.
*/
const metadata = defineTemplate((context, templateData) => {
	const codemeta = codemetaJson(context, templateData);
	const metadataFile = firstOf(context.metadataFile)?.data;
	const homepage = metadataFile?.homepage ?? codemeta.url ?? codemeta.codeRepository;
	return {
		description: metadataFile?.description ?? codemeta.description,
		homepage: normalizeGitUrl(homepage),
		topics: metadataFile?.keywords ?? codemeta.keywords
	};
});
//#endregion
export { metadata };
