const fs = require('fs');
var path = require('path');
const OUTPUT_PATH = './stencil';
const OUTPUT_FILENAME = 'custom-elements-full.json';

module.exports = function generateCustomElementsJson(docsData) {
	console.log('Generating Custom Elements JSON');
	// TODO: Add slot info?

	const jsonData = {
		components: docsData.components.map(component => ({
			name: component.tag,
			description: component.docs,
			attributes: component.props
				.filter(prop => prop.attr)
				.map(prop => {
					// normalize formatting of default value to match type formatting
					const formattedDefault = prop.default ? prop.default.replace(/'/g, '"') : '';

					// remove escape characters, we do not need them
					const formattedDocs = prop.docs ? prop.docs.replace(/\\/g, '') : '';

					return {
						name: prop.attr,
						type: prop.type,
						description: formattedDocs,
						defaultValue: formattedDefault,
						required: prop.required
					};
				})
		}))
	};

	const folder = path.join(__dirname, OUTPUT_PATH);

	if (!fs.existsSync(folder)) {
		fs.mkdirSync(folder);
	}

	return fs.writeFileSync(path.join(folder, OUTPUT_FILENAME), JSON.stringify(jsonData, null, 2));
};
