import { useAuth } from "@/auth";
import routes from "@/routes";
import { selectors } from "@/store";
import { useSelector } from "react-redux";
import { Navigate, Outlet } from "react-router-dom";

interface ICheckSubscriptionOutletProps {
  subscribedReturnUrl?: string;
  unsubscribedReturnUrl?: string;
}

function CheckSubscriptionOutlet({
  subscribedReturnUrl = routes.client.home(),
  unsubscribedReturnUrl,
}: ICheckSubscriptionOutletProps) {
  const status = useSelector(selectors.selectStatus);
  const { user } = useAuth();

  if (user && status === "subscribed") {
    return <Navigate to={subscribedReturnUrl} replace={true} />;
  }
  if (unsubscribedReturnUrl?.length) {
    return <Navigate to={unsubscribedReturnUrl} replace={true} />;
  }
  return <Outlet />;
}

export default CheckSubscriptionOutlet;
