import BadDataException from "./Exception/BadDataException";
import { ObjectType } from "./JSON";
import Typeof from "./Typeof";
export default class PositiveNumber {
    get positiveNumber() {
        return this._positiveNumber;
    }
    set positiveNumber(v) {
        this._positiveNumber = v;
    }
    constructor(positiveNumber) {
        this._positiveNumber = 0;
        if (typeof positiveNumber === Typeof.String) {
            positiveNumber = Number.parseInt(positiveNumber.toString().trim(), 10);
            if (isNaN(positiveNumber)) {
                throw new BadDataException(`Invalid number: ${positiveNumber}`);
            }
        }
        if (positiveNumber < 0) {
            throw new BadDataException("positiveNumber cannot be less than 0");
        }
        this.positiveNumber = positiveNumber;
    }
    toString() {
        return this.positiveNumber.toString();
    }
    isZero() {
        return this.positiveNumber === 0;
    }
    isOne() {
        return this.positiveNumber === 1;
    }
    toNumber() {
        return this.positiveNumber;
    }
    toJSON() {
        return {
            _type: ObjectType.PositiveNumber,
            value: this.toNumber(),
        };
    }
    static fromJSON(json) {
        if (json instanceof PositiveNumber) {
            return json;
        }
        if (typeof json === Typeof.Number) {
            return new PositiveNumber(json);
        }
        if (typeof json === Typeof.String) {
            return new PositiveNumber(json);
        }
        if (!json || json["_type"] !== ObjectType.PositiveNumber) {
            throw new BadDataException("Invalid PositiveNumber");
        }
        return new PositiveNumber(json["value"]);
    }
}
//# sourceMappingURL=PositiveNumber.js.map