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

const usePriorityLists = () => {
    const [isLoadingPriorityLists, setIsLoadingPriorityLists] = useState(false)
    const [errorPriorityLists, setErrorPriorityLists] = useState(false)
    const [priorityLists, setPriorityLists] = useState([])
    const requestServer = useRequest({auth: true, notification: false})

    useEffect(() => {
        setIsLoadingPriorityLists(true)
        requestServer(GET_PRIORITY_LIST, 'get', {auth: true, notification: false}).then(({data}) => {
            const formattedData = data.data.map((priorityList, index) => ({
                id: index,
                name: priorityList.name,
                value: priorityList.id,
            }));
            setIsLoadingPriorityLists(false)
            setPriorityLists(formattedData);
        }).catch(() => {
            setIsLoadingPriorityLists(false)
            setErrorPriorityLists(true)
        })
    }, [])

    return {
        priorityLists,
        isLoadingPriorityLists,
        errorPriorityLists
    }
};

export default usePriorityLists;