
const util = require('util')
const path = require("path");
const dirTree = require("directory-tree");
const tree = dirTree("docs");
const fs = require("fs");

console.log("Current directory:", __dirname);

/* 
	This transform the node from dirTree into a form acceptable by Vuepress sidebar 
	a.name becomes a.text
	a.path becomes a.link , and remove this field if there is children and set collapsible to true
	recursively do the same operator for each a.children
*/
function transform_sidebar(child) {

	child.title = child.name.replace(".md", "");;
	child.link = child.path.replace("docs", "");

	if (child.link == "/README.md") {
		child.link = "/";		/* special case */
		child.title = "Home";
	}

	/* no children , return array of link/text */
	if (!child.children) {
		return [child.link, child.title];
	}

	/* return as a hash record with title/children */
	if (child.children) {

		let rec = { title: child.title, collapsible: true, children: [] };
		child.children.forEach(children => {
			/* bad child if this is a terminating node */
			if (!children.name.endsWith(".md") && !children.children) {
				// console.log("Ignoring : " + children.name)
				return;
			}
			rec.children.push(transform_sidebar(children));
		});
		return rec;
	}
}

// comment

let sidebar = [];

tree.children.forEach(child => {
	if (child.name == ".vuepress") return;
	if (child.name == ".npmignore") return;
	sidebar.push(transform_sidebar(child));
});

console.log("SIDEBAR", sidebar[8]);


module.exports = {

	title: 'Kinisi-User-Guide',
	description: 'Just playing around',

	head: [
		['link', { rel: 'icon', href: '/favicon.ico' }]
	],

	server: {
		hmr: { port: 8080 },
	},

	// sidebar
	themeConfig: {
		sidebar: sidebar,
		searchMaxSuggestions: 10,
		lastUpdated: 'Last Updated' 			// string | boolean 
	},

	plugins: [
		["vuepress-plugin-code-copy", { staticIcon: true, align: 'top' }],
		['@vuepress/plugin-register-components', { componentsDir: "/vp/docs/.vuepress/compdir" }],
		['tabs'],
		// ["demo", { car: 'mercedes' }],
		["pagedata", { car: 'mercedes' }]
		// ['vuepress-plugin-graphviz']
	],

	// add javascript to header
	head: [
		// ['script', { src: 'https://cdnjs.cloudflare.com/ajax/libs/viz.js/1.3.0/viz123.js' }]
		['script', { src: '/viz.js' }]
	],

	/*
	markdown: {
		extendMarkdown: md => {
			md.use(require('markdown-it-html5-embed'), {
				html5embed: {
					useImageSyntax: true,
					useLinkSyntax: false
				}
			})
		}
	}
	*/

}

