# -*- coding: utf-8 -*-
"""
@author: yq
@time: 2025/4/24
@desc: 
"""

import numpy
from pandas import DataFrame, Series


def _filter_column_names(X):
    return (numpy.asarray(X)).astype(str)


def _get_column_names(X):
    if isinstance(X, DataFrame):
        return _filter_column_names(X.columns.values)
    elif isinstance(X, Series):
        return _filter_column_names(X.name)
    # elif isinstance(X, H2OFrame)
    elif hasattr(X, "names"):
        return _filter_column_names(X.names)
    else:
        return None


def Xtransformer_fit(self, X, y=None):
    # Collect feature name(s)
    active_fields = _get_column_names(X)
    if active_fields is not None:
        self.active_fields = active_fields
    # Collect label name(s)
    target_fields = _get_column_names(y)
    if target_fields is not None:
        self.target_fields = target_fields

    self.steps = list(self.steps)
    self._validate_steps()

    for (step_idx, name, transformer) in self._iter(with_final=False, filter_passthrough=False):
        transformer.fit(X)
        self.steps[step_idx] = (name, transformer)


def Xtransform(self, X):
    Xt = X
    for name, transform in self.steps[:-1]:
        if transform is not None:
            Xt = transform.transform(Xt)
    return Xt


def fit(self, X, y=None, **fit_params):
    fit_params_steps = self._check_fit_params(**fit_params)
    Xt = self.Xtransform(X)
    if self._final_estimator != 'passthrough':
        fit_params_last_step = fit_params_steps[self.steps[-1][0]]
        self._final_estimator.fit(Xt, y, **fit_params_last_step)
    return self
