import { promises as fs } from 'fs';
import { join } from 'path';
import { pathExists } from 'fs-extra';
import log from '../../lib/log.js';
import { EMULSIFY_PROJECT_TEMPLATES_FOLDER } from '../../lib/constants.js';
import renderTemplate from './renderTemplate.js';
export default async function resolveComponentTemplate(projectRoot, format, logicalName, vars) {
    const templatePath = join(projectRoot, EMULSIFY_PROJECT_TEMPLATES_FOLDER, format, logicalName);
    if (!(await pathExists(templatePath))) {
        return null;
    }
    const template = await fs.readFile(templatePath, 'utf8');
    if (template.trim() === '') {
        log('warn', `Component template override "${templatePath}" is empty; using the built-in template instead.`);
        return null;
    }
    return renderTemplate(template, vars);
}
