import { Plugin } from "..";

export default class CompileTimeJS extends Plugin {
    name = "compile-time-js";
    rewriteTriggers = ["html", "*"]
    longLasting = false;
    build = undefined;
    
    async rewriteFile(file: string, filePath: string): Promise<string> {
        let input = file;
        const regex = /{&(.+)&}/gms;

        let m;
        
        while ((m = regex.exec(file)) !== null) {
            if (m.index === regex.lastIndex) {
                regex.lastIndex++;
            }

            input = file.slice(0, m.index) + eval(m[1]) + file.slice(m.index + m[0].length);
        }

        return input;
    }
}