"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 ethers_1 = require("ethers");
const SsiManagement_1 = __importDefault(require("../../../src/port/in/ssiManagement/SsiManagement"));
const SecurityRole_1 = require("../../../src/domain/context/security/SecurityRole");
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("🧪 SSI 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);
        }
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._SSI_MANAGER_ROLE,
        }));
    }, 900_000);
    it("Set and Get revocation registry successfully", async () => {
        expect((await SsiManagement_1.default.setRevocationRegistryAddress(new in_1.SetRevocationRegistryAddressRequest({
            securityId: equity.evmDiamondAddress,
            revocationRegistryId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).payload).toBe(true);
        expect(await SsiManagement_1.default.getRevocationRegistryAddress(new in_1.GetRevocationRegistryAddressRequest({
            securityId: equity.evmDiamondAddress,
        }))).toEqual(config_1.CLIENT_ACCOUNT_ECDSA_A.id.toString());
    }, 600_000);
    it("Issuer functionality work successfully", async () => {
        //Add issuer
        expect((await SsiManagement_1.default.addIssuer(new in_1.AddIssuerRequest({
            securityId: equity.evmDiamondAddress,
            issuerId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).payload).toBe(true);
        //Get Issuer List
        expect(await SsiManagement_1.default.getIssuerListCount(new in_1.GetIssuerListCountRequest({
            securityId: equity.evmDiamondAddress,
        }))).toEqual(1);
        //Get Issuer List Members
        expect(await SsiManagement_1.default.getIssuerListMembers(new in_1.GetIssuerListMembersRequest({
            securityId: equity.evmDiamondAddress,
            start: 0,
            end: 1,
        }))).toContain(config_1.CLIENT_ACCOUNT_ECDSA_A.id.toString());
        //Remove Issuer
        expect((await SsiManagement_1.default.removeIssuer(new in_1.RemoveIssuerRequest({
            securityId: equity.evmDiamondAddress,
            issuerId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).payload).toBe(true);
        //Get Issuer List
        expect(await SsiManagement_1.default.getIssuerListCount(new in_1.GetIssuerListCountRequest({
            securityId: equity.evmDiamondAddress,
        }))).toEqual(0);
    }, 600_000);
    it("Should return an error if try to add the same issuer", async () => {
        //Add issuer
        expect((await SsiManagement_1.default.addIssuer(new in_1.AddIssuerRequest({
            securityId: equity.evmDiamondAddress,
            issuerId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).payload).toBe(true);
        //Try to do Add the same issuer
        let thrownError;
        try {
            await SsiManagement_1.default.addIssuer(new in_1.AddIssuerRequest({
                securityId: equity.evmDiamondAddress,
                issuerId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            }));
        }
        catch (error) {
            thrownError = error;
        }
        expect(thrownError).toBeInstanceOf(Error);
    }, 600_000);
});
