"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const utils_1 = require("../../utils");
class ReleaseBuilder {
    constructor() { }
    createReleaseJSON(dirPath, callback) {
        let result = {
            name: path_1.default.basename(dirPath),
            type: "folder",
            children: [],
        };
        fs_1.default.readdir(dirPath, (err, files) => {
            if (err) {
                callback(err, null);
                return;
            }
            let pending = files.length;
            if (!pending) {
                callback(null, result);
                return;
            }
            files.forEach((file) => {
                const filePath = path_1.default.join(dirPath, file);
                fs_1.default.stat(filePath, (err, stats) => {
                    if (stats && stats.isDirectory()) {
                        this.createReleaseJSON(filePath, (err, res) => {
                            result.children.push(res);
                            if (!--pending)
                                callback(null, result);
                        });
                    }
                    else {
                        fs_1.default.readFile(filePath, "utf-8", (err, data) => {
                            if (err) {
                                callback(err, null);
                                return;
                            }
                            result.children.push({
                                type: "file",
                                name: file,
                                content: data,
                                children: [],
                            });
                            if (!--pending)
                                callback(null, result);
                        });
                    }
                });
            });
        });
    }
    isFileExistAndCreated(fileName, data) {
        const filePath = path_1.default.join(__dirname, `../../data/${fileName}`);
        if (!fs_1.default.existsSync(filePath)) {
            this.createReleaseFile(filePath, data);
        }
        this.createReleaseFile(filePath, data);
    }
    createReleaseFile(filePath, data) {
        fs_1.default.writeFile(filePath, JSON.stringify(data, null, 2), (err) => {
            if (err) {
                utils_1.logger.error(`Error: ${err}`);
            }
            else {
                utils_1.logger.info(`success...`);
            }
        });
    }
    projectInit(appName, structure) {
        const folderPath = path_1.default.join(appName);
        if (!fs_1.default.existsSync(folderPath)) {
            fs_1.default.mkdirSync(folderPath);
        }
        this.createDirectoryContents(appName, folderPath, structure.children);
        return true;
    }
    createDirectoryContents(appName, currentPath, children) {
        children.forEach((child) => {
            const childPath = path_1.default.join(currentPath, child.name);
            if (child.type === "folder") {
                fs_1.default.mkdir(childPath, (err) => {
                    if (err) {
                        console.error(`Error creating folder ${child.name}:`, err);
                    }
                    else {
                        this.createDirectoryContents(appName, childPath, child.children);
                    }
                });
            }
            else if (child.type === "file" && child.content !== undefined) {
                if (child.name === "www") {
                    const updatedData = child.content.replace(/debug = require\("debug"\)\("appName:server"\);/, `debug = require("debug")("${appName}:server");`);
                    fs_1.default.writeFile(path_1.default.join(currentPath, child.name), updatedData, (err) => {
                        if (err) {
                            console.error(`Error creating file ${child.name}:`, err);
                        }
                    });
                }
                else if (child.name === "package.json") {
                    const jsonData = JSON.parse(child.content);
                    jsonData.name = appName;
                    const updatedData = JSON.stringify(jsonData, null, 2);
                    fs_1.default.writeFile(path_1.default.join(currentPath, child.name), updatedData, (err) => {
                        if (err) {
                            console.error(`Error creating file ${child.name}:`, err);
                        }
                    });
                }
                else {
                    fs_1.default.writeFile(path_1.default.join(currentPath, child.name), child.content, (err) => {
                        if (err) {
                            console.error(`Error creating file ${child.name}:`, err);
                        }
                    });
                }
            }
        });
    }
}
exports.default = ReleaseBuilder;
