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({
		xueke: null,
		nianji: null
	})
	
	const userCode = getUserIdentity();

	onLoad(({nianji,cardId}) => {
		if (userCode !== 'Visitor') {
			const auth = cacheManager.get('auth');
			const { cardId:cardId_, nianji:nianji_ } = auth
			data.nianji = nianji_ || null;
			data.xueke = cardId_ || null;
		} else {
			data.nianji = nianji || null;
			data.xueke = cardId || null;
		}
	});

	async function getCatalogue() {
		return await catchError(httpCatalogue.getCatalogue({
			nianji: +data.nianji,
			cardId: +data.xueke
		}));
	}

	return {
		...toRefs(data),

		// 获取章节数据
		getCatalogue
	}
}