import { SlashingManager__factory as SlashingManagerFactory, } from "../../types";
import { getDeploymentChain, readDeploymentArgs, storeDeploymentArgs, } from "../utils";
const DEFAULT_ADMIN_DELAY = 60n * 60n * 24n * 2n; // 2 days
/**
 * Deploys the SlashingManager contract and saves the deployment arguments
 * @param param0 - The deployment arguments
 * @returns The deployed SlashingManager contract
 */
export const deployAndSaveSlashingManager = async ({ admin, initialDelay, hre, }) => {
    const { ethers } = await hre.network.connect();
    const [signer] = await ethers.getSigners();
    const chain = getDeploymentChain(hre);
    const delay = initialDelay !== undefined ? BigInt(initialDelay) : DEFAULT_ADMIN_DELAY;
    // Reject zero delay: a zero `initialDelay` collapses the two-step
    // DEFAULT_ADMIN_ROLE handover (M-17) into a single transaction.
    if (delay === 0n) {
        throw new Error("SlashingManager initialDelay must be > 0 (two-step admin handover)");
    }
    const preDeployedArgs = readDeploymentArgs("SlashingManager", chain);
    if (!admin ||
        (preDeployedArgs?.constructorArgs?.admin === admin &&
            String(preDeployedArgs?.constructorArgs?.initialDelay ?? "") ===
                String(delay))) {
        if (!preDeployedArgs?.address) {
            throw new Error("SlashingManager address not found, it must be deployed first");
        }
        const slashingManagerContract = SlashingManagerFactory.connect(preDeployedArgs.address, signer);
        return { slashingManager: slashingManagerContract };
    }
    const slashingManagerFactory = await ethers.getContractFactory("SlashingManager");
    const slashingManager = await slashingManagerFactory.deploy(delay, admin);
    await slashingManager.waitForDeployment();
    const blockNumber = await ethers.provider.getBlockNumber();
    const slashingManagerAddress = await slashingManager.getAddress();
    storeDeploymentArgs({
        constructorArgs: {
            initialDelay: delay.toString(),
            admin,
        },
        blockNumber,
        address: slashingManagerAddress,
    }, "SlashingManager", chain);
    const slashingManagerContract = SlashingManagerFactory.connect(slashingManagerAddress, signer);
    return { slashingManager: slashingManagerContract };
};
