"use strict";
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useInputState = void 0;
var utils_1 = require("../utils");
var react_1 = require("react");
var useInputState = function () {
    var _a = __read((0, react_1.useState)(''), 2), amountStr = _a[0], setAmountStr = _a[1];
    var _b = __read((0, react_1.useState)((0, utils_1.toBN)(0)), 2), amountBN = _b[0], setAmountBN = _b[1];
    var _c = __read((0, react_1.useState)(false), 2), amountIsMax = _c[0], setAmountIsMax = _c[1];
    function setMax(amt, dec) {
        setAmount(amt, dec);
        setAmountIsMax(true);
    }
    function handleEmpty() {
        setAmountStr('');
        setAmountBN((0, utils_1.toBN)(0));
        setAmountIsMax(false);
    }
    function resetInput() {
        handleEmpty();
    }
    function setAmount(amt, dec) {
        if (dec == null)
            throw new Error('Cant use contract decimals');
        if (amt === '')
            return handleEmpty();
        var amtStr = typeof amt === 'string' ? amt : (0, utils_1.formatUnits)(amt, dec);
        var amtBN = typeof amt === 'string' ? (0, utils_1.safeParseUnits)(amt, dec) : amt;
        setAmountBN(amtBN);
        setAmountStr(amtStr);
        setAmountIsMax(false);
    }
    function setAmountWithMax(amtStr, dec, maxBN) {
        if (dec == null)
            throw new Error('Cant use contract decimals');
        // maxDecimals prevents users from inputting numbers with
        // more decimal precision than contract.decimals().
        if (amtStr === '')
            return handleEmpty();
        var desiredAmount = (0, utils_1.safeParseUnits)(amtStr, dec);
        if (desiredAmount.gt(maxBN)) {
            var maxStr = (0, utils_1.formatUnits)(maxBN, dec);
            setAmountBN(maxBN);
            setAmountStr(maxStr);
            setAmountIsMax(true);
        }
        else {
            setAmountBN(desiredAmount);
            setAmountStr((0, utils_1.maxDecimals)(amtStr, dec));
            setAmountIsMax(false);
        }
    }
    return {
        amountIsMax: amountIsMax,
        amountStr: amountStr,
        amountBN: amountBN,
        setAmountWithMax: setAmountWithMax,
        setAmount: setAmount,
        setMax: setMax,
        resetInput: resetInput,
    };
};
exports.useInputState = useInputState;
//# sourceMappingURL=input.js.map