// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

/**
 * @title AddressMapperEmpty
 * @notice Same interface as AddressMapper but with no initial mappings (for chains other than 138).
 * @dev Deploy on Cronos, BSC, etc. and add mappings via setMapping() if needed.
 */
contract AddressMapperEmpty {
    mapping(address => address) private _addressMap;
    mapping(address => address) private _reverseMap;
    address public owner;

    event AddressMapped(address indexed genesisAddress, address indexed deployedAddress);
    event MappingRemoved(address indexed genesisAddress);

    modifier onlyOwner() {
        require(msg.sender == owner, "AddressMapper: caller is not owner");
        _;
    }

    constructor() {
        owner = msg.sender;
    }

    function getDeployedAddress(address genesisAddress) external view returns (address) {
        address deployed = _addressMap[genesisAddress];
        return deployed == address(0) ? genesisAddress : deployed;
    }

    function getGenesisAddress(address deployedAddress) external view returns (address) {
        return _reverseMap[deployedAddress];
    }

    function isMapped(address addr) external view returns (bool) {
        return _addressMap[addr] != address(0);
    }

    function setMapping(address genesisAddress, address deployedAddress) external onlyOwner {
        require(genesisAddress != address(0), "AddressMapper: genesis address cannot be zero");
        require(deployedAddress != address(0), "AddressMapper: deployed address cannot be zero");
        require(genesisAddress != deployedAddress, "AddressMapper: addresses must be different");
        address oldDeployed = _addressMap[genesisAddress];
        if (oldDeployed != address(0)) delete _reverseMap[oldDeployed];
        address oldGenesis = _reverseMap[deployedAddress];
        if (oldGenesis != address(0)) delete _addressMap[oldGenesis];
        _addressMap[genesisAddress] = deployedAddress;
        _reverseMap[deployedAddress] = genesisAddress;
        emit AddressMapped(genesisAddress, deployedAddress);
    }

    function removeMapping(address genesisAddress) external onlyOwner {
        address deployed = _addressMap[genesisAddress];
        require(deployed != address(0), "AddressMapper: mapping does not exist");
        delete _addressMap[genesisAddress];
        delete _reverseMap[deployed];
        emit MappingRemoved(genesisAddress);
    }

    function transferOwnership(address newOwner) external onlyOwner {
        require(newOwner != address(0), "AddressMapper: new owner cannot be zero");
        owner = newOwner;
    }
}
