"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const web3_eth_abi_1 = __importDefault(require("web3-eth-abi"));
const getContract_1 = require("../../../../utils/solidity/getContract");
const Contracts_1 = require("../../../../Contracts");
const getRoutes_1 = require("../../hub/calls/getRoutes");
const getHub_1 = require("../../hub/calls/getHub");
const FunctionSignatures_1 = require("../utils/FunctionSignatures");
const isTakePermitted = (environment, tradingContractAddress, exchangeName, makerQuantity, takerQuantity, fillTakerQuantity, id) => __awaiter(this, void 0, void 0, function* () {
    const hubAddress = yield getHub_1.getHub(environment, tradingContractAddress);
    const { policyManagerAddress, tradingAddress } = yield getRoutes_1.getRoutes(environment, hubAddress);
    // TODO: Jenna said we need this later!
    //
    // const priceFeedContract = await getContract(
    //   Contracts.TestingPriceFeed,
    //   priceSourceAddress,
    //   environment,
    // );
    // const orderPrice = await priceFeedContract.methods.getOrderPriceInfo(
    //   makerQuantity.token.address,
    //   takerQuantity.token.address,
    //   makerQuantity.quantity,
    //   takerQuantity.quantity,
    // ).call();
    const policyManager = yield getContract_1.getContract(environment, Contracts_1.Contracts.PolicyManager, policyManagerAddress);
    const exchangeAddress = environment.deployment.exchangeConfigs[exchangeName].exchange;
    const result = yield policyManager.methods
        .preValidate(web3_eth_abi_1.default.encodeFunctionSignature(FunctionSignatures_1.FunctionSignatures.takeOrder), [
        '0x0000000000000000000000000000000000000000',
        tradingAddress.toString(),
        makerQuantity.token.address.toString(),
        takerQuantity.token.address.toString(),
        exchangeAddress.toString(),
    ], [
        makerQuantity.quantity.toString(),
        takerQuantity.quantity.toString(),
        fillTakerQuantity.quantity.toString(),
    ], id
        ? `0x${Number(id)
            .toString(16)
            .padStart(64, '0')}`
        : '0x0')
        .call();
    return !!result;
});
exports.isTakePermitted = isTakePermitted;
