import { useEffect, useState } from "react";
import { getStringFromStorage, saveToStorage } from "../utils";

export function useCommentState() {
  const [open, setOpen] = useState(false);
  const [mounted, setMounted] = useState(false);

  // 从 localStorage 恢复评论区状态
  useEffect(() => {
    if (typeof window === "undefined") return;
    const saved = getStringFromStorage("aweme_player_comments_open", "false");
    if (saved === "true") {
      setOpen(true);
    }
    // 短暂延迟后标记为已挂载，启用动画
    requestAnimationFrame(() => {
      requestAnimationFrame(() => {
        setMounted(true);
      });
    });
  }, []);

  // 持久化评论区状态
  useEffect(() => {
    if (typeof window === "undefined") return;
    saveToStorage("aweme_player_comments_open", open.toString());
  }, [open]);

  return { open, setOpen, mounted };
}
