import assert from 'assert';
import path from 'path';
import { minimatch } from 'minimatch';
import TemplateFile from './template-file.mjs';
export default class TemplateFileFs {
    fragmentFiles;
    rootFiles = [];
    extension;
    delimiter;
    constructor(options = {}) {
        this.extension = options.extension || 'jhi';
        this.delimiter = options.delimiter || '&';
        this.fragmentFiles = {};
    }
    isTemplate(filePath) {
        return this.isRootTemplate(filePath) || this.isDerivedTemplate(filePath);
    }
    isRootTemplate(filePath) {
        return path.extname(filePath) === `.${this.extension}`;
    }
    isDerivedTemplate(filePath) {
        return minimatch(filePath, `**/*.${this.extension}.*`, { dot: true });
    }
    add(file) {
        assert(file.contents, 'contents is required');
        const templateFile = this.get(file.path);
        templateFile.compile(file.path, file.contents.toString(), { delimiter: this.delimiter });
        if (templateFile.rootTemplate) {
            templateFile.file = file;
        }
        else {
            this.get(templateFile.parentPath).addFragment(templateFile);
        }
        return templateFile;
    }
    get(filePath) {
        assert(filePath, 'filePath is required');
        this.fragmentFiles[filePath] = this.fragmentFiles[filePath] || new TemplateFile(path.basename(filePath), this.extension);
        return this.fragmentFiles[filePath];
    }
}
