import { acceptHMRUpdate, defineStore } from 'pinia'
import { useStorage } from '@vueuse/core'
import { getDetailCode } from '/@src/service/code'

export const useCodes = defineStore('codeStore', () => {
    // 🟡 상태
    const codes = ref<Record<number, Array<{ cd: string; nm: string }>>>({})

    // 🔵 액션: 코드 불러오기
    const setDetailCode = async (cd_grp: number) => {
        if (!codes.value[cd_grp]) {
            const res = await getDetailCode(cd_grp)
            codes.value[cd_grp] = res
        }
    }

    // 🟢 코드 목록 가져오기
    const getCodeList = (cd_grp: number) => {
        return codes.value[cd_grp] || []
    }

    return {
        codes,
        setDetailCode,
        getCodeList,
    }
})

/**
 * Pinia supports Hot Module replacement so you can edit your stores and
 * interact with them directly in your app without reloading the page.
 *
 * @see https://pinia.esm.dev/cookbook/hot-module-replacement.html
 * @see https://vitejs.dev/guide/api-hmr.html
 */
if (import.meta.hot) {
    import.meta.hot.accept(acceptHMRUpdate(useCodes, import.meta.hot))
}
