"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const template_1 = require("../create/template");
const templateProcessor_1 = require("../create/templateProcessor");
const fs = (0, tslib_1.__importStar)(require("fs-extra"));
const path_1 = require("path");
const abiToScript_1 = (0, tslib_1.__importDefault)(require("../create/abiToScript"));
class Refresh extends core_1.Command {
    constructor() {
        super(...arguments);
        this.dir = ".";
    }
    async catch(error) {
        // do something or
        // re-throw to be handled globally
        throw error;
    }
    async run() {
        this.dir = process.cwd();
        if (fs.existsSync(this.dir)) {
            let numFiles = fs.readdirSync(this.dir).length;
            if (numFiles && fs.existsSync((0, path_1.join)(this.dir, "tstemplate.json"))) {
                await this.handleExistingProject(Refresh.hardHatTemplate);
                return;
            }
            else {
                core_1.CliUx.ux.error("Cannot refresh HardHat project; use tokenscript create -h <HardHat Project Dir>");
            }
        }
    }
    getContract(values, templateDef) {
        const addressIndex = templateDef.templateFields.findIndex((element) => element.token === 'CONTRACT_ADDRESS');
        const chainIndex = templateDef.templateFields.findIndex((element) => element.token === 'CONTRACT_CHAIN');
        const nameIndex = templateDef.templateFields.findIndex((element) => element.token === 'TOKENSCRIPT_NAME');
        const addressVal = values[addressIndex];
        const chainIdVal = values[chainIndex];
        const nameVal = values[nameIndex];
        const tokenAddress = { chainId: chainIdVal, address: addressVal, name: nameVal };
        return tokenAddress;
    }
    async handleExistingProject(hardHat) {
        let templateDef = fs.readJsonSync(this.dir + "/tstemplate.json");
        if (!templateDef)
            throw new Error("Template file is invalid or not readable");
        if (hardHat) {
            //TODO: Keep tokenscript file and work out how to make the diff
            //      For first draft, rebuild tokenscript xml base
            if (fs.existsSync((0, path_1.join)(this.dir, "tokenscript.xml"))) {
                fs.rmSync((0, path_1.join)(this.dir, "tokenscript.xml"));
            }
            //add rebuild files in case we need them
            template_1.Templates.copyHHInit(this.dir);
            let templateProcessor = new templateProcessor_1.TemplateProcessor(templateDef, this.dir);
            let values = templateProcessor.getValuesFromTSTemplate();
            let tokenAddress = this.getContract(values, templateDef);
            //pull HardHat directory from tstemplate
            hardHat = templateProcessor.getHardHatDirectory();
            await templateProcessor.processTemplateUpdate(values);
            let ABIEncoder = new abiToScript_1.default(this.dir, tokenAddress);
            await ABIEncoder.initFromHardHat(hardHat, templateDef);
            template_1.Templates.cleanBuildFiles(this.dir);
            return true;
        }
        return false;
    }
}
exports.default = Refresh;
Refresh.description = 'Refresh a HardHat project';
Refresh.SRC_XML_FILE = "tokenscript.xml";
Refresh.hardHatTemplate = "hardhat";
