"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Templates = void 0;
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs-extra"));
const path_1 = require("path");
const path_2 = require("path");
class Templates {
    static getTemplateDescriptor(id) {
        const fileContents = fs.readFileSync((0, path_1.resolve)(this.templateDir, id, "tstemplate.json"), "utf-8");
        let templateDef = JSON.parse(fileContents);
        if (!templateDef)
            throw new Error("Could not resolve template");
        return templateDef;
    }
    static getScriptTemplate(tokenName) {
        let cardTemplate = {
            name: "",
            description: "",
            templateFields: [
                {
                    name: "Card Name",
                    token: `${tokenName}`,
                    prompt: ""
                }
            ]
        };
        return cardTemplate;
    }
    static copyTemplate(id, dest) {
        fs.copySync((0, path_1.resolve)(this.templateDir, id), dest, { recursive: true });
    }
    static initTemplate(dest, template) {
        let files = fs.readdirSync(dest);
        for (let fileName of files) {
            if (fileName.endsWith("html")) {
                fs.rmSync(fileName);
            }
        }
        if (fs.existsSync((0, path_2.join)(dest, "tokenscript.xml"))) {
            fs.rmSync((0, path_2.join)(dest, "tokenscript.xml"));
        }
        fs.rmSync((0, path_2.join)(dest, "tstemplate.json"));
        fs.copyFileSync((0, path_1.resolve)(this.templateDir, template, "tstemplate.json"), (0, path_2.join)(dest, "tstemplate.json"));
        this.copyHHInit(dest);
    }
    static copyHHInit(dest) {
        fs.copyFileSync((0, path_1.resolve)(this.templateDir, "hardhat", "tokenscript.xml"), (0, path_2.join)(dest, "tokenscript.xml"));
        this.copyCards(dest);
    }
    static copyCards(dest) {
        const files = fs.readdirSync((0, path_2.join)(this.templateDir, "hardhat")).filter(fileName => fileName.endsWith('.html'));
        for (const file of files) {
            fs.copyFileSync((0, path_1.resolve)(this.templateDir, "hardhat", file), (0, path_2.join)(dest, file));
        }
    }
    static cleanBuildFiles(dest) {
        //remove prep files
        if (fs.existsSync((0, path_2.join)(dest, "card-prototype.html"))) {
            fs.rmSync((0, path_2.join)(dest, "card-prototype.html"));
        }
    }
}
exports.Templates = Templates;
Templates.DEV_PATH = (0, path_2.join)(__dirname, "..", "..", "static/templates");
Templates.PROD_PATH = (0, path_2.join)(__dirname, "..", "templates");
Templates.templateDir = fs.existsSync(Templates.DEV_PATH) ? Templates.DEV_PATH : Templates.PROD_PATH;
Templates.templatesList = [
    {
        id: "emptySvelte",
        name: "Empty Svelte Project",
        description: "An empty TokenScript project with Svelte & Vite",
        location: "templates/emptySvelte"
    },
    {
        id: "emptyTypescript",
        name: "Empty TypeScript Project",
        description: "An empty TokenScript project with TypeScript & Webpack",
        location: "templates/emptyTypescript"
    },
    {
        id: "empty",
        name: "Empty Project",
        description: "An empty TokenScript project",
        location: "templates/empty"
    },
    /*{
        id: "entryToken",
        name: "Entry Token",
        description: "An example of using TokenScript to interact with ioT devices",
        location: "templates/entryToken"
    }*/
];
