/**
 * Copyright © Volker Schukai and all contributing authors, {{copyRightYear}}. All rights reserved.
 * Node module: @schukai/monster
 *
 * This source code is licensed under the GNU Affero General Public License version 3 (AGPLv3).
 * The full text of the license can be found at: https://www.gnu.org/licenses/agpl-3.0.en.html
 *
 * For those who do not wish to adhere to the AGPLv3, a commercial license is available.
 * Acquiring a commercial license allows you to use this software without complying with the AGPLv3 terms.
 * For more information about purchasing a commercial license, please contact Volker Schukai.
 *
 * SPDX-License-Identifier: AGPL-3.0
 */

import { Base } from "./base.mjs";
import { instanceSymbol } from "../constants.mjs";

export { Version, getMonsterVersion };

/**
 * The version object contains a semantic version number
 *
 * @externalExample ../../example/types/version-1.mjs
 * @license AGPLv3
 * @since 1.0.0
 * @author Volker Schukai
 * @copyright Volker Schukai
 * @summary The version object contains a semantic version number
 */
class Version extends Base {
	/**
	 *
	 * @param major
	 * @param minor
	 * @param patch
	 * @throws {Error} major is not a number
	 * @throws {Error} minor is not a number
	 * @throws {Error} patch is not a number
	 */
	constructor(major, minor, patch) {
		super();

		if (
			typeof major === "string" &&
			minor === undefined &&
			patch === undefined
		) {
			const parts = major.toString().split(".");
			major = parseInt(parts[0] || 0);
			minor = parseInt(parts[1] || 0);
			patch = parseInt(parts[2] || 0);
		}

		if (major === undefined) {
			throw new Error("major version is undefined");
		}

		if (minor === undefined) {
			minor = 0;
		}

		if (patch === undefined) {
			patch = 0;
		}

		this.major = parseInt(major);
		this.minor = parseInt(minor);
		this.patch = parseInt(patch);

		if (isNaN(this.major)) {
			throw new Error("major is not a number");
		}

		if (isNaN(this.minor)) {
			throw new Error("minor is not a number");
		}

		if (isNaN(this.patch)) {
			throw new Error("patch is not a number");
		}
	}

	/**
	 * This method is called by the `instanceof` operator.
	 * @return {symbol}
	 * @since 2.1.0
	 */
	static get [instanceSymbol]() {
		return Symbol.for("@schukai/monster/types/version");
	}

	/**
	 *
	 * @return {string}
	 */
	toString() {
		return `${this.major}.${this.minor}.${this.patch}`;
	}

	/**
	 * returns 0 if equal, -1 if the object version is less and 1 if greater
	 * than the compared version
	 *
	 * @param {string|Version} version Version to compare
	 * @return {number}
	 */
	compareTo(version) {
		if (version instanceof Version) {
			version = version.toString();
		}

		if (typeof version !== "string") {
			throw new Error("type exception");
		}

		if (version === this.toString()) {
			return 0;
		}

		const a = [this.major, this.minor, this.patch];
		const b = version.split(".");
		const len = Math.max(a.length, b.length);

		for (let i = 0; i < len; i += 1) {
			if (
				(a[i] && !b[i] && parseInt(a[i]) > 0) ||
				parseInt(a[i]) > parseInt(b[i])
			) {
				return 1;
			} else if (
				(b[i] && !a[i] && parseInt(b[i]) > 0) ||
				parseInt(a[i]) < parseInt(b[i])
			) {
				return -1;
			}
		}

		return 0;
	}
}

let monsterVersion;

/**
 * Version of monster
 *
 * @return {Version}
 * @license AGPLv3
 * @since 1.0.0
 * @copyright Volker Schukai
 * @author Volker Schukai
 */
function getMonsterVersion() {
	if (monsterVersion instanceof Version) {
		return monsterVersion;
	}

	/** don't touch, replaced by make with package.json version */
	monsterVersion = new Version("4.137.5");

	return monsterVersion;
}
