"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 types_1 = require("../../../polkadot/types");
const sinon_1 = __importDefault(require("sinon"));
const transferTickerOwnership_1 = require("../transferTickerOwnership");
const mocks_1 = require("../../../testUtils/mocks");
const types_2 = require("../../../types");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/TickerReservation', require("../../../testUtils/mocks/entities").mockTickerReservationModule('~/api/entities/TickerReservation'));
describe('transferTickerOwnership procedure', () => {
    let mockContext;
    let signerValueToSignatoryStub;
    let authorizationToAuthorizationDataStub;
    let dateToMomentStub;
    let ticker;
    let did;
    let expiry;
    let rawSignatory;
    let rawAuthorizationData;
    let rawMoment;
    let args;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        signerValueToSignatoryStub = sinon_1.default.stub(utilsConversionModule, 'signerValueToSignatory');
        authorizationToAuthorizationDataStub = sinon_1.default.stub(utilsConversionModule, 'authorizationToAuthorizationData');
        dateToMomentStub = sinon_1.default.stub(utilsConversionModule, 'dateToMoment');
        ticker = 'someTicker';
        did = 'someOtherDid';
        expiry = new Date('10/14/3040');
        rawSignatory = mocks_1.dsMockUtils.createMockSignatory({
            Identity: mocks_1.dsMockUtils.createMockIdentityId(did),
        });
        rawAuthorizationData = mocks_1.dsMockUtils.createMockAuthorizationData({
            TransferTicker: mocks_1.dsMockUtils.createMockTicker(ticker),
        });
        rawMoment = mocks_1.dsMockUtils.createMockMoment(new bignumber_js_1.default(expiry.getTime()));
        args = {
            ticker,
            target: did,
        };
    });
    let addTransactionStub;
    let transaction;
    beforeEach(() => {
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        transaction = mocks_1.dsMockUtils.createTxStub('identity', 'addAuthorization');
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        signerValueToSignatoryStub
            .withArgs({ type: types_2.SignerType.Identity, value: did }, mockContext)
            .returns(rawSignatory);
        authorizationToAuthorizationDataStub
            .withArgs({ type: types_2.AuthorizationType.TransferTicker, value: ticker }, mockContext)
            .returns(rawAuthorizationData);
        dateToMomentStub.withArgs(expiry, mockContext).returns(rawMoment);
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.procedureMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should throw an error if an Asset with that ticker has already been launched', () => {
        mocks_1.entityMockUtils.configureMocks({
            tickerReservationOptions: {
                details: {
                    owner: mocks_1.entityMockUtils.getIdentityInstance(),
                    expiryDate: null,
                    status: types_2.TickerReservationStatus.AssetCreated,
                },
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(transferTickerOwnership_1.prepareTransferTickerOwnership.call(proc, args)).rejects.toThrow('An Asset with this ticker has already been created');
    });
    test('should add an add authorization transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        yield transferTickerOwnership_1.prepareTransferTickerOwnership.call(proc, args);
        sinon_1.default.assert.calledWith(addTransactionStub, sinon_1.default.match({
            transaction,
            resolvers: sinon_1.default.match.array,
            args: [rawSignatory, rawAuthorizationData, null],
        }));
    }));
    test('should add an add authorization transaction with expiry to the queue if an expiry date was passed', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        yield transferTickerOwnership_1.prepareTransferTickerOwnership.call(proc, Object.assign(Object.assign({}, args), { expiry }));
        sinon_1.default.assert.calledWith(addTransactionStub, sinon_1.default.match({
            transaction,
            resolvers: sinon_1.default.match.array,
            args: [rawSignatory, rawAuthorizationData, rawMoment],
        }));
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = transferTickerOwnership_1.getAuthorization.bind(proc);
            expect(boundFunc(args)).toEqual({
                roles: [{ type: types_2.RoleType.TickerOwner, ticker }],
                permissions: {
                    assets: [],
                    transactions: [types_1.TxTags.identity.AddAuthorization],
                    portfolios: [],
                },
            });
        });
    });
});
//# sourceMappingURL=transferTickerOwnership.js.map