import path from 'path'

module.exports = {
	treeItemClicked,
	getTree,
	gotTree,
}

function treeItemClicked({state, actions, opts: {api}}, item) {
	let items = Object.assign({}, state.tree.items)

	const open = items[item.path]
	if (open) {
		// dispatch(C.CLOSE_TREE_ITEM, item)
		delete items[item.path]
		Object.keys(items).forEach( p => {
			if (path.join(p, '/').indexOf(path.join(item.path, '/')) === 0) delete items[p]
		})

	} else {
		actions.getTree(item.path)
		// api.getTree(item.path)
		// 	.then(json => {
		// 		dispatch(actions.gotTree, json)
		// 	})
	}

	return {
		...state,
		tree: {
			items,
			selected: item.path,
			fetching: !open,
		},
	}	

	// tree.selected = item.path
	// tree.fetching = !open
	// return newState

	// return {
	// 	...state,
	// 	tree: {items, selected: item.path, fetching},
	// }
}

function getTree({state, actions,  opts: {api}}, path) {
	api.getTree(path)
		.then(json => {
			actions.gotTree(json)
		})

	return state
}

function gotTree({state}, newTree) {
	return {
		...state,
		tree: {
			...state.tree,
			fetching: false,
			items: {
				...state.tree.items,
				[newTree.path]: newTree.nodes
			},
		},
	}
}

@@ -16,16 +16,56 @@
 m 'path'
+%0Aimport %7B Utils %7D from 'react-tree-menu'
 %0A%0Amodule
@@ -114,16 +114,33 @@
 gotTree,
+%0A%0A%09treeCollapsed,
 %0A%7D%0A%0Afunc
@@ -718,17 +718,16 @@
 %0A%09%09%7D,%0A%09%7D
-%09
 %0A%0A%09// tr
@@ -1069,93 +1069,797 @@
  %7B%0A%09
-return %7B%0A%09%09...state,%0A%09%09tree: %7B%0A%09%09%09...state.tree,%0A%09%09%09fetching: false,%0A%09%09%09items
+let items = %5B%5D.concat(state.tree.items)%0A%0A%09if (state.tree.cache) %7B%0A%09%09const node = state.tree.cache%0A%09%09node.children = newTree.nodes%0A%0A%09%09// console.log(%60node-$%7BJSON.stringify(state.tree.cache)%7D%60)%0A%09%09// console.log(%60gotTree-$%7BJSON.stringify(newTree.nodes)%7D%60)%0A%09%7D else %7B%0A%09%09items = %5B%5D.concat(newTree.nodes)%0A%09%7D%0A%0A%09// items = Utils.getNewTreeState(lineage, items, %22collapsed%22)%0A%0A%09return %7B%0A%09%09...state,%0A%09%09tree: %7B%0A%09%09%09source: newTree.path,%0A%09%09%09items,%0A%09%09%7D,%0A%09%7D%0A%7D%0A%0Afunction treeCollapsed(%7Bstate, actions%7D, lineage) %7B%0A%09let tree = %5B%5D.concat(state.tree.items)%0A%09const node = getNode(tree, lineage)%0A%09// console.log(%60node-$%7BJSON.stringify(node)%7D%60)%0A%0A%09// console.log(%60utils-$%7BJSON.stringify(Utils)%7D%60)%0A%09// return state%0A%0A%09node.collapsed = !node.collapsed%0A%0A%09actions.getTree(node.path)%0A%0A%09return %7B%0A%09%09...state,%0A%09%09tree
 : %7B%0A%09%09%09
-%09
 ...s
@@ -1871,60 +1871,394 @@
 tree
-.items,%0A%09%09%09%09%5BnewTree.path%5D: newTree.nodes%0A%09%09%09%7D,%0A%09%09%7D,
+,%0A%09%09%09cache: node,%0A%09%09%09items: tree%0A%09%09%7D%0A%09%7D%0A%7D%0A%0Aconst getNode = (tree, lineage) =%3E %7B%0A%09// console.log(%60lineage-$%7BJSON.stringify(lineage)%7D%60)%0A%09if (lineage.length === 0) %7B%0A%09%09return null%0A%09%7D else if (lineage.length === 1) %7B%0A%09%09// console.log(%60uno-$%7BJSON.stringify(tree%5Blineage%5B0%5D%5D)%7D%60)%0A%09%09return tree%5Blineage%5B0%5D%5D%0A%09%7D else %7B%0A%09%09const node = lineage.shift()%0A%09%09return getNode(tree%5Bnode%5D.children, lineage)
 %0A%09%7D%0A

Implement tree collapse handling