"use strict";
// SPDX-License-Identifier: Apache-2.0
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
require("../environmentMock");
const in_1 = require("../../../src/port/in");
const RegulationType_1 = require("../../../src/domain/context/factory/RegulationType");
const RPCTransactionAdapter_1 = require("../../../src/port/out/rpc/RPCTransactionAdapter");
const MirrorNodeAdapter_1 = require("../../../src/port/out/mirror/MirrorNodeAdapter");
const NetworkService_1 = __importDefault(require("../../../src/app/service/network/NetworkService"));
const RPCQueryAdapter_1 = require("../../../src/port/out/rpc/RPCQueryAdapter");
const config_1 = require("../../config");
const Injectable_1 = __importDefault(require("../../../src/core/injectable/Injectable"));
const Account_1 = __importDefault(require("../../../src/domain/context/account/Account"));
const Management_1 = __importDefault(require("../../../src/port/in/management/Management"));
const ethers_1 = require("ethers");
in_1.SDK.log = { level: "ERROR", transports: new in_1.LoggerTransports.Console() };
const decimals = 0;
const name = "TEST_SECURITY_TOKEN";
const symbol = "TEST";
const isin = "ABCDE123456Z";
const votingRight = true;
const informationRight = false;
const liquidationRight = true;
const subscriptionRight = false;
const conversionRight = true;
const redemptionRight = false;
const putRight = true;
const dividendRight = 1;
const currency = "0x345678";
const numberOfShares = 0;
const nominalValue = 1000;
const nominalValueDecimals = 3;
const regulationType = RegulationType_1.RegulationType.REG_D;
const regulationSubType = RegulationType_1.RegulationSubType.B_506;
const countries = "AF,HG,BN";
const info = "Anything";
const configId = "0x0000000000000000000000000000000000000000000000000000000000000000";
const configVersion = 1;
const mirrorNode = {
    name: "testmirrorNode",
    baseUrl: "https://testnet.mirrornode.hedera.com/api/v1/",
};
const rpcNode = {
    name: "testrpcNode",
    baseUrl: "http://127.0.0.1:7546/api",
};
let th;
let mirrorNodeAdapter;
describe("🧪 Management tests", () => {
    let ns;
    let rpcQueryAdapter;
    let equity;
    const url = "http://127.0.0.1:7546";
    const customHttpProvider = new ethers_1.ethers.JsonRpcProvider(url);
    beforeAll(async () => {
        try {
            mirrorNodeAdapter = Injectable_1.default.resolve(MirrorNodeAdapter_1.MirrorNodeAdapter);
            mirrorNodeAdapter.set(mirrorNode);
            th = Injectable_1.default.resolve(RPCTransactionAdapter_1.RPCTransactionAdapter);
            ns = Injectable_1.default.resolve(NetworkService_1.default);
            rpcQueryAdapter = Injectable_1.default.resolve(RPCQueryAdapter_1.RPCQueryAdapter);
            rpcQueryAdapter.init();
            ns.environment = "testnet";
            ns.configuration = {
                factoryAddress: config_1.FACTORY_ADDRESS,
                resolverAddress: config_1.RESOLVER_ADDRESS,
            };
            ns.mirrorNode = mirrorNode;
            ns.rpcNode = rpcNode;
            await th.init(true);
            const account = new Account_1.default({
                id: config_1.CLIENT_ACCOUNT_ECDSA.id.toString(),
                evmAddress: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress,
                alias: config_1.CLIENT_ACCOUNT_ECDSA.alias,
                privateKey: config_1.CLIENT_ACCOUNT_ECDSA.privateKey,
                publicKey: config_1.CLIENT_ACCOUNT_ECDSA.publicKey,
            });
            await th.register(account, true);
            th.setSignerOrProvider(new ethers_1.Wallet(config_1.CLIENT_ACCOUNT_ECDSA.privateKey?.key ?? "", customHttpProvider));
            const requestST = new in_1.CreateEquityRequest({
                name,
                symbol,
                isin,
                decimals,
                isWhiteList: false,
                erc20VotesActivated: false,
                isControllable: true,
                arePartitionsProtected: false,
                clearingActive: false,
                internalKycActivated: true,
                isMultiPartition: false,
                diamondOwnerAccount: config_1.CLIENT_ACCOUNT_ECDSA.id.toString(),
                votingRight,
                informationRight,
                liquidationRight,
                subscriptionRight,
                conversionRight,
                redemptionRight,
                putRight,
                dividendRight,
                currency,
                numberOfShares: numberOfShares.toString(),
                nominalValue: nominalValue.toString(),
                nominalValueDecimals: nominalValueDecimals,
                regulationType: RegulationType_1.CastRegulationType.toNumber(regulationType),
                regulationSubType: RegulationType_1.CastRegulationSubType.toNumber(regulationSubType),
                isCountryControlListWhiteList: true,
                countries,
                info,
                configId,
                configVersion,
            });
            equity = (await in_1.Equity.create(requestST)).security;
        }
        catch (error) {
            console.error("Error in beforeAll setup:", error);
        }
    }, 900_000);
    function checkConfig(resolver, configId, configVersion, configInfo) {
        expect(configInfo.resolverAddress).toEqual(resolver);
        expect(configInfo.configId).toEqual(configId);
        expect(configInfo.configVersion).toEqual(configVersion);
        return true;
    }
    it("Fetches configInfo successfully", async () => {
        const res = await Management_1.default.getConfigInfo(new in_1.GetConfigInfoRequest({
            securityId: equity.evmDiamondAddress.toString(),
        }));
        expect(checkConfig(config_1.RESOLVER_ADDRESS, configId, configVersion, res)).toBe(true);
    }, 600_000);
    it("Updates configVersion correctly", async () => {
        const newConfigVersion = 2;
        const request = new in_1.UpdateConfigVersionRequest({
            configVersion: newConfigVersion,
            securityId: equity.evmDiamondAddress,
        });
        const res = await Management_1.default.updateConfigVersion(request);
        const configInfo = await Management_1.default.getConfigInfo(new in_1.GetConfigInfoRequest({
            securityId: equity.evmDiamondAddress.toString(),
        }));
        checkConfig(config_1.RESOLVER_ADDRESS, configId, newConfigVersion, configInfo);
        expect(res.payload).toBe(true);
    }, 600_000);
    it("Updates config correctly", async () => {
        const configVersion = 2;
        const newConfigId = "0x0000000000000000000000000000000000000000000000000000000000000003";
        const request = new in_1.UpdateConfigRequest({
            configId: newConfigId,
            configVersion: configVersion,
            securityId: equity.evmDiamondAddress,
        });
        const res = await Management_1.default.updateConfig(request);
        const configInfo = await Management_1.default.getConfigInfo(new in_1.GetConfigInfoRequest({
            securityId: equity.evmDiamondAddress.toString(),
        }));
        checkConfig(config_1.RESOLVER_ADDRESS, newConfigId, configVersion, configInfo);
        expect(res.payload).toBe(true);
    }, 600_000);
    it("Updates resolver correctly", async () => {
        const configVersion = 3;
        const newResolver = "0.0.2166987";
        const request = new in_1.UpdateResolverRequest({
            configVersion: configVersion,
            configId,
            securityId: equity.evmDiamondAddress.toString(),
            resolver: newResolver,
        });
        const res = await Management_1.default.updateResolver(request);
        const configInfo = await Management_1.default.getConfigInfo(new in_1.GetConfigInfoRequest({
            securityId: equity.evmDiamondAddress.toString(),
        }));
        checkConfig(newResolver, configId, configVersion, configInfo);
        expect(res.payload).toBe(true);
    }, 600_000);
});
