import {useCallback, useEffect, useState} from "react";
import {getAllCategory} from "@/service/api/article.ts";


const ArticleTags: OptionItem[] = [];
export default function useArticleTags() {
	const [tags, _setTags] = useState<OptionItem[]>([]);
	const setTags = useCallback(() => {
		_setTags([

			...ArticleTags
		])
	}, [])

	useEffect(() => {
		if (ArticleTags.length === 0) {
			getAllCategory().then(res => {
				ArticleTags.length = 0;
				res.tags.forEach(t => {
					const item = {
						label: t.tag_name,
						value: t.tag_id,
						children: t.sons && t.sons.length > 0 ? t.sons.map(s => ({
							label: s.tag_name,
							value: s.tag_id
						})) : []
					};
					ArticleTags.push(item)
				})
				setTags()
			})
		}else{
			setTags()
		}
	}, [])
	return tags
}