import { artifacts, deployScript } from '@rocketh';
import { namehash, zeroAddress } from 'viem';
import { createInterfaceId } from '../../test/fixtures/createInterfaceId.js';
export default deployScript(async ({ deploy, get, execute: write, read, namedAccounts, network, registerUnwrappedNames, }) => {
    const { deployer, owner } = namedAccounts;
    const registry = get('ENSRegistry');
    const registrar = get('BaseRegistrarImplementation');
    const registrarSecurityController = get('RegistrarSecurityController');
    const priceOracle = get('ExponentialPremiumPriceOracle');
    const reverseRegistrar = get('ReverseRegistrar');
    const defaultReverseRegistrar = get('DefaultReverseRegistrar');
    const controller = await deploy('ETHRegistrarController', {
        account: deployer,
        artifact: artifacts.ETHRegistrarController,
        args: [
            registrar.address,
            priceOracle.address,
            60n,
            86400n,
            reverseRegistrar.address,
            defaultReverseRegistrar.address,
            registry.address,
        ],
    });
    if (!controller.newlyDeployed)
        return;
    // Transfer ownership to owner
    if (owner !== deployer) {
        console.log(`  - Transferring ownership of ETHRegistrarController to ${owner}`);
        await write(controller, {
            functionName: 'transferOwnership',
            args: [owner],
            account: deployer,
        });
    }
    // Only attempt to make controller etc changes directly on testnets
    if (network.name === 'mainnet' && !network.tags?.tenderly)
        return;
    // Add controller to BaseRegistrarImplementation
    console.log(`  - Adding ETHRegistrarController via RegistrarSecurityController`);
    await write(registrarSecurityController, {
        functionName: 'addRegistrarController',
        args: [controller.address],
        account: owner,
    });
    // Add controller to ReverseRegistrar
    console.log(`  - Adding ETHRegistrarController as controller on ReverseRegistrar`);
    await write(reverseRegistrar, {
        functionName: 'setController',
        args: [controller.address, true],
        account: owner,
    });
    // Add controller to DefaultReverseRegistrar
    console.log(`  - Adding ETHRegistrarController as controller on DefaultReverseRegistrar`);
    await write(defaultReverseRegistrar, {
        functionName: 'setController',
        args: [controller.address, true],
        account: owner,
    });
    // Set interface on resolver
    const artifact = artifacts.IETHRegistrarController;
    const interfaceId = createInterfaceId(artifact.abi);
    const resolver = await read(registry, {
        functionName: 'resolver',
        args: [namehash('eth')],
    });
    if (resolver === zeroAddress) {
        console.warn(`  - WARN: No resolver set for .eth; not setting interface ${interfaceId} for ETHRegistrarController`);
        return;
    }
    console.log(`  - Setting ETHRegistrarController interface ID ${interfaceId} on .eth resolver`);
    await write({ ...artifacts.OwnedResolver, address: resolver }, {
        functionName: 'setInterface',
        args: [namehash('eth'), interfaceId, controller.address],
        account: owner,
    });
    if (registerUnwrappedNames) {
        console.log('  - Running registerUnwrappedNames hook');
        await registerUnwrappedNames();
    }
}, {
    id: 'ETHRegistrarController v3.0.0',
    tags: ['category:ethregistrar', 'ETHRegistrarController'],
    dependencies: [
        'ENSRegistry',
        'BaseRegistrarImplementation',
        'RegistrarSecurityController',
        'ExponentialPremiumPriceOracle',
        'ReverseRegistrar',
        'DefaultReverseRegistrar',
        'NameWrapper',
        'OwnedResolver',
    ],
});
