"use strict";
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 AccountManagement_1 = require("../AccountManagement");
const internal_1 = require("../internal");
const mocks_1 = require("../testUtils/mocks");
const types_1 = require("../types");
jest.mock('~/base/Procedure', require("../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
jest.mock('~/api/entities/Account', require("../testUtils/mocks/entities").mockAccountModule('~/api/entities/Account'));
describe('AccountManagement class', () => {
    let context;
    let accountManagement;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
    });
    beforeEach(() => {
        context = mocks_1.dsMockUtils.getContextInstance();
        accountManagement = new AccountManagement_1.AccountManagement(context);
    });
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.entityMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    describe('method: removeSecondaryAccounts', () => {
        test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const accounts = [mocks_1.entityMockUtils.getAccountInstance({ address: 'someAccount' })];
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: { accounts }, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield accountManagement.removeSecondaryAccounts({ accounts });
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: revokePermissions', () => {
        test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const account = mocks_1.entityMockUtils.getAccountInstance({ address: 'someAccount' });
            const secondaryAccounts = [
                {
                    account,
                    permissions: {
                        tokens: { type: types_1.PermissionType.Include, values: [] },
                        transactions: { type: types_1.PermissionType.Include, values: [] },
                        portfolios: { type: types_1.PermissionType.Include, values: [] },
                    },
                },
            ];
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: { secondaryAccounts }, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield accountManagement.revokePermissions({ secondaryAccounts: [account] });
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: modifyPermissions', () => {
        test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const secondaryAccounts = [
                {
                    account: mocks_1.entityMockUtils.getAccountInstance({ address: 'someAccount' }),
                    permissions: { tokens: null, transactions: null, portfolios: null },
                },
            ];
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: { secondaryAccounts }, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield accountManagement.modifyPermissions({ secondaryAccounts });
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: inviteAccount', () => {
        test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const args = {
                targetAccount: 'someAccount',
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield accountManagement.inviteAccount(args);
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: freezeSecondaryAccounts', () => {
        test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const args = {
                freeze: true,
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield accountManagement.freezeSecondaryAccounts();
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: unfreezeSecondaryAccounts', () => {
        test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const args = {
                freeze: false,
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield accountManagement.unfreezeSecondaryAccounts();
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: getAccount', () => {
        test('should return an Account object with the passed address', () => __awaiter(void 0, void 0, void 0, function* () {
            const params = { address: 'testAddress' };
            const result = accountManagement.getAccount(params);
            expect(result.address).toBe(params.address);
        }));
        test('should return the current Account if no address is passed', () => __awaiter(void 0, void 0, void 0, function* () {
            const address = 'someAddress';
            mocks_1.dsMockUtils.configureMocks({ contextOptions: { currentPairAddress: address } });
            const result = accountManagement.getAccount();
            expect(result.address).toBe(address);
        }));
    });
    describe('method: getAccountBalance', () => {
        const fakeBalance = {
            free: new bignumber_js_1.default(100),
            locked: new bignumber_js_1.default(0),
            total: new bignumber_js_1.default(100),
        };
        test('should return the free and locked POLYX balance of the current Account', () => __awaiter(void 0, void 0, void 0, function* () {
            mocks_1.dsMockUtils.configureMocks({ contextOptions: { balance: fakeBalance } });
            const result = yield accountManagement.getAccountBalance();
            expect(result).toEqual(fakeBalance);
        }));
        test('should return the free and locked POLYX balance of the supplied account', () => __awaiter(void 0, void 0, void 0, function* () {
            mocks_1.entityMockUtils.configureMocks({ accountOptions: { getBalance: fakeBalance } });
            let result = yield accountManagement.getAccountBalance({ account: 'someId' });
            expect(result).toEqual(fakeBalance);
            result = yield accountManagement.getAccountBalance({
                account: new internal_1.Account({ address: 'someId ' }, mocks_1.dsMockUtils.getContextInstance()),
            });
            expect(result).toEqual(fakeBalance);
        }));
        test('should allow subscription (with and without a supplied account id)', () => __awaiter(void 0, void 0, void 0, function* () {
            const unsubCallback = 'unsubCallback';
            mocks_1.dsMockUtils.configureMocks({ contextOptions: { balance: fakeBalance } });
            mocks_1.entityMockUtils.configureMocks({ accountOptions: { getBalance: fakeBalance } });
            let accountBalanceStub = mocks_1.dsMockUtils.getContextInstance().getCurrentAccount().getBalance.resolves(unsubCallback);
            const callback = (() => 1);
            let result = yield accountManagement.getAccountBalance(callback);
            expect(result).toEqual(unsubCallback);
            sinon_1.default.assert.calledWithExactly(accountBalanceStub, callback);
            accountBalanceStub = mocks_1.entityMockUtils.getAccountGetBalanceStub().resolves(unsubCallback);
            const account = 'someId';
            result = yield accountManagement.getAccountBalance({ account }, callback);
            expect(result).toEqual(unsubCallback);
            sinon_1.default.assert.calledWithExactly(accountBalanceStub, callback);
        }));
    });
    describe('method: getAccounts', () => {
        test('should return the list of signer accounts associated to the SDK', () => __awaiter(void 0, void 0, void 0, function* () {
            const accounts = [mocks_1.entityMockUtils.getAccountInstance()];
            mocks_1.dsMockUtils.configureMocks({
                contextOptions: {
                    getAccounts: accounts,
                },
            });
            const result = accountManagement.getAccounts();
            expect(result).toEqual(accounts);
        }));
    });
});
//# sourceMappingURL=AccountManagement.js.map