"use strict";
/*
 * Copyright 2020 NEM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.NemgenService = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const BootstrapUtils_1 = require("./BootstrapUtils");
class NemgenService {
    constructor(logger, params) {
        this.logger = logger;
        this.params = params;
    }
    async run(presetData) {
        const networkIdentifier = presetData.networkIdentifier;
        const symbolServerImage = presetData.symbolServerImage;
        const target = this.params.target;
        if (!presetData.nodes || !presetData.nodes.length) {
            throw new Error('Nodes must be defined in preset when running nemgen');
        }
        const nemesisWorkingDir = BootstrapUtils_1.BootstrapUtils.getTargetNemesisFolder(target, true);
        const nemesisSeedFolder = path_1.join(nemesisWorkingDir, `seed`, `${networkIdentifier}`, `0000`);
        await BootstrapUtils_1.BootstrapUtils.mkdir(nemesisSeedFolder);
        await fs_1.promises.copyFile(path_1.join(BootstrapUtils_1.BootstrapUtils.ROOT_FOLDER, `config`, `hashes.dat`), path_1.join(nemesisSeedFolder, `hashes.dat`));
        const name = presetData.nodes[0].name;
        const serverConfigWorkingDir = BootstrapUtils_1.BootstrapUtils.getTargetNodesFolder(target, true, name, 'server-config');
        BootstrapUtils_1.BootstrapUtils.validateFolder(nemesisWorkingDir);
        BootstrapUtils_1.BootstrapUtils.validateFolder(serverConfigWorkingDir);
        const cmd = [
            `${presetData.catapultAppFolder}/bin/catapult.tools.nemgen`,
            '--resources=/server-config',
            '--nemesisProperties=./server-config/block-properties-file.properties',
        ];
        const binds = [`${serverConfigWorkingDir}:/server-config`, `${nemesisWorkingDir}:/nemesis`];
        const userId = await BootstrapUtils_1.BootstrapUtils.resolveDockerUserFromParam(this.logger, this.params.user);
        const { stdout, stderr } = await BootstrapUtils_1.BootstrapUtils.runImageUsingExec(this.logger, {
            catapultAppFolder: presetData.catapultAppFolder,
            image: symbolServerImage,
            userId: userId,
            workdir: '/nemesis',
            cmds: cmd,
            binds: binds,
        });
        if (stdout.indexOf('<error>') > -1) {
            this.logger.info(stdout);
            this.logger.error(stderr);
            throw new Error('Nemgen failed. Check the logs!');
        }
        this.logger.info('Nemgen executed!!!!');
    }
}
exports.NemgenService = NemgenService;
