import Mustache from 'mustache';

const renderTemplate = (templates, templateId, data) => {
    const template = templates[templateId];

    const isUsingCustomTemplate = !template && document.getElementById(templateId) !== null;
    if (isUsingCustomTemplate) {
        const customTemplate = document.getElementById(templateId).innerHTML;
        return Mustache.render(customTemplate, data);
    }

    if (!isUsingCustomTemplate && !template) {
        const msg = `Template ${templateId} has not been found.\n\nIf this is a custom template, make sure you spelled the template name correctly.\n\nIf using a custom template was not your intention, use one of the following templates:${Object.keys(templates).map(tpl => `\n\t- ${tpl}`)}`;
        console.error(msg);
        return;
    }

    return template(data);
}

export default renderTemplate;