"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const bignumber_js_1 = require("bignumber.js");
const contractWrapperFactory_1 = require("../contractWrapperFactory");
const utils_1 = require("../utils");
const web3EventService_1 = require("../web3EventService");
const wrapperService_1 = require("../wrapperService");
const erc20_1 = require("./erc20");
const locking4Reputation_1 = require("./locking4Reputation");
class LockingToken4ReputationWrapper extends locking4Reputation_1.Locking4ReputationWrapper {
    constructor() {
        super(...arguments);
        this.name = "LockingToken4Reputation";
        this.friendlyName = "Locking Token For Reputation";
        this.factory = exports.LockingToken4ReputationFactory;
    }
    initialize(options) {
        const _super = name => super[name];
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield _super("_initialize").call(this, options);
            if (!options.priceOracleContract) {
                throw new Error(`priceOracleContract not supplied`);
            }
            this.logContractFunctionCall("LockingToken4Reputation.initialize", options);
            return this.wrapTransactionInvocation("LockingToken4Reputation.initialize", options, this.contract.initialize, [options.avatarAddress,
                options.reputationReward,
                options.lockingStartTime.getTime() / 1000,
                options.lockingEndTime.getTime() / 1000,
                options.redeemEnableTime.getTime() / 1000,
                options.maxLockingPeriod,
                options.priceOracleContract]);
        });
    }
    release(options) {
        const _super = name => super[name];
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield _super("_release").call(this, options);
            this.logContractFunctionCall("LockingToken4Reputation.release", options);
            return this.wrapTransactionInvocation("LockingToken4Reputation.release", options, this.contract.release, [options.lockerAddress, options.lockId]);
        });
    }
    /**
     * Returns reason why can't lock, else null if can lock
     */
    getLockBlocker(options) {
        const _super = name => super[name];
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const msg = yield _super("getLockBlocker").call(this, options);
            if (msg) {
                return msg;
            }
            if (!options.tokenAddress) {
                return "tokenAddress was not supplied";
            }
            const token = yield erc20_1.Erc20Factory.at(options.tokenAddress);
            const balance = yield utils_1.Utils.getTokenBalance(options.lockerAddress, token.address);
            const amount = new bignumber_js_1.default(options.amount);
            if (balance.lt(amount)) {
                return "the account has insufficient balance";
            }
            return null;
        });
    }
    lock(options) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const msg = yield this.getLockBlocker(options);
            if (msg) {
                throw new Error(msg);
            }
            this.logContractFunctionCall("LockingToken4Reputation.lock", options);
            return this.wrapTransactionInvocation("LockingToken4Reputation.lock", options, this.contract.lock, [options.amount, options.period, options.tokenAddress], { from: options.lockerAddress });
        });
    }
    getTokenForLock(lockingId) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.logContractFunctionCall("LockingToken4Reputation.lockedTokens");
            const address = yield this.contract.lockedTokens(lockingId);
            return wrapperService_1.WrapperService.factories.Erc20.at(address);
        });
    }
    getPriceOracleAddress() {
        this.logContractFunctionCall("LockingToken4Reputation.priceOracleContract");
        return this.contract.priceOracleContract();
    }
}
exports.LockingToken4ReputationWrapper = LockingToken4ReputationWrapper;
class LockingToken4ReputationType extends contractWrapperFactory_1.ContractWrapperFactory {
    deployed() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            throw new Error("LockingToken4Reputation has not been deployed");
        });
    }
}
exports.LockingToken4ReputationType = LockingToken4ReputationType;
exports.LockingToken4ReputationFactory = new LockingToken4ReputationType("LockingToken4Reputation", LockingToken4ReputationWrapper, new web3EventService_1.Web3EventService());
//# sourceMappingURL=lockingToken4Reputation.js.map