import { Local } from "@/utils/storage";
import { useEffect, useState } from "react";

/**
 * @description
 *
  1、查看内容 - ViewContent   打开首页
  2.完成注册 - CompleteRegistration
  3.登陆 - SubmitApplication
  4.开始试用游戏 - StartTrial    启动一次游戏就触发
  5.加入购物车 - AddToCart    付费一次就回传一次，包含复充
  6.拉起订单 - InitiateCheckout    用户发起充值，调出第三方充值二维码
  7.首充事件 - Purchase   新用户第一次完成首充回传一次，之后复充不在回传
 */
type PixelType = "kwai_pixel" | "facebook_pixel";

const useEventPoint = () => {
    const [source, setSource] = useState<PixelType | null>(null);
    useEffect(() => {
        setSource(Local.getKey("ban_pixel_type"));
    }, []);

    // 查看内容 | null
    const eventView = () => {
        if (source === "kwai_pixel") {
            window.kwaiq.track("contentView");
        }
        if (source === "facebook_pixel") {
            window.fbq("track", "ViewContent");
        }
    };
    // 注册
    const eventRegister = () => {
        if (source === "kwai_pixel") {
            window.kwaiq.instance(Local.getKey("ban_pixel_id")).track("completeRegistration");
        }
        if (source === "facebook_pixel") {
            window.fbq("track", "CompleteRegistration");
        }
    };

    // 登录
    const eventLogin = () => {
        if (source === "facebook_pixel") {
            window.fbq("track", "SubmitApplication");
        }
    };

    // 开始试用游戏
    const eventStartTrial = () => {
        if (source === "facebook_pixel") {
            window.fbq("track", "StartTrial");
        }
    };

    // 充值
    const eventPurchase = () => {
        if (source === "kwai_pixel") {
            window.kwaiq.instance(Local.getKey("ban_pixel_id")).track("purchase");
        }
        if (source === "facebook_pixel") {
            window.fbq("track", "AddToCart");
        }
    };

    //拉起订单
    const eventInitiate = () => {
        if (source === "facebook_pixel") {
            window.fbq("track", "InitiateCheckout");
        }
    };

    //首充事件
    const eventFirstDeposit = () => {
        if (source === "kwai_pixel") {
            window.kwaiq.instance(Local.getKey("ban_pixel_id")).track("firstDeposit");
        }
        if (source === "facebook_pixel") {
            window.fbq("track", "Purchase");
        }
    };

    return {
        eventView,
        eventRegister,
        eventLogin,
        eventStartTrial,
        eventPurchase,
        eventInitiate,
        eventFirstDeposit,
    };
};

export { useEventPoint };
