"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 modifyVenue_1 = require("../modifyVenue");
const mocks_1 = require("../../../testUtils/mocks");
const types_2 = require("../../../types");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/Venue', require("../../../testUtils/mocks/entities").mockVenueModule('~/api/entities/Venue'));
describe('modifyVenue procedure', () => {
    let mockContext;
    let addTransactionStub;
    let venueId;
    let venue;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        venueId = new bignumber_js_1.default(1);
    });
    beforeEach(() => {
        mocks_1.entityMockUtils.configureMocks();
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        venue = mocks_1.entityMockUtils.getVenueInstance({ id: venueId });
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.entityMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should throw an error if the supplied description is the same as the current one', () => {
        const description = 'someDetails';
        const args = {
            venue,
            description,
        };
        mocks_1.entityMockUtils.configureMocks({
            venueOptions: {
                details: {
                    description,
                },
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(modifyVenue_1.prepareModifyVenue.call(proc, args)).rejects.toThrow('New description is the same as the current one');
    });
    test('should throw an error if the supplied type is the same as the current one', () => {
        const type = types_2.VenueType.Exchange;
        const args = {
            venue,
            type,
        };
        mocks_1.entityMockUtils.configureMocks({
            venueOptions: {
                details: {
                    description: 'someDescription',
                    type,
                },
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(modifyVenue_1.prepareModifyVenue.call(proc, args)).rejects.toThrow('New type is the same as the current one');
    });
    test('should add an update venue transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const description = 'someDetails';
        const type = types_2.VenueType.Exchange;
        const rawDetails = mocks_1.dsMockUtils.createMockVenueDetails(description);
        const rawType = mocks_1.dsMockUtils.createMockVenueType(type);
        const rawId = mocks_1.dsMockUtils.createMockU64(venueId.toNumber());
        const args = {
            venue,
            description,
            type,
        };
        sinon_1.default.stub(utilsConversionModule, 'numberToU64').returns(rawId);
        sinon_1.default.stub(utilsConversionModule, 'stringToVenueDetails').returns(rawDetails);
        sinon_1.default.stub(utilsConversionModule, 'venueTypeToMeshVenueType').returns(rawType);
        const updateVenueDetailsTransaction = mocks_1.dsMockUtils.createTxStub('settlement', 'updateVenueDetails');
        const updateVenueTypeTransaction = mocks_1.dsMockUtils.createTxStub('settlement', 'updateVenueType');
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        yield modifyVenue_1.prepareModifyVenue.call(proc, args);
        sinon_1.default.assert.calledWith(addTransactionStub, updateVenueDetailsTransaction, {}, rawId, rawDetails);
        sinon_1.default.assert.calledWith(addTransactionStub, updateVenueTypeTransaction, {}, rawId, rawType);
        yield modifyVenue_1.prepareModifyVenue.call(proc, {
            venue,
            type,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, updateVenueTypeTransaction, {}, rawId, rawType);
        yield modifyVenue_1.prepareModifyVenue.call(proc, {
            venue,
            description,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, updateVenueDetailsTransaction, {}, rawId, rawDetails);
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = modifyVenue_1.getAuthorization.bind(proc);
            let args = {
                venue,
                type: types_2.VenueType.Distribution,
            };
            expect(boundFunc(args)).toEqual({
                roles: [{ type: types_2.RoleType.VenueOwner, venueId }],
                permissions: {
                    portfolios: [],
                    transactions: [types_1.TxTags.settlement.UpdateVenueType],
                    tokens: [],
                },
            });
            args = {
                venue,
                description: 'someDescription',
            };
            expect(boundFunc(args)).toEqual({
                roles: [{ type: types_2.RoleType.VenueOwner, venueId }],
                permissions: {
                    portfolios: [],
                    transactions: [types_1.TxTags.settlement.UpdateVenueDetails],
                    tokens: [],
                },
            });
        });
    });
});
//# sourceMappingURL=modifyVenue.js.map