"use client";

import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import { useAuthStore } from "@/store/authStore";
import { authoringAccessError, type AuthoringTarget } from "./authoringAccess";

export function useAuthoringWriteGuard(target: Omit<AuthoringTarget, "userId">) {
  const { t } = useTranslation();
  return () => {
    const refusal = authoringAccessError({ ...target, userId: useAuthStore.getState().user?.id });
    if (refusal) toast.error(t(refusal));
    return refusal === null;
  };
}
