"""Input validation utilities for surrogate forest estimators."""

import numpy as np


def validate_sample_weight(sample_weight, n_samples):
    """Validate and prepare sample weights."""
    if sample_weight is not None:
        sample_weight = np.asarray(sample_weight, dtype=np.float64).ravel()
        if sample_weight.shape[0] != n_samples:
            raise ValueError("sample_weight must have the same length as X")
    else:
        sample_weight = np.ones(n_samples, dtype=np.float64)
    return sample_weight


def compute_feature_indices(max_features, n_features, random_state):
    """Compute the number of features to consider at each split."""
    if max_features is None:
        return n_features
    elif isinstance(max_features, str):
        if max_features == "sqrt":
            return max(1, int(np.sqrt(n_features)))
        elif max_features == "log2":
            return max(1, int(np.log2(n_features)))
        elif max_features == "auto":
            return max(1, int(np.sqrt(n_features)))
        else:
            raise ValueError(f"Invalid max_features: {max_features}")
    elif isinstance(max_features, float):
        return max(1, int(max_features * n_features))
    elif isinstance(max_features, int):
        return min(max_features, n_features)
    else:
        raise ValueError(f"Invalid max_features type: {type(max_features)}")
