"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 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'));
describe('TickerReservation class', () => {
    const ticker = 'FAKETICKER';
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
    });
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    test('should extend Entity', () => {
        expect(internal_1.TickerReservation.prototype instanceof internal_1.Entity).toBe(true);
    });
    describe('constructor', () => {
        test('should assign ticker to instance', () => {
            const context = mocks_1.dsMockUtils.getContextInstance();
            const tickerReservation = new internal_1.TickerReservation({ ticker }, context);
            expect(tickerReservation.ticker).toBe(ticker);
        });
    });
    describe('method: isUniqueIdentifiers', () => {
        test('should return true if the object conforms to the interface', () => {
            expect(internal_1.TickerReservation.isUniqueIdentifiers({ ticker: 'someTicker' })).toBe(true);
            expect(internal_1.TickerReservation.isUniqueIdentifiers({})).toBe(false);
            expect(internal_1.TickerReservation.isUniqueIdentifiers({ ticker: 3 })).toBe(false);
        });
    });
    describe('method: details', () => {
        let queryMultiStub;
        beforeEach(() => {
            mocks_1.dsMockUtils.createQueryStub('asset', 'tickers');
            mocks_1.dsMockUtils.createQueryStub('asset', 'tokens');
            queryMultiStub = mocks_1.dsMockUtils.getQueryMultiStub();
        });
        test('should return details for a free ticker', () => __awaiter(void 0, void 0, void 0, function* () {
            const context = mocks_1.dsMockUtils.getContextInstance();
            const tickerReservation = new internal_1.TickerReservation({ ticker }, context);
            queryMultiStub.resolves([
                mocks_1.dsMockUtils.createMockTickerRegistration(),
                mocks_1.dsMockUtils.createMockSecurityToken(),
            ]);
            const details = yield tickerReservation.details();
            expect(details).toMatchObject({
                owner: null,
                expiryDate: null,
                status: types_1.TickerReservationStatus.Free,
            });
        }));
        test('should return details for a reserved ticker', () => __awaiter(void 0, void 0, void 0, function* () {
            const ownerDid = 'someDid';
            const expiryDate = new Date(new Date().getTime() + 100000);
            const context = mocks_1.dsMockUtils.getContextInstance();
            const tickerReservation = new internal_1.TickerReservation({ ticker }, context);
            queryMultiStub.resolves([
                mocks_1.dsMockUtils.createMockTickerRegistration({
                    owner: mocks_1.dsMockUtils.createMockIdentityId(ownerDid),
                    expiry: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockMoment(expiryDate.getTime())),
                }),
                mocks_1.dsMockUtils.createMockSecurityToken(),
            ]);
            const details = yield tickerReservation.details();
            expect(details).toMatchObject({
                owner: { did: ownerDid },
                expiryDate,
                status: types_1.TickerReservationStatus.Reserved,
            });
        }));
        test('should return details for a permanently reserved ticker', () => __awaiter(void 0, void 0, void 0, function* () {
            const ownerDid = 'someDid';
            const expiryDate = null;
            const context = mocks_1.dsMockUtils.getContextInstance();
            const tickerReservation = new internal_1.TickerReservation({ ticker }, context);
            queryMultiStub.resolves([
                mocks_1.dsMockUtils.createMockTickerRegistration({
                    owner: mocks_1.dsMockUtils.createMockIdentityId(ownerDid),
                    expiry: mocks_1.dsMockUtils.createMockOption(), // null expiry
                }),
                mocks_1.dsMockUtils.createMockSecurityToken(),
            ]);
            const details = yield tickerReservation.details();
            expect(details).toMatchObject({
                owner: { did: ownerDid },
                expiryDate,
                status: types_1.TickerReservationStatus.Reserved,
            });
        }));
        test('should return details for a ticker for which a reservation expired', () => __awaiter(void 0, void 0, void 0, function* () {
            const ownerDid = 'someDid';
            const expiryDate = new Date(new Date().getTime() - 100000);
            const context = mocks_1.dsMockUtils.getContextInstance();
            const tickerReservation = new internal_1.TickerReservation({ ticker }, context);
            queryMultiStub.resolves([
                mocks_1.dsMockUtils.createMockTickerRegistration({
                    owner: mocks_1.dsMockUtils.createMockIdentityId(ownerDid),
                    expiry: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockMoment(expiryDate.getTime())),
                }),
                mocks_1.dsMockUtils.createMockSecurityToken(),
            ]);
            const details = yield tickerReservation.details();
            expect(details).toMatchObject({
                owner: { did: ownerDid },
                expiryDate,
                status: types_1.TickerReservationStatus.Free,
            });
        }));
        test('should return details for a ticker for which a token has already been created', () => __awaiter(void 0, void 0, void 0, function* () {
            const ownerDid = 'someDid';
            const expiryDate = null;
            const context = mocks_1.dsMockUtils.getContextInstance();
            const tickerReservation = new internal_1.TickerReservation({ ticker }, context);
            queryMultiStub.resolves([
                mocks_1.dsMockUtils.createMockTickerRegistration({
                    owner: mocks_1.dsMockUtils.createMockIdentityId(ownerDid),
                    expiry: mocks_1.dsMockUtils.createMockOption(),
                }),
                mocks_1.dsMockUtils.createMockSecurityToken({
                    /* eslint-disable @typescript-eslint/naming-convention */
                    owner_did: mocks_1.dsMockUtils.createMockIdentityId(ownerDid),
                    asset_type: mocks_1.dsMockUtils.createMockAssetType('EquityCommon'),
                    divisible: mocks_1.dsMockUtils.createMockBool(true),
                    total_supply: mocks_1.dsMockUtils.createMockBalance(1000),
                    /* eslint-enable @typescript-eslint/naming-convention */
                }),
            ]);
            const details = yield tickerReservation.details();
            expect(details).toMatchObject({
                owner: { did: ownerDid },
                expiryDate,
                status: types_1.TickerReservationStatus.TokenCreated,
            });
        }));
        test('should allow subscription', () => __awaiter(void 0, void 0, void 0, function* () {
            const context = mocks_1.dsMockUtils.getContextInstance();
            const tickerReservation = new internal_1.TickerReservation({ ticker }, context);
            const unsubCallback = 'unsubCallback';
            queryMultiStub.callsFake((_, cbFunc) => __awaiter(void 0, void 0, void 0, function* () {
                cbFunc([mocks_1.dsMockUtils.createMockTickerRegistration(), mocks_1.dsMockUtils.createMockSecurityToken()]);
                return unsubCallback;
            }));
            const callback = sinon_1.default.stub();
            const unsub = yield tickerReservation.details(callback);
            expect(unsub).toBe(unsubCallback);
            sinon_1.default.assert.calledWith(callback, {
                owner: null,
                expiryDate: null,
                status: types_1.TickerReservationStatus.Free,
            });
        }));
    });
    describe('method: extend', () => {
        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 tickerReservation = new internal_1.TickerReservation({ ticker }, context);
            const args = {
                ticker,
                extendPeriod: true,
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield tickerReservation.extend();
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: createToken', () => {
        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 tickerReservation = new internal_1.TickerReservation({ ticker }, context);
            const args = {
                ticker,
                name: 'TEST',
                totalSupply: new bignumber_js_1.default(100),
                isDivisible: true,
                tokenType: types_1.KnownTokenType.EquityCommon,
                tokenIdentifiers: [{ type: types_1.TokenIdentifierType.Isin, value: '12345' }],
                fundingRound: 'Series A',
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield tickerReservation.createToken(args);
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: transferOwnership', () => {
        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 tickerReservation = new internal_1.TickerReservation({ ticker }, context);
            const target = 'someOtherDid';
            const expiry = new Date('10/10/2022');
            const args = {
                ticker,
                target,
                expiry,
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield tickerReservation.transferOwnership(args);
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: exists', () => {
        test('should return whether the Reservation exists', () => __awaiter(void 0, void 0, void 0, function* () {
            const context = mocks_1.dsMockUtils.getContextInstance();
            const tickerRes = new internal_1.TickerReservation({ ticker: 'SOME_TICKER' }, context);
            mocks_1.dsMockUtils.createQueryStub('asset', 'tickers', {
                size: 10,
            });
            let result = yield tickerRes.exists();
            expect(result).toBe(true);
            mocks_1.dsMockUtils.createQueryStub('asset', 'tickers', {
                size: 0,
            });
            result = yield tickerRes.exists();
            expect(result).toBe(false);
        }));
    });
    describe('method: toJson', () => {
        test('should return a human readable version of the entity', () => {
            const context = mocks_1.dsMockUtils.getContextInstance();
            const tickerRes = new internal_1.TickerReservation({ ticker: 'SOME_TICKER' }, context);
            expect(tickerRes.toJson()).toBe('SOME_TICKER');
        });
    });
});
//# sourceMappingURL=index.js.map