"use client";

import React, { useEffect } from "react";
import { initSocket } from "@/providers/helper/socket.io";
// import { description } from "@/config/default";

const VisitorProvider = ({ userData, anonymous, description, children }) => {
  useEffect(() => {
    if (!description.useSocketIO) return;

    let socket;

    const socketConnection = async () => {
      socket = await initSocket();

      socket.connect();

      socket.on("connect", () => {});
      socket.emit("update-visitor", userData || anonymous);
      socket.on("connect_error", (error) => {
        console.log(error);
      });
    };

    socketConnection();

    return () => {
      if (socket?.connected) socket.disconnect();
    };
  }, [userData, anonymous, description]);

  return <>{children}</>;
};

export default VisitorProvider;
