"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 utilsConversionModule = __importStar(require("../../../../../utils/conversion"));
const Distributions_1 = require("../Distributions");
jest.mock('~/api/entities/DividendDistribution', require("../../../../../testUtils/mocks/entities").mockDividendDistributionModule('~/api/entities/DividendDistribution'));
jest.mock('~/api/entities/DefaultPortfolio', require("../../../../../testUtils/mocks/entities").mockDefaultPortfolioModule('~/api/entities/DefaultPortfolio'));
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('Distributions class', () => {
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
    });
    beforeEach(() => {
        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();
    });
    test('should extend namespace', () => {
        expect(Distributions_1.Distributions.prototype instanceof internal_1.Namespace).toBe(true);
    });
    describe('method: configureDividendDistribution', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        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 context = mocks_1.dsMockUtils.getContextInstance();
            const token = mocks_1.entityMockUtils.getSecurityTokenInstance();
            const distributions = new Distributions_1.Distributions(token, context);
            const args = { foo: 'bar' };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: Object.assign({ ticker: token.ticker }, args), transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield distributions.configureDividendDistribution(args);
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: getOne', () => {
        beforeAll(() => {
            sinon_1.default.stub(utilsConversionModule, 'stringToTicker');
            sinon_1.default.stub(utilsConversionModule, 'numberToU32');
        });
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should return the requested Distribution', () => __awaiter(void 0, void 0, void 0, function* () {
            const ticker = 'SOME_TICKER';
            const id = new bignumber_js_1.default(1);
            mocks_1.dsMockUtils.createQueryStub('corporateAction', 'corporateActions', {
                returnValue: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockCorporateAction({
                    kind: 'PredictableBenefit',
                    targets: {
                        identities: ['someDid'],
                        treatment: 'Exclude',
                    },
                    /* eslint-disable @typescript-eslint/naming-convention */
                    decl_date: 0,
                    record_date: null,
                    default_withholding_tax: 3,
                    withholding_tax: [],
                    /* eslint-enable @typescript-eslint/naming-convention */
                })),
            });
            mocks_1.dsMockUtils.createQueryStub('corporateAction', 'details', {
                returnValue: mocks_1.dsMockUtils.createMockText('something'),
            });
            mocks_1.dsMockUtils.createQueryStub('capitalDistribution', 'distributions', {
                returnValue: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockDistribution({
                    from: { did: 'someDid', kind: 'Default' },
                    currency: 'CLP',
                    /* eslint-disable @typescript-eslint/naming-convention */
                    per_share: 1000000000,
                    amount: 100000000000,
                    remaining: 5000000000,
                    reclaimed: false,
                    payment_at: 10000000000,
                    expires_at: null,
                    /* eslint-enable @typescript-eslint/naming-convention */
                })),
            });
            const context = mocks_1.dsMockUtils.getContextInstance();
            const token = mocks_1.entityMockUtils.getSecurityTokenInstance({ ticker });
            const target = new Distributions_1.Distributions(token, context);
            const { distribution, details } = yield target.getOne({ id });
            expect(distribution.id).toEqual(id);
            expect(distribution.token.ticker).toBe(ticker);
            expect(distribution instanceof internal_1.DividendDistribution).toBe(true);
            expect(details.fundsReclaimed).toBe(false);
            expect(details.remainingFunds).toEqual(new bignumber_js_1.default(5000));
        }));
        test('should throw an error if the Distribution does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
            const ticker = 'SOME_TICKER';
            const id = new bignumber_js_1.default(1);
            mocks_1.dsMockUtils.createQueryStub('corporateAction', 'corporateActions', {
                returnValue: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockCorporateAction({
                    kind: 'PredictableBenefit',
                    targets: {
                        identities: ['someDid'],
                        treatment: 'Exclude',
                    },
                    /* eslint-disable @typescript-eslint/naming-convention */
                    decl_date: 0,
                    record_date: null,
                    default_withholding_tax: 3,
                    withholding_tax: [],
                    /* eslint-enable @typescript-eslint/naming-convention */
                })),
            });
            mocks_1.dsMockUtils.createQueryStub('corporateAction', 'details', {
                returnValue: mocks_1.dsMockUtils.createMockText('something'),
            });
            mocks_1.dsMockUtils.createQueryStub('capitalDistribution', 'distributions', {
                returnValue: mocks_1.dsMockUtils.createMockOption(),
            });
            const context = mocks_1.dsMockUtils.getContextInstance();
            const token = mocks_1.entityMockUtils.getSecurityTokenInstance({ ticker });
            const target = new Distributions_1.Distributions(token, context);
            yield expect(target.getOne({ id })).rejects.toThrow('The Dividend Distribution does not exist');
            mocks_1.dsMockUtils.createQueryStub('corporateAction', 'corporateActions', {
                returnValue: mocks_1.dsMockUtils.createMockOption(),
            });
            mocks_1.dsMockUtils.createQueryStub('capitalDistribution', 'distributions', {
                returnValue: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockDistribution({
                    from: { did: 'someDid', kind: 'Default' },
                    currency: 'CLP',
                    /* eslint-disable @typescript-eslint/naming-convention */
                    per_share: 1000,
                    amount: 100000,
                    remaining: 5000,
                    reclaimed: false,
                    payment_at: 100000000,
                    expires_at: null,
                    /* eslint-enable @typescript-eslint/naming-convention */
                })),
            });
            return expect(target.getOne({ id })).rejects.toThrow('The Dividend Distribution does not exist');
        }));
    });
    describe('method: get', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should return all distributions associated to the token', () => __awaiter(void 0, void 0, void 0, function* () {
            const ticker = 'SOME_TICKER';
            const context = mocks_1.dsMockUtils.getContextInstance();
            const token = mocks_1.entityMockUtils.getSecurityTokenInstance({ ticker });
            context.getDividendDistributionsForTokens
                .withArgs({ tokens: [token] })
                .resolves('distributions');
            const target = new Distributions_1.Distributions(token, context);
            const result = yield target.get();
            expect(result).toBe('distributions');
        }));
    });
});
//# sourceMappingURL=Distributions.js.map