"use client";
import React from "react";

import { useAppSelector } from "@/lib/hooks";
import { usePathname, useRouter } from "next/navigation";

// set route for disabled user to come, if user has login
const IF_LOGIN = ["/login", "/register"];

export function GuardedRoute({ children }: React.PropsWithChildren) {
  const router = useRouter();
  const pathname = usePathname();

  const auth = useAppSelector((state) => state.auth);
  console.log("GuardedRoute", auth, pathname);

  return children;
}

interface IGuardedRoute {
  children: React.ReactNode;
  role: string;
}

export function NeedLoginRoute({ children, role }: IGuardedRoute) {
  const router = useRouter();

  const auth = useAppSelector((state) => state.auth);

  if (auth.token && auth.roles.includes(role)) {
    return children;
  }
  console.log("NeedLoginRoute", auth, role);
  router.push("/login");
  return;
}