"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(major, minor, patch, appendix) {
        this.major = major;
        this.minor = minor;
        this.patch = patch;
        this.appendix = appendix;
    }
    static initialize(currentRepo, cplaceVersion, 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`);
        }
        if (cplaceVersion) {
            this.parseCurrentVersion(cplaceVersion);
        }
        else {
            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, '');
            }
            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);
            }
        }
        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`);
        }
        if (this._currentVersion == undefined) {
            if (this._cplaceVersion) {
                this._currentVersion = this._cplaceVersion;
            }
            else {
                this._currentVersion = this._createdOnBranch;
            }
        }
    }
    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.error(utils_1.cerr `[CplaceVersion] provided version string does not match the provided pattern`);
            return undefined;
        }
        return new CplaceVersion(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) {
            const versionPattern = /([0-9]+)\.([0-9]+).([0-9]+)-?(SNAPSHOT|RC\.[0-9]+)?/;
            this._currentVersion = this.parseVersion(currentVersion, versionPattern);
        }
    }
    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 toString() {
        var _a, _b, _c, _d, _e;
        let version = `${(_a = this._currentVersion) === null || _a === void 0 ? void 0 : _a.major}.${(_b = this._currentVersion) === null || _b === void 0 ? void 0 : _b.minor}.${(_c = this._currentVersion) === null || _c === void 0 ? void 0 : _c.patch}`;
        if ((_d = this._currentVersion) === null || _d === void 0 ? void 0 : _d.appendix) {
            version += `-${(_e = this._currentVersion) === null || _e === void 0 ? void 0 : _e.appendix}`;
        }
        return version;
    }
    static get() {
        if (CplaceVersion._currentVersion === undefined) {
            throw new Error(`(CplaceVersion) version has not yet been initialized`);
        }
        return CplaceVersion._currentVersion;
    }
    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