"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
const _CommandUtils = require("typeorm/commands/CommandUtils");
const _nodepath = /*#__PURE__*/ _interop_require_default(require("node:path"));
const _chalk = /*#__PURE__*/ _interop_require_default(require("chalk"));
const _StringUtils = require("typeorm/util/StringUtils");
const _nodereadline = /*#__PURE__*/ _interop_require_wildcard(require("node:readline"));
const _PlatformTools = require("typeorm/platform/PlatformTools");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
function getTemplate(name, timestamp) {
    const migrationName = `${(0, _StringUtils.camelCase)(name, true)}${timestamp}`;
    return `import { MigrationInterface, QueryRunner } from "typeorm";

export class ${migrationName} implements MigrationInterface {
    name = '${migrationName}'

    public async up(queryRunner: QueryRunner): Promise<void> {
        // TODO: Add migration queries here
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        // TODO: Add rollback queries here
    }

}
`;
}
function getJavascriptTemplate(name, timestamp) {
    const migrationName = `${(0, _StringUtils.camelCase)(name, true)}${timestamp}`;
    return `const { MigrationInterface, QueryRunner } = require("typeorm");

module.exports = class ${migrationName} {
    name = '${migrationName}'

    async up(queryRunner) {
        // TODO: Add migration queries here
    }

    async down(queryRunner) {
        // TODO: Add rollback queries here
    }
}
`;
}
async function confirm(question, hint) {
    const line = _nodereadline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    return new Promise((resolve)=>{
        line.question(question, (response)=>{
            line.close();
            resolve(response);
        });
        line.write(hint);
    });
}
async function main() {
    const timestamp = _CommandUtils.CommandUtils.getTimestamp(Date.now());
    const extension = ".ts";
    try {
        console.log(_chalk.default.green(`Creating empty migration file...`));
        let name;
        while(!name){
            name = (await confirm("Please provide a name for the migration: ", "NewMigration"))?.trim();
            if (name) break;
            console.log(_chalk.default.red(`Name is required, try again`));
            process.exit(1);
        }
        const fullPath = _nodepath.default.resolve(process.cwd(), `src/migrations/${name}`);
        const filename = timestamp + "-" + _nodepath.default.basename(fullPath) + extension;
        const fileContent = false ? getJavascriptTemplate(_nodepath.default.basename(fullPath), timestamp) : getTemplate(_nodepath.default.basename(fullPath), timestamp);
        const migrationFileName = _nodepath.default.dirname(fullPath) + "/" + filename;
        await _CommandUtils.CommandUtils.createFile(migrationFileName, fileContent);
        console.log(_chalk.default.green(`Migration ${_chalk.default.blue(migrationFileName)} has been generated successfully.`));
    } catch (err) {
        _PlatformTools.PlatformTools.logCmdErr("Error during migration creation:", err);
        process.exit(1);
    }
}
let start = new Date().getTime();
main().then(()=>{
    let end = new Date().getTime();
    console.log("done", end - start, "ms");
    process.exit(0);
});

//# sourceMappingURL=typeorm-create.js.map