import {func} from "@/uni_modules/uv-ui-tools/libs/function/test";
import {injectLocal, provideLocal} from "@vueuse/core";
import {numberToLetter} from "@/utils";

const key = Symbol('QUESTION_OPTION_SERVICE')

export const useProvideQuestionOptionFormatter = function (formatter, optionClass) {
    if (!func(formatter)) throw new Error('formatter must be a function')
    const options = {formatter, optionClass}
    provideLocal(key, options)
    return options
}

export const useInjectQuestionOptionFormatter = function () {
    // 提供了默认注入，默认注入是题库的组织格式
    return injectLocal(key, {
        formatter: (options) => {
            return options.map((opt, idx) => {
                const code = numberToLetter(idx)
                return {code, option: code + '、' + opt}
            })
        },
        optionClass: undefined
    })
}