"use client";

import React, { useEffect } from "react";
import { useDispatch } from "react-redux";
import { initSocket } from "@/providers/helper/socket.io";
import {
  setUserOnline,
  setVisitorOnline,
} from "@/utils/store/features/data/useronline.slice";
// import { description } from "@/config/default";

const AdminVisitor = ({ children, description }) => {
  const dispatch = useDispatch();

  useEffect(() => {
    if (!description.useSocketIO) return;
    // first time
    const socketConnection = async () => {
      const socket = await initSocket();
      socket.emit("admin-getvisitor", {});

      socket.on("admin-getvisitor", (data) => {
        dispatch(setUserOnline(data.visitor.filter((item) => item?.data?._id)));
        dispatch(
          setVisitorOnline(
            data.visitor.filter((item) => !item?.data?._id || !item)
          )
        );
      });
    };

    socketConnection();
  }, [dispatch, description]);

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

export default AdminVisitor;
