import { MasterCSS } from '@master/css';
import parseHTML from './parse-html.mjs';
import { Text, Element } from 'domhandler';
import serialize from 'dom-serializer';

/**
 * Renders the page-required and sorted CSS text from HTML and injected it back into HTML
 * @param html
 * @param config
 */ function render(html, config) {
    const context = parseHTML(html);
    const { classes, nodes, htmlElement } = context;
    let { headElement, styleElement } = context;
    if (!classes.length) return {
        html,
        classes,
        nodes,
        htmlElement,
        headElement,
        styleElement
    };
    const css = new MasterCSS(config);
    classes.forEach((eachClass)=>css.add(eachClass));
    if (styleElement) {
        styleElement.childNodes = [
            new Text(css.text)
        ];
    } else {
        styleElement = new Element('style', {
            id: 'master'
        }, [
            new Text(css.text)
        ]);
        if (headElement) {
            headElement.childNodes.push(styleElement);
        } else {
            if (htmlElement) {
                headElement = new Element('head', {}, [
                    styleElement
                ]);
                htmlElement.childNodes.unshift(headElement);
            } else {
                nodes.unshift(styleElement);
            }
        }
    }
    return {
        html: serialize(nodes, {
            decodeEntities: false,
            encodeEntities: false
        }),
        css,
        classes,
        nodes,
        htmlElement,
        headElement,
        styleElement
    };
}

export { render as default };
