"use client";

import { useState, useCallback } from "react";
import { shareContent, ShareOptions } from "@/utils/share";

/**
 * Returns a share trigger and a transient "copied" toast flag.
 *
 * Usage:
 *   const { share, copied } = useShare();
 *   <button onClick={() => share({ title: "...", path: "/video/123" })}>
 *     {copied ? "Copied!" : "Share"}
 *   </button>
 */
export function useShare(toastMs = 2000) {
  const [copied, setCopied] = useState(false);

  const share = useCallback(async (opts: ShareOptions) => {
    const result = await shareContent(opts);
    if (result === "copied" || result === "fallback") {
      setCopied(true);
      setTimeout(() => setCopied(false), toastMs);
    }
  }, [toastMs]);

  return { share, copied };
}
