import { Utils } from "zion-ui"

export class Service {
	/** 拼接成层级数据 */
	getDataSource(dataSource) {
		const nextDataSource = dataSource.map(data => {
			return {
				...data,
				key: data["id"],
				uiPid: data["pid"],
			}
		})
		const treeData = Utils.getTreeData(nextDataSource)
		return treeData
	}
	/** 获取keyPath */
	getKeyPath(id, dataSource) {
		const dataSourceMap = {}
		const nextDataSource = dataSource.map(data => {
			const nextData = {
				...data,
				key: data["id"],
				uiPid: data["pid"],
				label: data["title"] || data["label"]
			}
			dataSourceMap[data["id"]] = nextData
			return nextData
		})
		const parentKeys = Utils.getParents(id, nextDataSource)
		const parentLabels: any = []
		parentKeys.forEach(parent => {
			if (dataSourceMap[parent]) {
				parentLabels.push(dataSourceMap[parent]["label"])
			}
		})
		const current = nextDataSource.find(data => data["key"] == id)
		if (current) {
			parentKeys.push(current["key"])
			parentLabels.push(current["label"])
		}
		return {
			key: parentKeys.join("."),
			label: parentLabels.join(".")
		}
	}
}