"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 internal_1 = require("../../../../internal");
const mocks_1 = require("../../../../testUtils/mocks");
const utils_1 = require("../../../../types/utils");
const utilsConversionModule = __importStar(require("../../../../utils/conversion"));
const utilsInternalModule = __importStar(require("../../../../utils/internal"));
const Portfolios_1 = require("../Portfolios");
jest.mock('~/api/entities/NumberedPortfolio', require("../../../../testUtils/mocks/entities").mockNumberedPortfolioModule('~/api/entities/NumberedPortfolio'));
jest.mock('~/base/Procedure', require("../../../../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
describe('Portfolios class', () => {
    const did = 'someDid';
    const rawIdentityId = mocks_1.dsMockUtils.createMockIdentityId(did);
    const numberedPortfolioId = new bignumber_js_1.default(1);
    const rawNumberedPortfolioId = mocks_1.dsMockUtils.createMockU64(numberedPortfolioId);
    let mockContext;
    let stringToIdentityIdStub;
    let u64ToBigNumberStub;
    let portfolios;
    let identity;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        stringToIdentityIdStub = sinon_1.default.stub(utilsConversionModule, 'stringToIdentityId');
        u64ToBigNumberStub = sinon_1.default.stub(utilsConversionModule, 'u64ToBigNumber');
    });
    beforeEach(() => {
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        identity = new internal_1.Identity({ did }, mockContext);
        portfolios = new Portfolios_1.Portfolios(identity, mockContext);
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.dsMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    test('should extend namespace', () => {
        expect(Portfolios_1.Portfolios.prototype instanceof internal_1.Namespace).toBe(true);
    });
    describe('method: getPortfolios', () => {
        test('should retrieve all the portfolios for the identity', () => __awaiter(void 0, void 0, void 0, function* () {
            mocks_1.dsMockUtils.createQueryStub('portfolio', 'portfolios', {
                entries: [
                    utils_1.tuple([mocks_1.dsMockUtils.createMockIdentityId(did), rawNumberedPortfolioId], mocks_1.dsMockUtils.createMockBytes()),
                ],
            });
            stringToIdentityIdStub.withArgs(did, mockContext).returns(rawIdentityId);
            u64ToBigNumberStub.returns(numberedPortfolioId);
            const result = yield portfolios.getPortfolios();
            expect(result).toHaveLength(2);
            expect(result[0] instanceof internal_1.DefaultPortfolio).toBe(true);
            expect(result[1] instanceof internal_1.NumberedPortfolio).toBe(true);
            expect(result[0].owner.did).toEqual(did);
            expect(result[1].id).toEqual(numberedPortfolioId);
        }));
    });
    describe('method: getCustodiedPortfolios', () => {
        test('should retrieve all the Portfolios custodied by the Identity', () => __awaiter(void 0, void 0, void 0, function* () {
            mocks_1.dsMockUtils.createQueryStub('portfolio', 'portfoliosInCustody');
            const entries = [
                utils_1.tuple({
                    args: [
                        rawIdentityId,
                        mocks_1.dsMockUtils.createMockPortfolioId({
                            did: rawIdentityId,
                            kind: mocks_1.dsMockUtils.createMockPortfolioKind('Default'),
                        }),
                    ],
                }, mocks_1.dsMockUtils.createMockBool(true)),
                utils_1.tuple({
                    args: [
                        rawIdentityId,
                        mocks_1.dsMockUtils.createMockPortfolioId({
                            did: rawIdentityId,
                            kind: mocks_1.dsMockUtils.createMockPortfolioKind({ User: rawNumberedPortfolioId }),
                        }),
                    ],
                }, mocks_1.dsMockUtils.createMockBool(true)),
            ];
            sinon_1.default.stub(utilsInternalModule, 'requestPaginated').resolves({ entries, lastKey: null });
            stringToIdentityIdStub.withArgs(did, mockContext).returns(rawIdentityId);
            u64ToBigNumberStub.returns(numberedPortfolioId);
            const { data } = yield portfolios.getCustodiedPortfolios();
            expect(data).toHaveLength(2);
            expect(data[0] instanceof internal_1.DefaultPortfolio).toBe(true);
            expect(data[1] instanceof internal_1.NumberedPortfolio).toBe(true);
            expect(data[0].owner.did).toEqual(did);
            expect(data[1].id).toEqual(numberedPortfolioId);
        }));
    });
    describe('method: getPortfolio', () => {
        test('should return the default portfolio for the current identity', () => __awaiter(void 0, void 0, void 0, function* () {
            const result = yield portfolios.getPortfolio();
            expect(result instanceof internal_1.DefaultPortfolio).toBe(true);
            expect(result.owner.did).toEqual(did);
        }));
        test('should return a numbered portfolio', () => __awaiter(void 0, void 0, void 0, function* () {
            const portfolioId = new bignumber_js_1.default(1);
            const result = yield portfolios.getPortfolio({ portfolioId });
            expect(result instanceof internal_1.NumberedPortfolio).toBe(true);
            expect(result.id).toEqual(portfolioId);
        }));
        test("should throw an error if a numbered portfolio doesn't exist", () => {
            const portfolioId = new bignumber_js_1.default(1);
            mocks_1.entityMockUtils.configureMocks({
                numberedPortfolioOptions: {
                    exists: false,
                },
            });
            return expect(portfolios.getPortfolio({ portfolioId })).rejects.toThrow("The Portfolio doesn't exist");
        });
    });
    describe('method: delete', () => {
        test('should prepare the procedure and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const portfolioId = new bignumber_js_1.default(5);
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: { id: portfolioId, did }, transformer: undefined }, mockContext)
                .resolves(expectedQueue);
            let queue = yield portfolios.delete({ portfolio: portfolioId });
            expect(queue).toBe(expectedQueue);
            queue = yield portfolios.delete({
                portfolio: new internal_1.NumberedPortfolio({ id: portfolioId, did }, mockContext),
            });
            expect(queue).toBe(expectedQueue);
        }));
    });
});
//# sourceMappingURL=Portfolios.js.map