<script module lang="ts">
	import { defineMeta } from "@storybook/addon-svelte-csf";
	import type { loadRepoIndex } from "$lib/api/repoIndex";
	import StoryAuthProvider from "../../../../.storybook/StoryAuthProvider.svelte";
	import RepoIndexView from "./RepoIndexView.svelte";
	import type { RepoInfo } from "./types";

	const repo: RepoInfo = {
		uri: "at://did:plc:repo/sh.tangled.repo.repo/3jzrepo",
		rkey: "3jzrepo",
		name: "tangled",
		ownerDid: "did:plc:owner",
		ownerHandle: "dawn",
		repoDid: "did:plc:repo",
		knot: "knot1.tangled.sh",
		defaultBranch: "main"
	};

	const data = {
		ref: "main",
		isEmpty: false,
		needsUpgrade: false,
		knotUnreachable: false,
		files: [
			{ name: "src", kind: "directory" as const, size: 0 },
			{ name: "README.md", kind: "file" as const, size: 512 }
		],
		readme: { filename: "README.md", contents: "# tangled" },
		readmeHtml: "<h1>tangled</h1>",
		commits: [
			{
				hash: "0123456789abcdef0123456789abcdef01234567",
				shortHash: "01234567",
				subject: "add the repository index",
				body: "",
				authorName: "dawn",
				authorEmail: "dawn@example.test",
				when: "2026-07-28T09:00:00Z"
			}
		],
		tagsByCommit: {},
		totalCommits: 42,
		branches: [
			{
				name: "main",
				hash: "0123456789abcdef0123456789abcdef01234567",
				when: "2026-07-28T09:00:00Z",
				isDefault: true
			}
		],
		totalBranches: 1,
		tags: [{ name: "v1.0.0", hash: "abcdef0123456789", commitHash: "0123456789abcdef" }],
		totalTags: 1,
		refs: { branches: ["main"], tags: ["v1.0.0"], capped: false },
		languages: [
			{ name: "TypeScript", percentage: 70, share: 70 },
			{ name: "Svelte", percentage: 30, share: 30 }
		]
	} satisfies Awaited<ReturnType<typeof loadRepoIndex>>;
	const cappedData = {
		...data,
		totalBranches: 100,
		totalTags: 100,
		refs: { ...data.refs, capped: true }
	} satisfies Awaited<ReturnType<typeof loadRepoIndex>>;
	const emptyData = {
		...data,
		isEmpty: true,
		files: [],
		readme: null,
		readmeHtml: null,
		commits: [],
		tagsByCommit: {},
		totalCommits: 0,
		branches: [],
		totalBranches: 0,
		tags: [],
		totalTags: 0,
		refs: { branches: [], tags: [], capped: false },
		languages: []
	} satisfies Awaited<ReturnType<typeof loadRepoIndex>>;

	const { Story } = defineMeta({
		title: "Repo/RepoIndexView",
		component: RepoIndexView,
		tags: ["autodocs"],
		args: { repo, data, bobbinUrl: "https://bobbin.example.test" }
	});
</script>

<Story name="Populated repository" />
<Story name="Reference counts are lower bounds" args={{ data: cappedData }} />
<Story name="Knot needs upgrade" args={{ data: { ...data, needsUpgrade: true } }} />
<Story name="Knot is unreachable" args={{ data: { ...data, knotUnreachable: true } }} />
<Story name="Empty repository" asChild>
	<StoryAuthProvider>
		<RepoIndexView {repo} data={emptyData} bobbinUrl="https://bobbin.example.test" />
	</StoryAuthProvider>
</Story>
