import Guard from "./guard";
/**
 * Represents the file version of an assembly
 */
export class FileVersion {
    constructor(major, minor, patch = 0, revision = 0) {
        this.major = major;
        this.minor = minor;
        this.patch = patch;
        this.revision = revision;
    }
    /**
     * Parses a file version string (format: `<major>.<minor>.<patch>.<revision>`) to an instance.
     *
     * @remarks
     * Throws an error when parsing fails.
     *
     * @param fileVersion - The file version string to parse
     * @returns A file version instance.
     */
    static parse(fileVersion) {
        Guard.stringNotNullOrEmpty(fileVersion);
        try {
            if (!/^(\d+\.){3}\d+$/.test(fileVersion)) {
                throw 0;
            }
            const parts = fileVersion.split(".");
            if (parts.length !== 4)
                throw 0;
            const major = parseInt(parts[0]);
            const minor = parseInt(parts[1]);
            const patch = parseInt(parts[2]);
            const revision = parseInt(parts[3]);
            if (isNaN(major + minor + patch + revision))
                throw 0;
            return new FileVersion(major, minor, patch, revision);
        }
        catch (ex) {
            throw "Invalid version format. (Format: <major>.<minor>.<patch>.<revision>)";
        }
    }
    /**
     * Tries to parse a file version string (see {@link FileVersion.parse}) and returns `null` or the specified default
     * value when it fails.
     *
     * @param fileVersion - The file version string to parse
     * @param defaultValue - The value to return when parsing fails
     * @returns A file version instance or `null`.
     */
    static tryParse(fileVersion, defaultValue = null) {
        try {
            return FileVersion.parse(fileVersion);
        }
        catch (ex) {
            return defaultValue;
        }
    }
    /**
     * Compares if the current instance is smaller than the specified version.
     *
     * @param otherVersion - The version to compare the current instance to
     * @returns A boolean with the result.
     */
    smallerThan(otherVersion) {
        return this.compareTo(otherVersion) === -1;
    }
    /**
     * Compares if the current instance is smaller or equal than the specified version.
     *
     * @param otherVersion - The version to compare the current instance to
     * @returns A boolean with the result.
     */
    smallerThanOrEqual(otherVersion) {
        return this.compareTo(otherVersion) !== 1;
    }
    /**
     * Compares if the current instance is greater than the specified version.
     *
     * @param otherVersion - The version to compare the current instance to
     * @returns A boolean with the result.
     */
    greaterThan(otherVersion) {
        return this.compareTo(otherVersion) === 1;
    }
    /**
     * Compares if the current instance is greater or equal than the specified version.
     *
     * @param otherVersion - The version to compare the current instance to
     * @returns A boolean with the result.
     */
    greaterThanOrEqual(otherVersion) {
        return this.compareTo(otherVersion) !== -1;
    }
    /**
     * Compares if the current instance equals (by values) the specified file.
     *
     * @param otherVersion - The version to compare the current instance to
     * @returns A boolean with the result.
     */
    equals(otherVersion) {
        return this.compareTo(otherVersion) === 0;
    }
    /**
     * Compare the current instance with the specified file.
     *
     * @param otherVersion - The version to compare the current instance to
     * @returns 0 when the versions are equal,.
     *          1 when the current instance is greater,
     *          -1 when the current instance is smaller
     */
    compareTo(otherVersion) {
        if (otherVersion == null) {
            return 1;
        }
        else if (this.major !== otherVersion.major) {
            return this.major < otherVersion.major ? -1 : 1;
        }
        else if (this.minor !== otherVersion.minor) {
            return this.minor < otherVersion.minor ? -1 : 1;
        }
        else if (this.patch !== otherVersion.patch) {
            return this.patch < otherVersion.patch ? -1 : 1;
        }
        else if (this.revision !== otherVersion.revision) {
            return this.revision < otherVersion.revision ? -1 : 1;
        }
        return 0;
    }
    /**
     * Gets the file version string representation for this instance.
     */
    toString() {
        if (isNaN(this.major + this.minor + this.patch + this.revision))
            return "";
        return `${this.major}.${this.minor}.${this.patch}.${this.revision}`;
    }
}
//# sourceMappingURL=fileversion.js.map