"""
Price basis selection logic for exit sell actions.

F02/F03/F04/F06 porting: Determines the basis for price selection (e.g., take-profit tier
prices vs. close-based protective limits) in the sell signal decision tree.

Ported from: src/gas_adapter_parts/gdf_01_price_metrics.gs:calcExitSellAction_()
Parity reference: tests/parity/test_price_basis_parity_v1.py
"""

import math


def is_finite(value) -> bool:
    """JavaScript Number.isFinite() semantics: true only for finite numbers."""
    return isinstance(value, (int, float)) and math.isfinite(value)


def select_price_basis_tier2(tp2_price: float) -> str:
    """
    Select price basis for PROFIT_TRIM_40/35 actions (profitPct >= 40/30).
    F02/F03: lines 774, 783

    GAS: Number.isFinite(tp2Price) && tp2Price > 0 ? "TAKE_PROFIT_TIER2_PRICE" : "PRIOR_CLOSE_X_0.998"
    """
    if is_finite(tp2_price) and tp2_price > 0:
        return "TAKE_PROFIT_TIER2_PRICE"
    return "PRIOR_CLOSE_X_0.998"


def select_price_basis_tier1(tp1_price: float) -> str:
    """
    Select price basis for PROFIT_TRIM_25/TAKE_PROFIT_TIER1 actions (profitPct >= 20/10).
    F04/F06: lines 792, 801

    GAS: Number.isFinite(tp1Price) && tp1Price > 0 ? "TAKE_PROFIT_TIER1_PRICE" : "PRIOR_CLOSE_X_0.998"
    """
    if is_finite(tp1_price) and tp1_price > 0:
        return "TAKE_PROFIT_TIER1_PRICE"
    return "PRIOR_CLOSE_X_0.998"
