"""Reference policy only. No provider calls."""
def select_provider(providers, merchant_id, currency, method, environment, attempt=None):
    if attempt and attempt.get("state") in ("processing", "unknown", "succeeded"):
        return {"state": "reconcile_first"}
    eligible = [p for p in providers if p["merchantId"] == merchant_id
                and p["environment"] == environment and p["authorized"] is True
                and p["available"] is True and currency in p["currencies"]
                and method in p["methods"]]
    return {"state": "selected", "provider": min(eligible, key=lambda p: p["priority"])["id"]} if eligible else {"state": "no_eligible_provider"}
def refundable_amount(captured, refunded, pending_refunds, requested):
    if any(type(x) is not int for x in (captured, refunded, pending_refunds, requested)) or min(captured, refunded, pending_refunds) < 0 or not 0 < requested <= captured-refunded-pending_refunds:
        raise ValueError("INVALID_REFUND_AMOUNT")
    return requested
def reconcile(gross, fees, refunds, net):
    if any(type(x) is not int for x in (gross, fees, refunds, net)):
        raise ValueError("INVALID_MINOR_UNITS")
    return {"balanced": gross-fees-refunds == net, "expected": gross-fees-refunds, "reported": net}
