/*
    작성자 : 박지윤
    작성일 : 2025-02-04
    수정자 :
    수정일 :
    설명   : 공통 api
*/
import { ref, onMounted } from "vue";
import $api from '@api';

const commonApi = (options = {}) => {
    const colorList = ref([]);
    const mbtiList = ref([]);
    const pwhintList = ref([]);
    const yearCategory = ref([]);
    const cateList = ref([]);

    // type 파라미터를 추가로 받도록 수정
    const CommonCode = async (path, endpoint, targetList, type = null) => {
        const params = type ? { type } : {};
        const response = await $api.get(`${path}/${endpoint}`, {
            params
        });
        targetList.value = response.data.data.map(item => ({
            label: item.CMNCODNAM,
            value: item.CMNCODVAL,
        }));
    };

    onMounted(async () => {
        // 요청할 데이터가 옵션으로 전달 -> 그에 맞게 호출
        // color 옵션에 type 포함
        if (options.loadColor) {
            await CommonCode("user", "color", colorList, options.colorType);
        }
        if (options.loadMbti) await CommonCode("user", "mbti", mbtiList);
        if (options.loadPwhint) await CommonCode("user", "pwhint", pwhintList);
        if (options.loadYearCategory) await CommonCode("project", "yearCategory", yearCategory);
        if (options.loadCateList) await CommonCode("worddict", "getWordCategory", cateList);
    });

    return { colorList, mbtiList, pwhintList, yearCategory, cateList };
};

export default commonApi;
