import {GET_LIMITED_PROVINCE_LIST} from "@/core/data/apiRoutes";
import {useRequest} from "@witel/webapp-builder";
import {useEffect, useState} from "react";

const useLimitedProvince = () => {
    const [isLoadingProvinceList, setIsLoadingProvinceList] = useState(false)
    const [errorProvinceList, setErrorProvinceList] = useState(false)
    const [provinceList, setProvinceList] = useState([])
    const requestServer = useRequest({auth: true, notification: false})

    useEffect(() => {
        setIsLoadingProvinceList(true)
        requestServer(GET_LIMITED_PROVINCE_LIST, 'get', {auth: true, notification: false}).then(({data}) => {
            const formattedData = data.data.map((province, index) => ({
                id: index,
                name: province.name,
                value: province.id,
            }));
            setIsLoadingProvinceList(false)
            setProvinceList(formattedData);
        }).catch(() => {
            setIsLoadingProvinceList(false)
            setErrorProvinceList(true)
        })
    }, [])

    return {
        provinceList,
        isLoadingProvinceList,
        errorProvinceList
    }
};

export default useLimitedProvince;