import useState from '~/hooks/useState'
import { fetch_json } from '~/hooks/utils'
import { load_config } from '~/hooks/config'

export const store_info = (key:string,res: any) => {
	useState().cvdata.value = {}
	useState().datalang.value = {}
	let models = []
	if (res.models) {
		models = res.models
	}
	Object.keys(res.data).forEach(ky => {
		if (ky === 'main') {
			useState().cvdata.value = res.data.main
			if (res.data.main.models) {
				res.data.main.models.forEach((mdl: any) => models.push(mdl))
			}
			if (res.data.main.showinfo) {
				const showItms = res.data.main.showinfo.filter((it: any) => it.ky === key)
				if (showItms[0]) {
					useState().showinfo.value = showItms[0]
				} else {
					const showPub = res.data.main.showinfo.filter((it: any) => it.ky === '')
					if (showPub[0])
						useState().showinfo.value = showPub[0]
				}
			}
		} else {
			useState().datalang.value[ky] = {}
	    Object.keys(res.data[ky]).forEach(sec => {
			  if (res.data[ky][sec]) {
					useState().datalang.value[ky][sec] = res.data[ky][sec]
				}
			})
		}
	})
	useState().models.value = models
	models.forEach((mdl: any) => useState().selectOps.value[mdl.id] = mdl)
}
export const load_data_model = async(id: string, rootpath: string, ky: string, with_auth: boolean, cllbck: any) => {
	if (id && useState().selectOps.value[id]) {
		const url = rootpath.includes('http') ? `${rootpath}/${id}` : useState().selectOps.value[id].path.replace('~',rootpath)
    const [res,err] =	await fetch_json(url, 2000, with_auth) 	
		if (err.length === 0 && res) {
	    store_info(ky,res)
	    useState().current_model.value = useState().selectOps.value[id]
	    useState().current_modelid.value = id
			if (cllbck) {
				cllbck()
			}
		}
  }
}
export const load_data_info = async (to: any, _from: any, next: any) => {
	let url = useState().CONFURLS.value.root || ''
	if (url.length > 0) {
		const [_res,err] = await load_config()
		if (err && err.length > 0) {
			next()
			return
		}
	}
	url = useState().CONFURLS.value.data || ''
	if (url.length == 0) {
		next()
		return
  }
	url = `${url}/${useState().MODEL_ID.value}`
	const [res,errmsg] =	await fetch_json(url, 2000,to.meta.withauth) 
	// fetch_json(url, 2000,to.meta.withauth,(res,errmsg) => {
	if (errmsg.length > 0) {
		next()
		return 
	}
	if (res && res.data) {
	  const ky = to.params.ky || to.query.k || ''
		store_info(ky,res)
	  if (useState().selectOps.value[useState().MODEL_ID.value]) {
			useState().current_model.value = useState().selectOps.value[useState().MODEL_ID.value]
		  useState().current_modelid.value = useState().MODEL_ID.value
		}
	} else {
		//next('404')
		next()
		return 
	}
 	next()
}
export const load_currentRoute= async(rt: any): Promise<boolean> => {
	let url = useState().CONFURLS.value.data || ''
	if (url.length == 0) {
		return false
  }
	url = `${url}/${useState().MODEL_ID.value}`
	const [res,errmsg] =	await fetch_json(url, 2000,rt.meta.withauth) 
	if (errmsg.length === 0 && res && res) {
	  const ky = rt.params.ky || rt.query.k || ''
		store_info(ky,res)
		if (useState().models.value[rt.meta.model]) {
			useState().current_model.value = useState().models.value[rt.meta.model]
	  }
		useState().current_modelid.value = rt.meta.model
		return true
	}	
	return false
}
export default {
  store_info,
	load_data_model,
	load_data_info,
	load_currentRoute,
}
