"use client";

import AdSlot from "@/components/AdSlot";
import { usePaymentStatus } from "@/lib/usePaymentStatus";

/**
 * Same as AdSlot, but hides itself once there's no reason to show an ad:
 *  - Paid tool the visitor has unlocked (bought individually or via the
 *    all-tools bundle) — paid tools show no ads once unlocked.
 *  - Free tool once the visitor owns the "Remove Ads & Limits" bundle.
 */
export default function ToolAdSlot({
  tool,
  label,
  className,
}: {
  tool: { slug: string; tier: "free" | "pro" };
  label?: string;
  className?: string;
}) {
  const { loading, plan, adFree, unlockedTools } = usePaymentStatus({
    kind: "TOOL",
    toolSlug: tool.slug,
  });

  if (loading) return null; // avoid a flash of an ad that then disappears

  const isPro = plan === "PRO";
  const adFreeForThisTool =
    tool.tier === "pro" ? isPro || unlockedTools.includes(tool.slug) : isPro || adFree;

  if (adFreeForThisTool) return null;

  return <AdSlot label={label} className={className} />;
}
