"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Yup = __importStar(require("yup"));
const bignumber_js_1 = __importDefault(require("bignumber.js"));
class BigNumberSchema extends Yup.mixed {
    constructor() {
        super({ type: 'bigNumber' });
        this.transform((value) => {
            if (this.isType(value)) {
                return value;
            }
            try {
                return new bignumber_js_1.default(value);
            }
            catch (err) {
                return 'Invalid BigNumber';
            }
        });
    }
    _typeCheck(value) {
        if (value === null) {
            return true;
        }
        return value && (value.isBigNumber || value._isBigNumber);
    }
    min(min, message) {
        return this.test({
            message,
            name: 'min',
            exclusive: true,
            params: { min },
            test(value) {
                return (value === null || value.isGreaterThanOrEqualTo(this.resolve(min)));
            },
        });
    }
    max(max, message) {
        return this.test({
            message,
            name: 'max',
            exclusive: true,
            params: { max },
            test(value) {
                return value === null || value.isLessThanOrEqualTo(this.resolve(max));
            },
        });
    }
    minDigits(minDigits, message) {
        return this.test({
            message,
            name: ' minLength',
            exclusive: true,
            params: { minDigits },
            test(value) {
                return (value === null ||
                    value.toFormat().replace(/[.,]/g, '').length >=
                        this.resolve(minDigits));
            },
        });
    }
    maxDigits(maxDigits, message) {
        return this.test({
            message,
            name: ' maxLength',
            exclusive: true,
            params: { maxDigits },
            test(value) {
                return (value === null ||
                    value.toFormat().replace(/[.,]/g, '').length <=
                        this.resolve(maxDigits));
            },
        });
    }
    lessThan(less, message) {
        return this.test({
            message,
            name: 'lessThan',
            exclusive: true,
            params: { less },
            test(value) {
                return value === null || value.isLessThan(this.resolve(less));
            },
        });
    }
    moreThan(more, message) {
        return this.test({
            message,
            name: 'moreThan',
            exclusive: true,
            params: { more },
            test(value) {
                return value === null || value.isGreaterThan(this.resolve(more));
            },
        });
    }
    postive(message) {
        return this.moreThan(0, message);
    }
    negative(message) {
        return this.lessThan(0, message);
    }
    required(message) {
        return this.test({
            message,
            name: 'required',
            exclusive: true,
            test(value) {
                return value !== null;
            },
        });
    }
}
exports.BigNumberSchema = BigNumberSchema;
//# sourceMappingURL=BigNumberSchema.js.map