"use client";

import { useRouter } from "next/navigation";
import { ArrowLeft } from "lucide-react";
import { Button } from "./ui/button";

type SharedBackButtonProps = {
	fallbackHref?: string;
	label?: string;
};

export function SharedBackButton({
	fallbackHref = "/login",
	label = "Back",
}: SharedBackButtonProps) {
	const router = useRouter();

	const onBack = () => {
		if (typeof window !== "undefined" && window.history.length > 1) {
			router.back();
			return;
		}
		router.push(fallbackHref);
	};

	return (
		<Button variant="outline" type="button" onClick={onBack}>
			<ArrowLeft className="mr-2 h-4 w-4" />
			{label}
		</Button>
	);
}
