"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.prepareSQL = exports.prepareAndMigrate = void 0;
const components_api_1 = require("../components-api");
const fs_1 = __importDefault(require("fs"));
const js_yaml_1 = __importDefault(require("js-yaml"));
const migrationPreparator_1 = __importDefault(require("../../migrationPreparator"));
const snapshotsDiffer_1 = require("../../snapshotsDiffer");
const prepareAndMigrate = async () => {
    const drizzleConfig = js_yaml_1.default.load(fs_1.default.readFileSync('drizzle.config.yml', { encoding: 'utf-8' }));
    const migrationRootFolder = drizzleConfig.migrationRootFolder || 'drizzle'; // or from config/params
    const dataFolder = drizzleConfig.dataFolder;
    try {
        const { prev, cur } = await prepareJsonSnapshots(migrationRootFolder, dataFolder);
        const sql = await exports.prepareSQL(prev, cur);
        // todo: save results to a new migration folder
        const folderName = prepareSnapshotFolderName();
        const migrationFolderPath = `./${migrationRootFolder}/${folderName}`;
        fs_1.default.mkdirSync(migrationFolderPath);
        fs_1.default.writeFileSync(`${migrationFolderPath}/snapshot.json`, JSON.stringify(cur, null, 2));
        fs_1.default.writeFileSync(`${migrationFolderPath}/migration.sql`, sql);
    }
    catch (e) {
        console.error(e);
    }
};
exports.prepareAndMigrate = prepareAndMigrate;
const prepareJsonSnapshots = async (migrationRootFolder, dataFolder) => {
    return await components_api_1.task('Preparing data schema json snapshot', async ({ setTitle }) => {
        return migrationPreparator_1.default(migrationRootFolder, dataFolder);
    });
};
const prepareSQL = async (prev, cur) => {
    const tablesResolver = async (input) => {
        try {
            const { created, deleted, renamed } = await components_api_1.promptTablesConflicts({
                newTables: input.created,
                missingTables: input.deleted,
            });
            return { created: created, deleted: deleted, renamed: renamed };
        }
        catch (e) {
            console.error(e);
            throw e;
        }
    };
    const columnsResolver = async (input) => {
        const result = await components_api_1.promptColumnsConflicts({ name: input.tableName, added: input.created, deleted: input.deleted });
        return { tableName: input.tableName, created: result.created, deleted: result.deleted, renamed: result.renamed };
    };
    return await snapshotsDiffer_1.applySnapshotsDiff(prev, cur, tablesResolver, columnsResolver);
};
exports.prepareSQL = prepareSQL;
const prepareSnapshotFolderName = () => {
    const now = new Date();
    return `${now.getFullYear()}${two(now.getUTCMonth() + 1)}${two(now.getUTCDate())}${two(now.getUTCHours())}${two(now.getUTCMinutes())}${two(now.getUTCSeconds())}`;
};
const two = (input) => {
    return input.toString().padStart(2, '0');
};
//# sourceMappingURL=migrate.js.map