"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 config_1 = require("../../config");
const ConnectRequest_1 = __importDefault(require("../../../src/port/in/request/network/ConnectRequest"));
const ethers_1 = require("ethers");
const RPCTransactionAdapter_1 = require("../../../src/port/out/rpc/RPCTransactionAdapter");
const NetworkService_1 = __importDefault(require("../../../src/app/service/network/NetworkService"));
const MirrorNodeAdapter_1 = require("../../../src/port/out/mirror/MirrorNodeAdapter");
const RPCQueryAdapter_1 = require("../../../src/port/out/rpc/RPCQueryAdapter");
const Injectable_1 = __importDefault(require("../../../src/core/injectable/Injectable"));
const SecurityRole_1 = require("../../../src/domain/context/security/SecurityRole");
const RegulationType_1 = require("../../../src/domain/context/factory/RegulationType");
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 = "0x858368";
const numberOfShares = 200000;
const nominalValue = 1000;
const nominalValueDecimals = 3;
const regulationType = RegulationType_1.RegulationType.REG_D;
const regulationSubType = RegulationType_1.RegulationSubType.C_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",
};
describe("🧪 Dividend test", () => {
    let th;
    let ns;
    let mirrorNodeAdapter;
    let rpcQueryAdapter;
    let equity;
    beforeAll(async () => {
        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);
        await th.register(undefined, true);
        const url = "http://127.0.0.1:7546";
        const customHttpProvider = new ethers_1.ethers.JsonRpcProvider(url);
        th.setSignerOrProvider(new ethers_1.Wallet(config_1.CLIENT_ACCOUNT_ECDSA.privateKey?.key ?? "", customHttpProvider));
        await in_1.Network.connect(new ConnectRequest_1.default({
            account: {
                accountId: config_1.CLIENT_ACCOUNT_ECDSA.id.toString(),
                privateKey: config_1.CLIENT_ACCOUNT_ECDSA.privateKey,
                evmAddress: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress?.toString(),
            },
            network: "testnet",
            wallet: in_1.SupportedWallets.METAMASK,
            mirrorNode: mirrorNode,
            rpcNode: rpcNode,
            debug: true,
        }));
        const requestST = new in_1.CreateEquityRequest({
            name: name,
            symbol: symbol,
            isin: isin,
            decimals: decimals,
            isWhiteList: false,
            erc20VotesActivated: false,
            isControllable: true,
            arePartitionsProtected: false,
            clearingActive: false,
            internalKycActivated: true,
            isMultiPartition: false,
            diamondOwnerAccount: config_1.CLIENT_ACCOUNT_ECDSA.id.toString(),
            votingRight: votingRight,
            informationRight: informationRight,
            liquidationRight: liquidationRight,
            subscriptionRight: subscriptionRight,
            conversionRight: conversionRight,
            redemptionRight: redemptionRight,
            putRight: putRight,
            dividendRight: dividendRight,
            currency: currency,
            numberOfShares: numberOfShares.toString(),
            nominalValue: nominalValue.toString(),
            nominalValueDecimals: nominalValueDecimals,
            regulationType: RegulationType_1.CastRegulationType.toNumber(regulationType),
            regulationSubType: RegulationType_1.CastRegulationSubType.toNumber(regulationSubType),
            isCountryControlListWhiteList: true,
            countries: countries,
            info: info,
            configId: configId,
            configVersion: configVersion,
        });
        equity = (await in_1.Equity.create(requestST)).security;
        console.log("equity: " + JSON.stringify(equity));
    }, 600_000);
    it("Dividends", async () => {
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress.toString(),
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._CORPORATEACTIONS_ROLE,
        }));
        const amount = "1";
        const recordTimestamp = Math.ceil(new Date().getTime() / 1000) + 1000;
        const executionTimestamp = recordTimestamp + 1000;
        await in_1.Dividend.setDividend(new in_1.SetDividendRequest({
            securityId: equity.evmDiamondAddress.toString(),
            amountPerUnitOfSecurity: amount,
            recordTimestamp: recordTimestamp.toString(),
            executionTimestamp: executionTimestamp.toString(),
        }));
        const dividend = await in_1.Dividend.getDividend(new in_1.GetDividendRequest({
            securityId: equity.evmDiamondAddress.toString(),
            dividendId: 1,
        }));
        const allDividends = await in_1.Dividend.getAllDividends(new in_1.GetAllDividendsRequest({
            securityId: equity.evmDiamondAddress.toString(),
        }));
        const dividendFor = await in_1.Dividend.getDividendFor(new in_1.GetDividendForRequest({
            securityId: equity.evmDiamondAddress.toString(),
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            dividendId: 1,
        }));
        expect(dividend.amountPerUnitOfSecurity).toEqual(amount);
        expect(dividend.amountDecimals).toEqual(0);
        expect(dividend.dividendId).toEqual(1);
        expect(dividend.executionDate.getTime() / 1000).toEqual(executionTimestamp);
        expect(dividend.recordDate.getTime() / 1000).toEqual(recordTimestamp);
        expect(dividendFor.tokenBalance).toEqual("0");
        expect(dividendFor.decimals).toEqual("0");
        expect(allDividends.length).toEqual(1);
        await in_1.Role.revokeRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress.toString(),
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._CORPORATEACTIONS_ROLE,
        }));
    }, 60_000);
});
