"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 queries_1 = require("../../../../middleware/queries");
const mocks_1 = require("../../../../testUtils/mocks");
const types_1 = require("../../../../types");
const utilsConversionModule = __importStar(require("../../../../utils/conversion"));
jest.mock('~/api/entities/Identity', require("../../../../testUtils/mocks/entities").mockIdentityModule('~/api/entities/Identity'));
jest.mock('~/api/entities/Asset', require("../../../../testUtils/mocks/entities").mockAssetModule('~/api/entities/Asset'));
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('~/api/entities/Venue', require("../../../../testUtils/mocks/entities").mockVenueModule('~/api/entities/Venue'));
jest.mock('~/base/Procedure', require("../../../../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
describe('Offering class', () => {
    let context;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
    });
    beforeEach(() => {
        context = mocks_1.dsMockUtils.getContextInstance();
    });
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    test('should extend Entity', () => {
        expect(internal_1.Offering.prototype instanceof internal_1.Entity).toBe(true);
    });
    describe('constructor', () => {
        test('should assign ticker and id to instance', () => {
            const ticker = 'SOME_TICKER';
            const id = new bignumber_js_1.default(1);
            const offering = new internal_1.Offering({ id, ticker }, context);
            expect(offering.asset.ticker).toBe(ticker);
            expect(offering.id).toEqual(id);
        });
    });
    describe('method: isUniqueIdentifiers', () => {
        test('should return true if the object conforms to the interface', () => {
            expect(internal_1.Offering.isUniqueIdentifiers({ id: new bignumber_js_1.default(1), ticker: 'symbol' })).toBe(true);
            expect(internal_1.Offering.isUniqueIdentifiers({})).toBe(false);
            expect(internal_1.Offering.isUniqueIdentifiers({ id: new bignumber_js_1.default(1) })).toBe(false);
            expect(internal_1.Offering.isUniqueIdentifiers({ id: 1 })).toBe(false);
        });
    });
    describe('method: details', () => {
        const ticker = 'FAKETICKER';
        const id = new bignumber_js_1.default(1);
        const someDid = 'someDid';
        const name = 'someSto';
        const otherDid = 'otherDid';
        const raisingCurrency = 'USD';
        const amount = new bignumber_js_1.default(1000);
        const price = new bignumber_js_1.default(100);
        const remaining = new bignumber_js_1.default(700);
        const date = new Date();
        const minInvestmentValue = new bignumber_js_1.default(1);
        const rawFundraiser = mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockFundraiser({
            /* eslint-disable @typescript-eslint/naming-convention */
            creator: mocks_1.dsMockUtils.createMockIdentityId(someDid),
            offering_portfolio: mocks_1.dsMockUtils.createMockPortfolioId({
                did: mocks_1.dsMockUtils.createMockIdentityId(someDid),
                kind: mocks_1.dsMockUtils.createMockPortfolioKind('Default'),
            }),
            offering_asset: mocks_1.dsMockUtils.createMockTicker(ticker),
            raising_portfolio: mocks_1.dsMockUtils.createMockPortfolioId({
                did: mocks_1.dsMockUtils.createMockIdentityId(otherDid),
                kind: mocks_1.dsMockUtils.createMockPortfolioKind({
                    User: mocks_1.dsMockUtils.createMockU64(new bignumber_js_1.default(1)),
                }),
            }),
            raising_asset: mocks_1.dsMockUtils.createMockTicker(raisingCurrency),
            tiers: [
                mocks_1.dsMockUtils.createMockFundraiserTier({
                    total: mocks_1.dsMockUtils.createMockBalance(amount),
                    price: mocks_1.dsMockUtils.createMockBalance(price),
                    remaining: mocks_1.dsMockUtils.createMockBalance(remaining),
                }),
            ],
            venue_id: mocks_1.dsMockUtils.createMockU64(new bignumber_js_1.default(1)),
            start: mocks_1.dsMockUtils.createMockMoment(new bignumber_js_1.default(date.getTime())),
            end: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockMoment(new bignumber_js_1.default(date.getTime()))),
            status: mocks_1.dsMockUtils.createMockFundraiserStatus('Live'),
            minimum_investment: mocks_1.dsMockUtils.createMockBalance(minInvestmentValue),
            /* eslint-enable @typescript-eslint/naming-convention */
        }));
        const rawName = mocks_1.dsMockUtils.createMockFundraiserName(name);
        let offering;
        beforeEach(() => {
            context = mocks_1.dsMockUtils.getContextInstance();
            offering = new internal_1.Offering({ ticker, id }, context);
        });
        test('should return details for an Asset offering', () => __awaiter(void 0, void 0, void 0, function* () {
            const fakeResult = {
                creator: expect.objectContaining({ did: someDid }),
                name,
                offeringPortfolio: expect.objectContaining({
                    owner: expect.objectContaining({ did: someDid }),
                }),
                raisingPortfolio: expect.objectContaining({
                    owner: expect.objectContaining({ did: otherDid }),
                    id: new bignumber_js_1.default(1),
                }),
                raisingCurrency,
                tiers: [
                    {
                        amount: amount.shiftedBy(-6),
                        price: price.shiftedBy(-6),
                        remaining: remaining.shiftedBy(-6),
                    },
                ],
                venue: expect.objectContaining({ id: new bignumber_js_1.default(1) }),
                start: date,
                end: date,
                status: {
                    sale: types_1.OfferingSaleStatus.Live,
                    timing: types_1.OfferingTimingStatus.Expired,
                    balance: types_1.OfferingBalanceStatus.Residual,
                },
                minInvestment: minInvestmentValue.shiftedBy(-6),
                totalAmount: amount.shiftedBy(-6),
                totalRemaining: remaining.shiftedBy(-6),
            };
            mocks_1.dsMockUtils.createQueryStub('sto', 'fundraisers', {
                returnValue: rawFundraiser,
            });
            mocks_1.dsMockUtils.createQueryStub('sto', 'fundraiserNames', {
                returnValue: rawName,
            });
            const details = yield offering.details();
            expect(details).toEqual(fakeResult);
        }));
        test('should allow subscription', () => __awaiter(void 0, void 0, void 0, function* () {
            const unsubCallback = 'unsubCallBack';
            mocks_1.dsMockUtils.createQueryStub('sto', 'fundraiserNames', {
                returnValue: rawName,
            });
            mocks_1.dsMockUtils.createQueryStub('sto', 'fundraisers').callsFake((_a, _b, cbFunc) => __awaiter(void 0, void 0, void 0, function* () {
                cbFunc(rawFundraiser, rawName);
                return unsubCallback;
            }));
            const callback = sinon_1.default.stub();
            const result = yield offering.details(callback);
            sinon_1.default
                .stub(utilsConversionModule, 'fundraiserToOfferingDetails')
                .returns({});
            expect(result).toBe(unsubCallback);
            sinon_1.default.assert.calledWithExactly(callback, sinon_1.default.match({}));
        }));
    });
    describe('method: close', () => {
        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 ticker = 'SOME_TICKER';
            const id = new bignumber_js_1.default(1);
            const offering = new internal_1.Offering({ id, ticker }, context);
            const args = {
                ticker,
                id,
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield offering.close();
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: modifyTimes', () => {
        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 ticker = 'SOME_TICKER';
            const id = new bignumber_js_1.default(1);
            const offering = new internal_1.Offering({ id, ticker }, context);
            const now = new Date();
            const start = new Date(now.getTime() + 100000);
            const end = new Date(start.getTime() + 100000);
            const args = {
                ticker,
                id,
                start,
                end,
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield offering.modifyTimes({
                start,
                end,
            });
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: getInvestments', () => {
        test('should return a list of investors', () => __awaiter(void 0, void 0, void 0, function* () {
            const ticker = 'SOME_TICKER';
            const id = new bignumber_js_1.default(1);
            const offering = new internal_1.Offering({ id, ticker }, context);
            const did = 'someDid';
            const offeringToken = 'TICKER';
            const raiseToken = 'USD';
            const offeringTokenAmount = new bignumber_js_1.default(10000);
            const raiseTokenAmount = new bignumber_js_1.default(1000);
            const items = [
                {
                    investor: did,
                    offeringToken,
                    raiseToken,
                    offeringTokenAmount: offeringTokenAmount.toNumber(),
                    raiseTokenAmount: raiseTokenAmount.toNumber(),
                    datetime: '2020-10-10',
                },
            ];
            /* eslint-disable @typescript-eslint/naming-convention */
            const investmentQueryResponse = {
                totalCount: 1,
                items,
            };
            /* eslint-enabled @typescript-eslint/naming-convention */
            mocks_1.dsMockUtils.createApolloQueryStub(queries_1.heartbeat(), true);
            mocks_1.dsMockUtils.createApolloQueryStub(queries_1.investments({
                stoId: id.toNumber(),
                ticker,
                count: 5,
                skip: 0,
            }), {
                investments: investmentQueryResponse,
            });
            let result = yield offering.getInvestments({
                size: new bignumber_js_1.default(5),
                start: new bignumber_js_1.default(0),
            });
            const { data } = result;
            expect(data[0].investor.did).toBe(did);
            expect(data[0].soldAmount).toEqual(offeringTokenAmount.div(Math.pow(10, 6)));
            expect(data[0].investedAmount).toEqual(raiseTokenAmount.div(Math.pow(10, 6)));
            mocks_1.dsMockUtils.createApolloQueryStub(queries_1.investments({
                stoId: id.toNumber(),
                ticker,
                count: undefined,
                skip: undefined,
            }), {
                investments: {
                    totalCount: 0,
                    items: [],
                },
            });
            result = yield offering.getInvestments();
            expect(result.data).toEqual([]);
            expect(result.next).toBeNull();
        }));
    });
    describe('method: freeze', () => {
        test('should prepare the procedure and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const ticker = 'SOME_TICKER';
            const id = new bignumber_js_1.default(1);
            const offering = new internal_1.Offering({ id, ticker }, context);
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: { ticker, id, freeze: true }, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield offering.freeze();
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: unfreeze', () => {
        test('should prepare the procedure and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const ticker = 'SOME_TICKER';
            const id = new bignumber_js_1.default(1);
            const offering = new internal_1.Offering({ id, ticker }, context);
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: { ticker, id, freeze: false }, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield offering.unfreeze();
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: invest', () => {
        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 ticker = 'SOME_TICKER';
            const id = new bignumber_js_1.default(1);
            const offering = new internal_1.Offering({ id, ticker }, context);
            const did = 'someDid';
            const purchasePortfolio = mocks_1.entityMockUtils.getDefaultPortfolioInstance({ did });
            const fundingPortfolio = mocks_1.entityMockUtils.getDefaultPortfolioInstance({ did });
            const purchaseAmount = new bignumber_js_1.default(10);
            const args = {
                ticker,
                id,
                purchasePortfolio,
                fundingPortfolio,
                purchaseAmount,
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield offering.invest({
                purchasePortfolio,
                fundingPortfolio,
                purchaseAmount,
            });
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: exists', () => {
        test('should return whether the Offering exists', () => __awaiter(void 0, void 0, void 0, function* () {
            const offering = new internal_1.Offering({ ticker: 'SOME_TICKER', id: new bignumber_js_1.default(1) }, context);
            mocks_1.dsMockUtils.createQueryStub('sto', 'fundraisers', {
                returnValue: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockFundraiser()),
            });
            let result = yield offering.exists();
            expect(result).toBe(true);
            mocks_1.dsMockUtils.createQueryStub('sto', 'fundraisers', {
                returnValue: mocks_1.dsMockUtils.createMockOption(),
            });
            result = yield offering.exists();
            expect(result).toBe(false);
        }));
    });
    describe('method: toJson', () => {
        test('should return a human readable version of the entity', () => {
            const offering = new internal_1.Offering({ ticker: 'SOME_TICKER', id: new bignumber_js_1.default(1) }, context);
            expect(offering.toJson()).toEqual({
                id: '1',
                ticker: 'SOME_TICKER',
            });
        });
    });
});
//# sourceMappingURL=index.js.map