import { defineTemplate } from "../metadata-types.js";
import { REPLACEMENTS, dependencyNames, firstOf, isValidUrl, mixedStringsToArray, nonEmpty, toAlias, toBasicLicenses, toBasicNames, toLocalUrl, toMb, toStatus } from "../utilities/template-helpers.js";
import { codemetaJson } from "./codemeta-json.js";
import is from "@sindresorhus/is";
//#region src/lib/templates/frontmatter.ts
/**
* A compact, non-nested, polyglot overview of the project.
* Designed for Obsidian frontmatter — flat keys with natural language names,
* blending all available sources into a single trackable snapshot.
*/
const frontmatter = defineTemplate((context, templateData) => {
	const codemeta = codemetaJson(context, templateData);
	const codeStats = firstOf(context.codeStats)?.data;
	const dependencyUpdates = firstOf(context.dependencyUpdates);
	const fileStats = firstOf(context.fileStats)?.data;
	const cinderCinderblockXml = firstOf(context.cinderCinderblockXml)?.data;
	const github = firstOf(context.github)?.data;
	const gitStats = firstOf(context.gitStats)?.data;
	const metascope = context.metascope?.data;
	const nodeNpmRegistry = firstOf(context.nodeNpmRegistry)?.data;
	const obsidianPluginManifestJson = firstOf(context.obsidianPluginManifestJson)?.data;
	const obsidianPluginRegistry = firstOf(context.obsidianPluginRegistry)?.data;
	const pythonPypiRegistry = firstOf(context.pythonPypiRegistry)?.data;
	const primaryLanguages = mixedStringsToArray(codemeta.programmingLanguage ?? github?.primaryLanguage, REPLACEMENTS) ?? null;
	const secondaryLanguages = mixedStringsToArray(codeStats?.total?.languages, REPLACEMENTS)?.filter((value) => !primaryLanguages?.includes(value)) ?? null;
	const rawName = codemeta.name === "undefined" ? null : cinderCinderblockXml === void 0 ? codemeta.name : `Cinder ${codemeta.name}`;
	const id = codemeta.identifier ?? rawName;
	const name = is.nonEmptyString(rawName) ? toAlias(rawName) : void 0;
	const aliases = id === name ? void 0 : id;
	return {
		Name: name ?? null,
		ID: id ?? null,
		Description: codemeta.description ?? null,
		Author: mixedStringsToArray(toBasicNames(codemeta.author)) ?? null,
		Contributor: mixedStringsToArray(toBasicNames(codemeta.contributor)) ?? null,
		Maintainer: mixedStringsToArray(toBasicNames(codemeta.maintainer)) ?? null,
		Version: codemeta.version ?? null,
		Account: github?.ownerLogin ?? null,
		Public: !(github?.isPrivate ?? false),
		Fork: github?.isFork ?? false,
		Published: Boolean(obsidianPluginRegistry?.url ?? nodeNpmRegistry?.url ?? pythonPypiRegistry?.url),
		Status: toStatus(codemeta.codeRepository, codemeta.author, [...codemeta.contributor ?? [], ...codemeta.maintainer ?? []], github?.isFork ?? false, templateData.authorName, templateData.githubAccount),
		Tags: codemeta.keywords ?? null,
		Aliases: nonEmpty([aliases]) ?? null,
		License: toBasicLicenses(codemeta.license) ?? null,
		Language: primaryLanguages,
		"Secondary Language": secondaryLanguages,
		"Repo Path": metascope?.options.path === void 0 ? null : `file://${metascope.options.path}`,
		"VS Code Path": metascope?.options.path === void 0 ? null : `vscode://file/${metascope.options.path}`,
		"Readme Path": toLocalUrl(codemeta.readme, metascope?.options.path) ?? null,
		"Homepage URL": codemeta.url !== void 0 && !codemeta.url.startsWith("https://github.com/") ? codemeta.url : null,
		"Repo URL": codemeta.codeRepository ?? github?.url ?? null,
		"Issues URL": codemeta.issueTracker ?? null,
		"Readme URL": codemeta.readme !== void 0 && isValidUrl(codemeta.readme) ? codemeta.readme : null,
		"Package URL": obsidianPluginRegistry?.url ?? nodeNpmRegistry?.url ?? pythonPypiRegistry?.url ?? null,
		Created: codemeta.dateCreated ?? null,
		"First Commit Date": gitStats?.commitDateFirst ?? null,
		"Latest Commit Date": gitStats?.commitDateLast ?? null,
		"Latest Push Date": github?.pushedAt ?? null,
		"Latest Release Date": github?.releaseDateLatest ?? nodeNpmRegistry?.publishDateLatest ?? pythonPypiRegistry?.publishDateLatest ?? gitStats?.tagVersionDateLatest ?? null,
		"Latest Release Version": obsidianPluginManifestJson?.version ?? nodeNpmRegistry?.versionLatest ?? pythonPypiRegistry?.versionLatest ?? github?.releaseVersionLatest ?? gitStats?.tagVersionLatest ?? null,
		Stars: github?.stargazerCount ?? null,
		Watchers: github?.watcherCount ?? null,
		Contributors: github?.contributorCount ?? gitStats?.contributorCount ?? null,
		Forks: github?.forkCount ?? null,
		"Downloads Total": nodeNpmRegistry?.downloadsTotal ?? obsidianPluginRegistry?.downloadCount ?? pythonPypiRegistry?.downloads180Days ?? github?.releaseDownloadCount ?? null,
		"Downloads Monthly": nodeNpmRegistry?.downloadsMonthly ?? pythonPypiRegistry?.downloadsMonthly ?? null,
		Releases: github?.releaseCount ?? gitStats?.tagReleaseCount ?? null,
		"Issues Open": github?.issueCountOpen ?? null,
		"Issues Closed": github?.issueCountClosed ?? null,
		"PRs Open": github?.pullRequestCountOpen ?? null,
		"PRs Merged": github?.pullRequestCountMerged ?? null,
		"PRs Closed": github?.pullRequestCountClosed ?? null,
		"Vulnerability Alerts": github?.vulnerabilityAlertCount ?? null,
		"Lines of Code": codeStats?.total?.code ?? null,
		"Total Files": fileStats?.totalFileCount ?? null,
		"Total Size MB": toMb(fileStats?.totalSizeBytes) ?? null,
		"Tracked Files": gitStats?.trackedFileCount ?? null,
		"Tracked Size MB": toMb(gitStats?.trackedSizeBytes) ?? null,
		"GitHub Size MB": toMb(github?.diskUsageBytes) ?? null,
		Dependencies: dependencyNames(codemeta, "prod") ?? null,
		"Dev Dependencies": dependencyNames(codemeta, "dev") ?? null,
		"Major Updates": dependencyUpdates?.data.major?.length ?? 0,
		"Minor Updates": dependencyUpdates?.data.minor?.length ?? 0,
		"Patch Updates": dependencyUpdates?.data.patch?.length ?? 0,
		"Total Updates": dependencyUpdates?.extra?.total ?? 0,
		Libyears: dependencyUpdates?.extra?.libyears ?? 0,
		Runtime: codemeta.runtimePlatform ?? null,
		"Operating System": codemeta.operatingSystem ?? null,
		"Forked From": github?.forkedFrom ?? null,
		"Fork Ahead": github?.commitsAheadUpstream ?? null,
		"Fork Behind": github?.commitsBehindUpstream ?? null,
		"Template From": github?.templateFrom ?? null,
		Organization: github?.isInOrganization ?? false,
		Monorepo: metascope?.workspaceDirectories === void 0 ? false : metascope.workspaceDirectories.length > 0,
		"Git Commits": gitStats?.commitCount ?? null,
		"Git Dirty Files": gitStats?.uncommittedFileCount ?? null,
		"Git Remotes Ahead": gitStats?.totalAhead ?? null,
		"Git Remotes Behind": gitStats?.totalBehind ?? null,
		"Git LFS": gitStats?.hasLfs ?? github?.hasLfs ?? false,
		"Git Tags": gitStats?.tagCount ?? null,
		"Git Current Branch": gitStats?.branchCurrent ?? null,
		"Git Branches": gitStats?.branchCount ?? null,
		"Git Remotes": gitStats?.remoteCount ?? null,
		"Git Submodules": gitStats?.submoduleCount ?? 0
	};
});
//#endregion
export { frontmatter };
