import { error } from "@sveltejs/kit";
import { gitTarget, tag, tags } from "$lib/api/gitclient";
import { toTagSummary } from "$lib/api/repo";
import type { PageLoad } from "./$types";

export const load: PageLoad = async (event) => {
	const parent = await event.parent();
	const git = gitTarget(parent.publicConfig, parent.repo, event.fetch);
	// the params arrive decoded, the wire names are raw
	const name = event.params.tag;
	let entry = await tag(git, name)
		.then((result) => result.tag)
		.catch(() => undefined);
	if (!entry && name === "latest") {
		entry = await tags(git, 1)
			.then((results) => results.tags?.[0])
			.catch(() => undefined);
	}
	if (!entry) error(404, `no tag named ${name}`);
	return { tag: toTagSummary(entry) };
};
