"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CplaceVersion = void 0;
const fs = require("fs");
const utils_1 = require("../utils");
const path = require("path");
class CplaceVersion {
    constructor(rawVersion, major, minor, patch, appendix) {
        this.rawVersion = rawVersion;
        this.major = major;
        this.minor = minor;
        this.patch = patch;
        this.appendix = appendix;
    }
    static initialize(currentRepo, providedCurrentVersion, force) {
        if (force) {
            this._currentVersion = undefined;
            this._cplaceVersion = undefined;
            this._createdOnBranch = undefined;
        }
        if (this._currentVersion ||
            this._cplaceVersion ||
            this._createdOnBranch) {
            throw new Error(`(CplaceVersion) version has already been initialized`);
        }
        this.parseVersionsFromVersionFile(currentRepo);
        // If cplaceVersion was not specified in version.gradle, use createOnBranch as fallback
        if (this._cplaceVersion == undefined) {
            this._cplaceVersion = this._createdOnBranch;
        }
        // Override current version if it was provided as a command line argument
        if (providedCurrentVersion) {
            this._currentVersion = new CplaceVersion(providedCurrentVersion, 0, 0, 0, '');
        }
        // If currentVersion is not in the version file and is not provided, use cplaceVersion as fallback
        if (this._currentVersion == undefined) {
            this._currentVersion = this._cplaceVersion;
        }
        if (this._currentVersion == undefined &&
            this._cplaceVersion == undefined &&
            this._createdOnBranch == undefined) {
            console.error(utils_1.cerr `[CplaceVersion] Cplace version was not detected in version.gradle file`);
            throw new Error(`[CplaceVersion] Cplace version was not detected in version.gradle file`);
        }
        console.log(utils_1.cgreen `⇢`, `current version: ${CplaceVersion.getCurrentVersion()}, cplace version: ${this._cplaceVersion.rawVersion}`);
    }
    static parseVersionsFromVersionFile(currentRepo) {
        const versionFilePath = path.resolve(currentRepo, CplaceVersion.VERSION_GRADLE);
        if (!fs.existsSync(versionFilePath)) {
            console.warn(utils_1.cwarn `[NPM] Could not find version.gradle in repo ${currentRepo}...`);
            console.warn(utils_1.cwarn `[CplaceVersion] -> Assuming version 1.0.0`);
            CplaceVersion._currentVersion = new CplaceVersion('1.0.0', 1, 0, 0, '');
        }
        else {
            const versionFileContent = fs.readFileSync(versionFilePath, 'utf8');
            const currentVersionString = this.readVersionStringFromFile(versionFileContent, 'currentVersion');
            const cplaceVersionString = this.readVersionStringFromFile(versionFileContent, 'cplaceVersion');
            const createdOnBranchString = this.readVersionStringFromFile(versionFileContent, 'createdOnBranch');
            this.parseCreatedOnBranch(createdOnBranchString);
            this.parseCurrentVersion(currentVersionString);
            this.parseCplaceVersion(cplaceVersionString);
        }
    }
    static readVersionStringFromFile(versionFileContent, stringPattern) {
        const versionString = versionFileContent
            .split('\n')
            .find((line) => line.includes(stringPattern) &&
            !line.trim().startsWith('//'));
        return versionString
            ? versionString.split('=')[1].replace(/'/g, '').trim()
            : null;
    }
    static parseVersion(versionString, versionPattern) {
        var _a, _b;
        const match = versionString.match(versionPattern);
        if (!match) {
            console.log(utils_1.cwarn `[CplaceVersion] provided version string '${versionString}' does not match the expected format`);
            return undefined;
        }
        return new CplaceVersion(versionString, parseInt(match[1]), parseInt(match[2]), parseInt((_a = match[3]) !== null && _a !== void 0 ? _a : 0), (_b = match[4]) !== null && _b !== void 0 ? _b : '');
    }
    static parseCurrentVersion(currentVersion) {
        if (currentVersion) {
            this._currentVersion = new CplaceVersion(currentVersion, 0, 0, 0, '');
        }
    }
    static parseCplaceVersion(cplaceVersionString) {
        if (cplaceVersionString) {
            const versionPattern = /([0-9]{2})\.([1-4])/;
            this._cplaceVersion = this.parseVersion(cplaceVersionString, versionPattern);
        }
    }
    static parseCreatedOnBranch(createdOnBranchString) {
        if (createdOnBranchString) {
            const versionPattern = /release\/([0-9]{2})\.([1-4])/;
            this._createdOnBranch = this.parseVersion(createdOnBranchString, versionPattern);
        }
    }
    static getCurrentVersion() {
        return this._currentVersion.rawVersion;
    }
    static get() {
        if (CplaceVersion._cplaceVersion === undefined) {
            throw new Error(`(CplaceVersion) version has not been initialized. cplaceVersion or createdOnBranch must be specified in version.gradle`);
        }
        return CplaceVersion._cplaceVersion;
    }
    static isVersionLowerThen23() {
        const version = this.get();
        if (!version) {
            return true;
        }
        return version.major < 23;
    }
}
exports.CplaceVersion = CplaceVersion;
CplaceVersion.VERSION_GRADLE = 'version.gradle';
CplaceVersion._currentVersion = undefined;
CplaceVersion._cplaceVersion = undefined;
CplaceVersion._createdOnBranch = undefined;
//# sourceMappingURL=CplaceVersion.js.map