import vituum from "vituum"
import nunjucks from "@vituum/vite-plugin-nunjucks"
import checker from "vite-plugin-checker";

export default {
    publicDir: "public",
    plugins: [
        vituum(),
        nunjucks({
            root: "./src",
            extensions: {
                IncludeOnceExtension: includeOnceExtension,
            },
        }),
        checker({
            typescript: true,
        }),
    ],
    server: {
        cors: {
            "origin": "*",
            "methods": "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS",
            "preflightContinue": false,
            "optionsSuccessStatus": 204
        },
        proxy: {
            '/api': {
                target: 'http://localhost:8080',
                changeOrigin: false,
            },
        }
    }
}

function includeOnceExtension() {
    this.tagName = "includeOnce";
    this.tags = [this.tagName];
    this.alreadyIncluded = new Set();

    this.parse = function (parser, nodes, _lexer) {
        const tag = parser.peekToken();
        if (!parser.skipSymbol(this.tagName)) {
            parser.fail("parseInclude: expected " + this.tagName);
        }

        const node = new nodes.Include(tag.lineno, tag.colno);
        node.template = parser.parseExpression();

        if (parser.skipSymbol("ignore") && parser.skipSymbol("missing")) {
            node.ignoreMissing = true;
        }

        parser.advanceAfterBlockEnd(tag.value);

        const includedPath = node.template.value;
        if (this.alreadyIncluded.has(includedPath)) {
            return new nodes.NodeList(0, 0, []);
        }

        this.alreadyIncluded.add(includedPath);
        return node;
    };
}
