import {
	reactive,
	toRefs
} from "vue";
import {
	catchError
} from "@/utils/common.js"
import * as httpCatalogue from "@/api/catalogue.js"
import cacheManager from "@/utils/cacheManager";
import {
	onLoad
} from "@dcloudio/uni-app";
import {getUserIdentity} from "@/utils/common.js"

		
export function useCatalogue() {
	const data = reactive({
		levelId: null,
		subjectId: null
	})
	
	const userCode = getUserIdentity();

	onLoad(({subjectId,levelId}) => {
		if (userCode !== 'Visitor') {
			const auth = cacheManager.get('auth');
			const { levelId:levelId, subjectId:subjectId } = auth
			data.subjectId = subjectId || null;
			data.levelId = levelId || null;
		} else {
			data.subjectId = subjectId || null;
			data.levelId = levelId || null;
		}
	});

	async function getCatalogue() {
		return await catchError(httpCatalogue.getCatalogue({
			subjectId: +data.subjectId,
			levelId: +data.levelId
		}));
	}

	return {
		...toRefs(data),

		// 获取章节数据
		getCatalogue
	}
}