"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 types_1 = require("../../../polkadot/types");
const sinon_1 = __importDefault(require("sinon"));
const modifyAsset_1 = require("../modifyAsset");
const mocks_1 = require("../../../testUtils/mocks");
const types_2 = require("../../../types");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/Asset', require("../../../testUtils/mocks/entities").mockAssetModule('~/api/entities/Asset'));
describe('modifyAsset procedure', () => {
    let mockContext;
    let stringToTickerStub;
    let stringToAssetNameStub;
    let stringToFundingRoundNameStub;
    let ticker;
    let rawTicker;
    let fundingRound;
    let identifiers;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        stringToTickerStub = sinon_1.default.stub(utilsConversionModule, 'stringToTicker');
        stringToAssetNameStub = sinon_1.default.stub(utilsConversionModule, 'stringToAssetName');
        stringToFundingRoundNameStub = sinon_1.default.stub(utilsConversionModule, 'stringToFundingRoundName');
        ticker = 'someTicker';
        rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
        fundingRound = 'Series A';
        identifiers = [
            {
                type: types_2.SecurityIdentifierType.Isin,
                value: 'someValue',
            },
        ];
    });
    let addTransactionStub;
    beforeEach(() => {
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        stringToTickerStub.withArgs(ticker, mockContext).returns(rawTicker);
    });
    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 the user has not passed any arguments', () => {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(modifyAsset_1.prepareModifyAsset.call(proc, {})).rejects.toThrow('Nothing to modify');
    });
    test('should throw an error if makeDivisible is set to true and the Asset is already divisible', () => {
        mocks_1.entityMockUtils.configureMocks({
            assetOptions: {
                details: { isDivisible: true },
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(modifyAsset_1.prepareModifyAsset.call(proc, {
            ticker,
            makeDivisible: true,
        })).rejects.toThrow('The Asset is already divisible');
    });
    test('should throw an error if newName is the same name currently in the Asset', () => {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(modifyAsset_1.prepareModifyAsset.call(proc, {
            ticker,
            name: 'ASSET_NAME',
        })).rejects.toThrow('New name is the same as current name');
    });
    test('should throw an error if newFundingRound is the same funding round name currently in the Asset', () => {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(modifyAsset_1.prepareModifyAsset.call(proc, {
            ticker,
            fundingRound,
        })).rejects.toThrow('New funding round name is the same as current funding round');
    });
    test('should throw an error if newIdentifiers are the same identifiers currently in the Asset', () => {
        mocks_1.entityMockUtils.configureMocks({
            assetOptions: {
                getIdentifiers: identifiers,
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(modifyAsset_1.prepareModifyAsset.call(proc, {
            ticker,
            identifiers,
        })).rejects.toThrow('New identifiers are the same as current identifiers');
    });
    test('should add a make divisible transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const transaction = mocks_1.dsMockUtils.createTxStub('asset', 'makeDivisible');
        const result = yield modifyAsset_1.prepareModifyAsset.call(proc, {
            ticker,
            makeDivisible: true,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, sinon_1.default.match({ transaction, args: [rawTicker] }));
        expect(result.ticker).toBe(ticker);
    }));
    test('should add a rename Asset transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const newName = 'NEW_NAME';
        const rawAssetName = mocks_1.dsMockUtils.createMockAssetName(newName);
        stringToAssetNameStub.withArgs(newName, mockContext).returns(rawAssetName);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const transaction = mocks_1.dsMockUtils.createTxStub('asset', 'renameAsset');
        const result = yield modifyAsset_1.prepareModifyAsset.call(proc, {
            ticker,
            name: newName,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, { transaction, args: [rawTicker, rawAssetName] });
        expect(result.ticker).toBe(ticker);
    }));
    test('should add a set funding round transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const newFundingRound = 'Series B';
        const rawFundingRound = mocks_1.dsMockUtils.createMockFundingRoundName(newFundingRound);
        stringToFundingRoundNameStub.withArgs(newFundingRound, mockContext).returns(rawFundingRound);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const transaction = mocks_1.dsMockUtils.createTxStub('asset', 'setFundingRound');
        const result = yield modifyAsset_1.prepareModifyAsset.call(proc, {
            ticker,
            fundingRound: newFundingRound,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, {
            transaction,
            args: [rawTicker, rawFundingRound],
        });
        expect(result.ticker).toBe(ticker);
    }));
    test('should add a update identifiers transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const rawIdentifier = mocks_1.dsMockUtils.createMockAssetIdentifier({
            Isin: mocks_1.dsMockUtils.createMockU8aFixed(identifiers[0].value),
        });
        sinon_1.default.stub(utilsConversionModule, 'securityIdentifierToAssetIdentifier').returns(rawIdentifier);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const transaction = mocks_1.dsMockUtils.createTxStub('asset', 'updateIdentifiers');
        const result = yield modifyAsset_1.prepareModifyAsset.call(proc, {
            ticker,
            identifiers,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, {
            transaction,
            args: [rawTicker, [rawIdentifier]],
        });
        expect(result.ticker).toBe(ticker);
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = modifyAsset_1.getAuthorization.bind(proc);
            const name = 'NEW NAME';
            const args = {
                ticker,
            };
            expect(boundFunc(args)).toEqual({
                permissions: {
                    transactions: [],
                    portfolios: [],
                    assets: [expect.objectContaining({ ticker })],
                },
            });
            expect(boundFunc(Object.assign(Object.assign({}, args), { makeDivisible: true, name, fundingRound, identifiers }))).toEqual({
                permissions: {
                    transactions: [
                        types_1.TxTags.asset.MakeDivisible,
                        types_1.TxTags.asset.RenameAsset,
                        types_1.TxTags.asset.SetFundingRound,
                        types_1.TxTags.asset.UpdateIdentifiers,
                    ],
                    portfolios: [],
                    assets: [expect.objectContaining({ ticker })],
                },
            });
        });
    });
});
//# sourceMappingURL=modifyAsset.js.map