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

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

	const nestedPath = async ({ canvas }: PlayContext) => {
		await expect(canvas.getByRole("link", { name: "tangled" })).toHaveAttribute(
			"href",
			"/dawn/tangled/tree/main"
		);
		await expect(canvas.getByRole("link", { name: "web" })).toHaveAttribute(
			"href",
			"/dawn/tangled/tree/main/web"
		);
		await expect(canvas.getByRole("link", { name: "src" })).toHaveAttribute(
			"href",
			"/dawn/tangled/tree/main/web/src"
		);
		const current = canvas.getByText("lib");
		await expect(current).toHaveAttribute("aria-current", "page");
		await expect(canvas.queryByRole("link", { name: "lib" })).toBeNull();
	};

	const rootIsCurrent = async ({ canvas }: PlayContext) => {
		const current = canvas.getByText("tangled");
		await expect(current).toHaveAttribute("aria-current", "page");
		await expect(canvas.queryByRole("link")).toBeNull();
	};

	const encodesSegments = async ({ canvas }: PlayContext) => {
		await expect(canvas.getByRole("link", { name: "what#next?" })).toHaveAttribute(
			"href",
			"/dawn/tangled/tree/main/what%23next%3F"
		);
		await expect(canvas.getByText("100%.md")).toHaveAttribute("aria-current", "page");
	};

	const { Story } = defineMeta({
		title: "Repo/RepoCrumbs",
		component: RepoCrumbs,
		tags: ["autodocs"],
		args: {
			ownerHandle: "dawn",
			repoName: "tangled",
			ref: "main",
			path: "web/src/lib"
		}
	});
</script>

<Story name="Nested path" play={nestedPath} />
<Story name="Repository root" args={{ path: "" }} play={rootIsCurrent} />
<Story
	name="Segments needing encoding"
	args={{ path: "what#next?/100%.md" }}
	play={encodesSegments}
/>
