import {defineStore} from 'pinia';

import {reactive,ref} from 'vue';
import { book_item_data, chapter_item_data, user_status } from '../data/data';
import { tools } from '../framework/tools';
export const UserStatus = defineStore('user-status',()=>{
	let data:user_status = reactive(new user_status())
	let directoryList = ref<chapter_item_data[]>(null)
	async function updateUserSelectBook(d:book_item_data){
		directoryList.value = null
		data.cur_select_book_data=d;
	}
	function getUserSelectBook():book_item_data{
		if(data==null){
			return null
		}
		if(data.cur_select_book_data==null){
			return null
		}
		return data.cur_select_book_data
	}
	
	function getSelectBookDirectoryList(call_back){
		if(directoryList.value!=null){
			call_back(directoryList.value)
		}else{
			let selectBook = getUserSelectBook()
			if(selectBook!=null){
				tools.getChapterList(selectBook.chapter_path,(chapter_ls)=>{
					directoryList.value = chapter_ls
					call_back(directoryList.value)
				})
			}else{
				call_back(null)
			}
		}
		
	}
	return {data,getUserSelectBook,updateUserSelectBook,getSelectBookDirectoryList}
})