import {getPaperBatches} from "@/api/dz/papers.js";
import {createEventHook, injectLocal, provideLocal} from "@vueuse/core";

const key = Symbol('PaperBatchCondition')

export const useProvidePaperBatchCondition = function (type) {
    // 批次
    const buildType = type
    const batchId = ref('')
    const batchList = ref([])
    const batchEvent = createEventHook()

    onMounted(async () => {
        const res = await getPaperBatches()
        batchList.value = res.data
    })
    watch(batchId, async (batchId) => {
        if (batchId) {
            const payload = {buildType, batchId}
            await batchEvent.trigger(payload)
        }
    })

    const payload = {buildType, batchId, batchList, onBatchReady: batchEvent.on}
    provideLocal(key, payload)
    return payload
}

export const useInjectPaperBatchCondition = function () {
    return injectLocal(key)
}