<script module lang="ts">
	import { defineMeta, type StoryContext } from "@storybook/addon-svelte-csf";
	import { expect } from "storybook/test";
	import BranchTable from "./BranchTable.svelte";
	import type { BranchSummary } from "./types";

	const branches: BranchSummary[] = [
		{
			name: "main",
			hash: "0123456789abcdef0123456789abcdef01234567",
			when: "2026-07-28T09:00:00Z",
			isDefault: true,
			message: "merge pull request #42\n\na longer body nobody reads"
		},
		{
			name: "feature/storybook",
			hash: "abcdef0123456789abcdef0123456789abcdef01",
			when: "2026-07-27T09:00:00Z",
			isDefault: false,
			message: "add stories for everything"
		}
	];

	type PlayContext = Pick<StoryContext<Record<string, unknown>>, "canvas">;

	const rowsLinkOut = async ({ canvas }: PlayContext) => {
		const treeLinks = canvas.getAllByRole("link", { name: "main" });
		await expect(treeLinks[0]).toHaveAttribute("href", "/dawn/tangled/tree/main");
		const hashLinks = canvas.getAllByRole("link", { name: "01234567" });
		await expect(hashLinks[0]).toHaveAttribute("href", "/dawn/tangled/commits/main");
		await expect(canvas.getByText("merge pull request #42")).toBeInTheDocument();
		await expect(canvas.queryByText(/a longer body/)).toBeNull();
		await expect(canvas.getAllByText("Default")).toHaveLength(2);
	};

	const empty = async ({ canvas }: PlayContext) => {
		await expect(canvas.getByText("This repository has no branches.")).toBeVisible();
	};

	const { Story } = defineMeta({
		title: "Repo/BranchTable",
		component: BranchTable,
		tags: ["autodocs"],
		args: {
			ownerHandle: "dawn",
			repoName: "tangled",
			branches
		}
	});
</script>

<Story name="Branches" play={rowsLinkOut} />
<Story name="Empty" args={{ branches: [] }} play={empty} />
