"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(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 bignumber_js_1 = __importDefault(require("bignumber.js"));
const sinon_1 = __importDefault(require("sinon"));
const subsidizeAccount_1 = require("../subsidizeAccount");
const internal_1 = require("../../../internal");
const mocks_1 = require("../../../testUtils/mocks");
const types_1 = require("../../../types");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/Account', require("../../../testUtils/mocks/entities").mockAccountModule('~/api/entities/Account'));
describe('subsidizeAccount procedure', () => {
    let mockContext;
    let addTransactionStub;
    let signerToStringStub;
    let stringToAccountIdStub;
    let bigNumberToBalanceStub;
    let args;
    const authId = new bignumber_js_1.default(1);
    const address = 'beneficiary';
    const allowance = new bignumber_js_1.default(1000);
    let beneficiary;
    let rawBeneficiaryAccount;
    let rawAllowance;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        signerToStringStub = sinon_1.default.stub(utilsConversionModule, 'signerToString');
        stringToAccountIdStub = sinon_1.default.stub(utilsConversionModule, 'stringToAccountId');
        bigNumberToBalanceStub = sinon_1.default.stub(utilsConversionModule, 'bigNumberToBalance');
    });
    beforeEach(() => {
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        args = { beneficiary: address, allowance };
        beneficiary = mocks_1.entityMockUtils.getAccountInstance({ address });
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.procedureMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should throw an error if the subsidizer has already sent a pending authorization to beneficiary Account with the same allowance to accept', () => {
        const sentAuthorizations = {
            data: [
                new internal_1.AuthorizationRequest({
                    target: beneficiary,
                    issuer: mocks_1.entityMockUtils.getIdentityInstance(),
                    authId,
                    expiry: null,
                    data: {
                        type: types_1.AuthorizationType.AddRelayerPayingKey,
                        value: {
                            beneficiary,
                            subsidizer: mocks_1.entityMockUtils.getAccountInstance(),
                            allowance: new bignumber_js_1.default(1000),
                        },
                    },
                }, mockContext),
            ],
            next: null,
            count: new bignumber_js_1.default(1),
        };
        mocks_1.dsMockUtils.configureMocks({
            contextOptions: {
                sentAuthorizations,
            },
        });
        signerToStringStub.withArgs(beneficiary).returns(address);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        expect(subsidizeAccount_1.prepareSubsidizeAccount.call(proc, args)).rejects.toThrow('The Beneficiary Account already has a pending invitation to add this account as a subsidizer');
    });
    test('should add an add authorization transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const mockBeneficiary = mocks_1.entityMockUtils.getAccountInstance({ address: 'mockAddress' });
        const issuer = mocks_1.entityMockUtils.getIdentityInstance();
        const subsidizer = mocks_1.entityMockUtils.getAccountInstance();
        const sentAuthorizations = {
            data: [
                new internal_1.AuthorizationRequest({
                    target: mockBeneficiary,
                    issuer,
                    authId,
                    expiry: null,
                    data: {
                        type: types_1.AuthorizationType.AddRelayerPayingKey,
                        value: {
                            beneficiary: mockBeneficiary,
                            subsidizer,
                            allowance: new bignumber_js_1.default(100),
                        },
                    },
                }, mockContext),
                new internal_1.AuthorizationRequest({
                    target: beneficiary,
                    issuer: mocks_1.entityMockUtils.getIdentityInstance(),
                    authId,
                    expiry: null,
                    data: {
                        type: types_1.AuthorizationType.AddRelayerPayingKey,
                        value: {
                            beneficiary,
                            subsidizer,
                            allowance: new bignumber_js_1.default(100),
                        },
                    },
                }, mockContext),
            ],
            next: null,
            count: new bignumber_js_1.default(2),
        };
        mocks_1.dsMockUtils.configureMocks({
            contextOptions: {
                sentAuthorizations,
            },
        });
        rawBeneficiaryAccount = mocks_1.dsMockUtils.createMockAccountId(address);
        rawAllowance = mocks_1.dsMockUtils.createMockBalance(allowance);
        stringToAccountIdStub.withArgs(address, mockContext).returns(rawBeneficiaryAccount);
        bigNumberToBalanceStub.withArgs(allowance, mockContext).returns(rawAllowance);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const transaction = mocks_1.dsMockUtils.createTxStub('relayer', 'setPayingKey');
        yield subsidizeAccount_1.prepareSubsidizeAccount.call(proc, Object.assign(Object.assign({}, args), { beneficiary }));
        sinon_1.default.assert.calledWith(addTransactionStub, sinon_1.default.match({
            transaction,
            resolvers: sinon_1.default.match.array,
            args: [rawBeneficiaryAccount, rawAllowance],
        }));
    }));
});
//# sourceMappingURL=subsidizeAccount.js.map