"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SeededPodInitializer = void 0;
const fs_extra_1 = require("fs-extra");
const LogUtil_1 = require("../logging/LogUtil");
const ErrorUtil_1 = require("../util/errors/ErrorUtil");
const Initializer_1 = require("./Initializer");
/**
 * Uses a {@link RegistrationManager} to initialize accounts and pods
 * for all seeded pods. Reads the pod settings from seededPodConfigJson.
 */
class SeededPodInitializer extends Initializer_1.Initializer {
    constructor(registrationManager, configFilePath) {
        super();
        this.logger = (0, LogUtil_1.getLoggerFor)(this);
        this.registrationManager = registrationManager;
        this.configFilePath = configFilePath;
    }
    async handle() {
        if (!this.configFilePath) {
            return;
        }
        const configuration = await (0, fs_extra_1.readJson)(this.configFilePath, 'utf8');
        let count = 0;
        for await (const input of configuration) {
            const config = {
                confirmPassword: input.password,
                createPod: true,
                createWebId: true,
                register: true,
                ...input,
            };
            this.logger.info(`Initializing pod ${input.podName}`);
            // Validate the input JSON
            const validated = this.registrationManager.validateInput(config, true);
            this.logger.debug(`Validated input: ${JSON.stringify(validated)}`);
            // Register and/or create a pod as requested. Potentially does nothing if all booleans are false.
            try {
                await this.registrationManager.register(validated, true);
                this.logger.info(`Initialized seeded pod and account for "${input.podName}".`);
                count += 1;
            }
            catch (error) {
                this.logger.warn(`Error while initializing seeded pod: ${(0, ErrorUtil_1.createErrorMessage)(error)})}`);
            }
        }
        this.logger.info(`Initialized ${count} seeded pods.`);
    }
}
exports.SeededPodInitializer = SeededPodInitializer;
//# sourceMappingURL=SeededPodInitializer.js.map