// TODO: mocked. swap for sh.tangled.pipeline.listPipelines via bobbin, the way
// issues/+page.ts does. a real load hands `q` to the server instead of matching here.
import { pipelines as mockPipelines } from "$lib/components/repo/pipelines/mock";
import { matchesQuery, type PipelineFilter } from "$lib/components/repo/pipelines/pipeline";
import type { PageLoad } from "./$types";

const FILTERS: PipelineFilter[] = ["all", "push", "pull_request"];

export const load: PageLoad = async (event) => {
	const requested = event.url.searchParams.get("trigger") as PipelineFilter | null;
	const filter = requested && FILTERS.includes(requested) ? requested : "all";
	const query = event.url.searchParams.get("q") ?? "";

	const pipelines = mockPipelines
		.filter((pipeline) => filter === "all" || pipeline.trigger.kind === filter)
		.filter((pipeline) => matchesQuery(pipeline, query));

	return {
		filter,
		query,
		pipelines,
		// lets the list tell "no runs at all" from "nothing matched"
		hasAny: mockPipelines.length > 0
	};
};
