"""Whitelist [sync/static] specific rules customization."""
from typing import Iterator

from im360.contracts.config import UnifiedAccessLogger
from im360.internals.core import firewall
from im360.internals.core.firewall import FirewallRules
from defence360agent.utils.validate import IPVersion

from .log import rules as log_rules
from .types_ import FirewallRule


def rules(
    ipset_name: str,
    ip_version: IPVersion,
    priority: int = FirewallRules.WHITELIST_PRIORITY,
) -> Iterator[FirewallRule]:
    """Yield iptables *ip_version* white.sync rules for *ipset_name*."""
    yield from log_rules(
        ipset_name, ip_version, UnifiedAccessLogger.WHITELIST, priority
    )
    rule_name = FirewallRules.ipset_rule(ipset_name, FirewallRules.RETURN)
    yield FirewallRule(rule=rule_name, priority=priority)
    yield FirewallRule(
        rule=rule_name,
        table=FirewallRules.NAT
        if firewall.is_nat_available(ip_version)
        else FirewallRules.MANGLE,
        priority=priority,
    )
