"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handler = exports.builder = exports.desc = exports.command = void 0;
const semver_1 = require("semver");
const config_1 = require("../config/config");
const utils_1 = require("@syftdata/common/lib/utils");
const utils_2 = require("../utils");
const codehandler_1 = require("@syftdata/codehandler");
exports.command = 'publish';
exports.desc = 'Publish event definitions';
const builder = (y) => {
    return y
        .option('apikey', {
        describe: 'Syft API key.',
        type: 'string'
    })
        .option('input', {
        describe: 'input folder with events.ts file.',
        default: (0, utils_2.getSchemaFolder)(),
        type: 'string'
    })
        .positional('release', {
        choices: ['major', 'minor', 'patch'],
        default: 'patch'
    });
};
exports.builder = builder;
async function handler({ input, release, apikey, remote }) {
    if (apikey === undefined) {
        (0, utils_1.logFatal)('Please provide an API key');
        return;
    }
    const project = (0, codehandler_1.createTSProject)([input]);
    const ast = (0, codehandler_1.generateASTForProject)(project);
    const expression = (0, config_1.getConfigExpressionForProject)(project);
    if (ast === undefined || expression === undefined) {
        (0, utils_1.logFatal)('Config file is not found in the input folder');
        return;
    }
    const config = (0, config_1.getConfigFromExpression)(expression);
    const currVer = (0, semver_1.parse)(config.version);
    if (currVer === null) {
        (0, utils_1.logFatal)(`An invalid version is provided in config: ${config.version}`);
        return;
    }
    const newVer = (0, semver_1.inc)(currVer, release, true);
    if (newVer === null) {
        (0, utils_1.logFatal)(`Failed to increment version: ${config.version}`);
        return;
    }
    (0, config_1.setConfigVersionOnExpression)(expression, newVer);
    project.saveSync();
    (0, utils_1.logInfo)(`Version bumped from ${config.version} -> ${newVer ?? ''}`);
}
exports.handler = handler;
