import { createContext, useEffect, useReducer, useState } from "react";
import { GET_CATEGORY } from "@/core/data/apiRoutes";
import useRequest from "@/lib/app/hooks/useRequest";

const reducer = (state, action) => {
    switch (action.type) {
        case "CHANGE_ACTIVE_TAB":
            return state.map((answer, index) =>
                action.newValue === index
                    ? { ...answer, active: true }
                    : {
                          ...answer,
                          active: false,
                      }
            );
        case "CHANGE_LIST":
            return [...action.newList];
        case "CHANGE_ACTIVE_ID":
            return state.map((answer, index) =>
                action.tab_id === answer.id
                    ? {
                          ...answer,
                          active_category_id: action.category_id,
                      }
                    : { ...answer }
            );
        default:
            return state;
    }
};

const submission = (handleCategories, operation) => {
    switch (operation.type) {
        case "SET_CATEGORIES":
            return { ...handleCategories, categoryLists: operation.categoriesByAPI };
        case "SET_SUBCATEGORIES":
            return { ...handleCategories, subCategoryLists: operation.subCategoriesByAPI };
        default:
            return handleCategories;
    }
};

export const AnswersContext = createContext();
export const AnswersProvider = ({ children }) => {
    const requestServer = useRequest({ auth: true, notification: false });
    const [openCallDialog, setOpenCallDialog] = useState(false);
    const [activeTab, setActiveTab] = useState(0);
    const [handleCategories, operation] = useReducer(submission, {
        categoryLists: [],
        subCategoryLists: [],
    });
    const [state, dispatch] = useReducer(reducer, []);

    useEffect(() => {
        requestServer(GET_CATEGORY, "get")
            .then(({ data }) => {
                const subCategoriesByAPI = data.data;
                const categoriesByAPI = subCategoriesByAPI.reduce((accumulator, currentValue) => {
                    if (!accumulator.find((item) => item.category_id === currentValue.category_id)) {
                        accumulator.push(currentValue);
                    }
                    return accumulator;
                }, []);
                operation({ type: "SET_CATEGORIES", categoriesByAPI });
                operation({ type: "SET_SUBCATEGORIES", subCategoriesByAPI });
            })
            .catch(() => {});
    }, []);

    return (
        <AnswersContext.Provider
            value={{
                openCallDialog,
                setOpenCallDialog,
                state,
                dispatch,
                activeTab,
                setActiveTab,
                categoryLists: handleCategories.categoryLists,
                subCategoryLists: handleCategories.subCategoryLists,
            }}
        >
            {children}
        </AnswersContext.Provider>
    );
};
