import React from "react";
import { useEffect } from "react";
import { customerApi } from "@/api/api";
import { useDispatch } from "react-redux";
import { useRouter, useSearchParams } from "next/navigation";
import { categoryStoreActions } from "@/store/category";
import { accountStoreActions } from "@/store/account";
import { loadCart, loadWishlist } from "@/store/index";

const CustomDataLoad = () => {
  const dispatch = useDispatch();
  const params = useSearchParams();
  const router = useRouter();
  useEffect(() => {
    if (params.get("loginPopup") === "true") {
      dispatch(accountStoreActions.openSignInUI());
      router.replace("/", undefined, { shallow: true });
    }
  }, [params]);
  const onCategoryload = async () => {
    dispatch(categoryStoreActions.setCategoryLoader({ loader: true }));
    await customerApi
      .get("/categories?page=1&limit=0&params=&setName=category_main")
      .then((res) => {
        dispatch(
          categoryStoreActions.updateCategory({ category: res.data.result })
        );
        dispatch(categoryStoreActions.setCategoryLoader({ loader: false }));
      })
      .catch((err) => {
        dispatch(categoryStoreActions.setCategoryLoader({ loader: false }));
        console.log(err);
      });
  };
  useEffect(() => {
    onCategoryload();
  }, []);
  return;
};

export default CustomDataLoad;
