import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { slaughterGetProvinceService } from "../../../../slaughter-house/services/slaughter-get-provinces";

export const useAddBuyer = (page) => {
  const dispatch = useDispatch();
  const [killHouses, setKillhouses] = useState([]);
  const [stewards, setStewards] = useState([]);
  const [userData, setUserData] = useState(null);
  const [provinceData, setProvinceData] = useState([]);
  const [cityData, setCityData] = useState([]);
  const { inspectorGetKillHouses } = useSelector(
    (state) => state.inspectorSlice
  );

  useEffect(() => {
    dispatch(slaughterGetProvinceService()).then((r) => {
      setProvinceData(r.payload.data);
    });
  }, []);

  useEffect(() => {
    if (page === "admin_add_buyer" && inspectorGetKillHouses?.length) {
      setKillhouses(
        inspectorGetKillHouses.map((item) => ({
          name: item.name,
          key: item.key,
          killer: item.killer,
        }))
      );
    }
  }, [page, inspectorGetKillHouses]);

  return {
    killHouses,
    setKillhouses,
    stewards,
    setStewards,
    userData,
    setUserData,
    provinceData,
    cityData,
    setCityData,
  };
};
