
/**
 * @param {String} id HTML ID
 * @param {String} type Base tag type => examples include 'button' 'div'
 * @param {Object} attrs Config object with the following valid fields and their types
 *
 * @returns HTMLElement
 */
exports.new_node = function (id, type, attrs) {
	let node = document.createElement(type)
	if(id) { node.id = id; }

	for(const key in attrs) {
		if(key == 'classList') {
			attrs['classList'].forEach(cls => node.classList.add(cls))
			continue
		}
		node[key] = attrs[key]
	}
	return node
}

/**
 * {Config} config | configuration object passed from the ipc
 * {String} dom_id | element container for the server list itself
 * */
exports.build_server_list = function (config, dom_id) {
	const channels = require('./channels.js')
	if(!config['servers']) { return; }


	let container = document.getElementById(dom_id)

	for(const server of config['servers']) {
		const id = server['domain'] || server['ip']

		let child = html.new_node(id, 'button', {
			'classList': ['btn', 'btn-outline-secondary', 'btn-nav-settings'],
			'textContent': server['name'],
			'type': 'button',
			'onClick': channels.get_channels(id, server['port'])
		})
		// Why not jquery? because it doesn't write to the node's attributes itself
		// for some reason
		child.addEventListener('click', function() {
			// NOTE: at some point /invite/<code> will be handled on port
			channels.get_channels(server['domain'], server['port'])
		})

		container.appendChild(child)
	}
}
