"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 types_1 = require("../../../../../types");
const utils_1 = require("../../../../../types/utils");
const utilsConversionModule = __importStar(require("../../../../../utils/conversion"));
const __1 = require("../");
jest.mock('~/api/entities/Identity', require("../../../../../testUtils/mocks/entities").mockIdentityModule('~/api/entities/Identity'));
jest.mock('~/api/entities/SecurityToken', require("../../../../../testUtils/mocks/entities").mockSecurityTokenModule('~/api/entities/SecurityToken'));
jest.mock('~/base/Procedure', require("../../../../../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
describe('CorporateActions class', () => {
    let context;
    let ticker;
    let rawTicker;
    let token;
    let corporateActions;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        ticker = 'SOME_TICKER';
        sinon_1.default
            .stub(utilsConversionModule, 'stringToTicker')
            .withArgs(ticker, context)
            .returns(rawTicker);
    });
    beforeEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        context = mocks_1.dsMockUtils.getContextInstance();
        rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
        token = mocks_1.entityMockUtils.getSecurityTokenInstance();
        corporateActions = new __1.CorporateActions(token, context);
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.entityMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    test('should extend namespace', () => {
        expect(__1.CorporateActions.prototype instanceof internal_1.Namespace).toBe(true);
    });
    describe('method: setDefaults', () => {
        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 targets = {
                identities: ['someDid'],
                treatment: types_1.TargetTreatment.Exclude,
            };
            const defaultTaxWithholding = new bignumber_js_1.default(15);
            const taxWithholdings = [
                {
                    identity: 'someDid',
                    percentage: new bignumber_js_1.default(20),
                },
            ];
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({
                args: { ticker: 'SOME_TICKER', targets, taxWithholdings, defaultTaxWithholding },
                transformer: undefined,
            }, context)
                .resolves(expectedQueue);
            const queue = yield corporateActions.setDefaults({
                targets,
                taxWithholdings,
                defaultTaxWithholding,
            });
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: setAgent', () => {
        test('should prepare the procedure and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const target = 'someDid';
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: { ticker, target }, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield corporateActions.setAgent({ target });
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: removeAgent', () => {
        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 expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: { ticker: 'SOME_TICKER' }, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield corporateActions.removeAgent();
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: remove', () => {
        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 expectedQueue = 'someQueue';
            const corporateAction = new bignumber_js_1.default(100);
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: { corporateAction, ticker: 'SOME_TICKER' }, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield corporateActions.remove({ corporateAction });
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: getAgents', () => {
        test('should retrieve a list of agent identities', () => __awaiter(void 0, void 0, void 0, function* () {
            const did = 'someDid';
            const otherDid = 'otherDid';
            const fakeTicker = 'TEST';
            const identity = mocks_1.entityMockUtils.getIdentityInstance({ did });
            mocks_1.dsMockUtils.createQueryStub('externalAgents', 'groupOfAgent', {
                entries: [
                    utils_1.tuple([mocks_1.dsMockUtils.createMockTicker(fakeTicker), mocks_1.dsMockUtils.createMockIdentityId(did)], mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockAgentGroup('PolymeshV1Caa'))),
                    utils_1.tuple([mocks_1.dsMockUtils.createMockTicker(fakeTicker), mocks_1.dsMockUtils.createMockIdentityId(otherDid)], mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockAgentGroup('PolymeshV1Pia'))),
                ],
            });
            const result = yield corporateActions.getAgents();
            expect(result).toEqual([identity]);
        }));
    });
    describe('method: getDefaults', () => {
        test("should retrieve the Security Token's Corporate Actions defaults", () => __awaiter(void 0, void 0, void 0, function* () {
            const dids = ['someDid', 'otherDid'];
            const targets = {
                identities: [
                    mocks_1.entityMockUtils.getIdentityInstance({ did: dids[0] }),
                    mocks_1.entityMockUtils.getIdentityInstance({ did: dids[1] }),
                ],
                treatment: types_1.TargetTreatment.Include,
            };
            const defaultTaxWithholding = new bignumber_js_1.default(10);
            const taxWithholdings = [{ identity: targets.identities[0], percentage: new bignumber_js_1.default(15) }];
            mocks_1.dsMockUtils.createQueryStub('corporateAction', 'defaultTargetIdentities');
            mocks_1.dsMockUtils.createQueryStub('corporateAction', 'defaultWithholdingTax');
            mocks_1.dsMockUtils.createQueryStub('corporateAction', 'didWithholdingTax');
            mocks_1.dsMockUtils.getQueryMultiStub().resolves([
                mocks_1.dsMockUtils.createMockTargetIdentities({
                    identities: dids,
                    treatment: 'Include',
                }),
                mocks_1.dsMockUtils.createMockPermill(10 * 10000),
                [[mocks_1.dsMockUtils.createMockIdentityId(dids[0]), mocks_1.dsMockUtils.createMockPermill(15 * 10000)]],
            ]);
            const result = yield corporateActions.getDefaults();
            expect(result).toEqual({
                targets,
                defaultTaxWithholding,
                taxWithholdings,
            });
        }));
    });
});
//# sourceMappingURL=index.js.map