import DatabaseProperty from "./Database/DatabaseProperty";
import BadDataException from "./Exception/BadDataException";
import { ObjectType } from "./JSON";
import Zod from "../Utils/Schema/Zod";
export default class Version extends DatabaseProperty {
    get version() {
        return this._version;
    }
    set version(v) {
        const re = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z\d][-a-zA-Z.\d]*)?(\+[a-zA-Z\d][-a-zA-Z.\d]*)?$/i;
        const isValid = re.test(v);
        if (!isValid) {
            throw new BadDataException("Version is not in valid format.");
        }
        this._version = v;
    }
    constructor(version) {
        super();
        this._version = "";
        this.version = version;
    }
    toString() {
        return this.version;
    }
    toJSON() {
        return {
            _type: ObjectType.Version,
            value: this.toString(),
        };
    }
    static fromJSON(json) {
        if (json["_type"] === ObjectType.Version) {
            return new Version(json["value"] || "");
        }
        throw new BadDataException("Invalid JSON: " + JSON.stringify(json));
    }
    static toDatabase(value) {
        if (value) {
            if (typeof value === "string") {
                value = new Version(value);
            }
            return value.toString();
        }
        return null;
    }
    static fromDatabase(_value) {
        if (_value) {
            return new Version(_value);
        }
        return null;
    }
    static getSchema() {
        return Zod.object({
            _type: Zod.literal(ObjectType.Version),
            value: Zod.string().openapi({
                type: "string",
                example: "1.0.0",
            }),
        }).openapi({
            type: "object",
            description: "Version object",
            example: { _type: ObjectType.Version, value: "1.0.0" },
        });
    }
}
//# sourceMappingURL=Version.js.map