"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 renamePortfolio_1 = require("../renamePortfolio");
const mocks_1 = require("../../../testUtils/mocks");
const types_2 = require("../../../types");
const utils_1 = require("../../../types/utils");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/NumberedPortfolio', require("../../../testUtils/mocks/entities").mockNumberedPortfolioModule('~/api/entities/NumberedPortfolio'));
describe('renamePortfolio procedure', () => {
    const id = new bignumber_js_1.default(1);
    const did = 'someDid';
    const identityId = mocks_1.dsMockUtils.createMockIdentityId(did);
    const rawPortfolioNumber = mocks_1.dsMockUtils.createMockU64(id.toNumber());
    let mockContext;
    let stringToIdentityIdStub;
    let numberToU64Stub;
    let textToStringStub;
    let stringToTextStub;
    let portfoliosStub;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        stringToIdentityIdStub = sinon_1.default.stub(utilsConversionModule, 'stringToIdentityId');
        numberToU64Stub = sinon_1.default.stub(utilsConversionModule, 'numberToU64');
        textToStringStub = sinon_1.default.stub(utilsConversionModule, 'textToString');
        stringToTextStub = sinon_1.default.stub(utilsConversionModule, 'stringToText');
    });
    beforeEach(() => {
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        stringToIdentityIdStub.withArgs(did, mockContext).returns(identityId);
        numberToU64Stub.withArgs(id, mockContext).returns(rawPortfolioNumber);
        mocks_1.entityMockUtils.configureMocks({
            numberedPortfolioOptions: {
                isOwnedBy: true,
            },
        });
        portfoliosStub = mocks_1.dsMockUtils.createQueryStub('portfolio', 'portfolios');
    });
    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 new name is the same as the current one', () => {
        const newName = 'newName';
        const rawNewName = mocks_1.dsMockUtils.createMockText(newName);
        textToStringStub.withArgs(rawNewName).returns(newName);
        portfoliosStub.resolves(rawNewName);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(renamePortfolio_1.prepareRenamePortfolio.call(proc, {
            id,
            did,
            name: newName,
        })).rejects.toThrow('New name is the same as current name');
    });
    test('should throw an error if there already is a portfolio with the new name', () => {
        const portfolioName = 'portfolioName';
        const rawPortfolioName = mocks_1.dsMockUtils.createMockText(portfolioName);
        const entryPortfolioName = 'someName';
        const rawEntryPortfolioName = mocks_1.dsMockUtils.createMockText(entryPortfolioName);
        textToStringStub.withArgs(rawPortfolioName).returns(portfolioName);
        textToStringStub.withArgs(rawEntryPortfolioName).returns(entryPortfolioName);
        portfoliosStub = mocks_1.dsMockUtils.createQueryStub('portfolio', 'portfolios', {
            entries: [utils_1.tuple([], rawEntryPortfolioName)],
        });
        portfoliosStub.resolves(rawPortfolioName);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(renamePortfolio_1.prepareRenamePortfolio.call(proc, {
            id,
            did,
            name: entryPortfolioName,
        })).rejects.toThrow('A portfolio with that name already exists');
    });
    test('should add a rename portfolio transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const portfolioName = 'portfolioName';
        const rawPortfolioName = mocks_1.dsMockUtils.createMockText(portfolioName);
        const newName = 'newName';
        const rawNewName = mocks_1.dsMockUtils.createMockText(newName);
        textToStringStub.withArgs(rawPortfolioName).returns(portfolioName);
        stringToTextStub.returns(rawNewName);
        portfoliosStub.resolves(rawPortfolioName);
        const transaction = mocks_1.dsMockUtils.createTxStub('portfolio', 'renamePortfolio');
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const result = yield renamePortfolio_1.prepareRenamePortfolio.call(proc, {
            id,
            did,
            name: newName,
        });
        const addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        sinon_1.default.assert.calledWith(addTransactionStub, transaction, {}, rawPortfolioNumber, rawNewName);
        expect(result.id).toBe(id);
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = renamePortfolio_1.getAuthorization.bind(proc);
            const args = {
                did,
                id,
            };
            const portfolio = mocks_1.entityMockUtils.getNumberedPortfolioInstance({ did, id });
            expect(boundFunc(args)).toEqual({
                roles: [{ type: types_2.RoleType.PortfolioCustodian, portfolioId: { did, number: id } }],
                permissions: {
                    tokens: [],
                    portfolios: [portfolio],
                    transactions: [types_1.TxTags.portfolio.RenamePortfolio],
                },
            });
        });
    });
});
//# sourceMappingURL=renamePortfolio.js.map