"""Public API compatibility layer for the acfmperiod package."""

from __future__ import annotations

from typing import Optional

from numpy.typing import ArrayLike, NDArray

from .acfcalc import covcormper, covcorper, gmat, mperacf, peracf
from .datatypes import AcfResult, CrossPeriodogramResult, NAAction
from .periodograms import crossperiodogram, mcrossperiodogram, mperioreg, perioreg


def PerioReg(series: ArrayLike) -> NDArray:
    """R-style alias for :func:`acfmperiod.periodograms.perioreg`.

    Parameters
    ----------
    series
        Input one-dimensional numeric series.

    Returns
    -------
    NDArray
        Classical mirrored periodogram values.
    """

    return perioreg(series)


def MPerioReg(series: ArrayLike) -> NDArray:
    """R-style alias for :func:`acfmperiod.periodograms.mperioreg`.

    Parameters
    ----------
    series
        Input one-dimensional numeric series.

    Returns
    -------
    NDArray
        Robust mirrored M-periodogram values.
    """

    return mperioreg(series)


def CrossPeriodogram(series1: ArrayLike, series2: ArrayLike) -> CrossPeriodogramResult:
    """R-style alias for :func:`acfmperiod.periodograms.crossperiodogram`.

    Parameters
    ----------
    series1
        First input series.
    series2
        Second input series with the same length as ``series1``.

    Returns
    -------
    CrossPeriodogramResult
        Directed classical cross-periodogram estimates.
    """

    return crossperiodogram(series1, series2)


def MCrossPeriodogram(series1: ArrayLike, series2: ArrayLike) -> CrossPeriodogramResult:
    """R-style alias for :func:`acfmperiod.periodograms.mcrossperiodogram`.

    Parameters
    ----------
    series1
        First input series.
    series2
        Second input series with the same length as ``series1``.

    Returns
    -------
    CrossPeriodogramResult
        Directed robust cross-periodogram estimates.
    """

    return mcrossperiodogram(series1, series2)


def PerACF(
    x: ArrayLike,
    lagmax: Optional[int] = None,
    typevalue: str = "correlation",
    doplot: bool = True,
    naaction: NAAction = "fail",
    demean: bool = True,
    **plotkwargs,
) -> AcfResult:
    """R-style alias for :func:`acfmperiod.acfcalc.peracf`.

    Parameters
    ----------
    x
        Input vector or matrix-like object.
    lagmax
        Number of lags to compute, or ``None`` for automatic selection.
    typevalue
        ``"correlation"`` or ``"covariance"`` (prefixes allowed).
    doplot
        If ``True``, plot the result.
    naaction
        Missing-data policy (``"fail"``, ``"pass"``, or callable).
    demean
        If ``True``, subtract column means before estimation.
    **plotkwargs
        Plot options forwarded to the plotting helper.

    Returns
    -------
    AcfResult
        Classical ACF/ACOVF estimate.
    """

    return peracf(
        x=x,
        lagmax=lagmax,
        typevalue=typevalue,
        doplot=doplot,
        naaction=naaction,
        demean=demean,
        **plotkwargs,
    )


def MPerACF(
    x: ArrayLike,
    lagmax: Optional[int] = None,
    typevalue: str = "correlation",
    doplot: bool = True,
    naaction: NAAction = "fail",
    demean: bool = True,
    **plotkwargs,
) -> AcfResult:
    """R-style alias for :func:`acfmperiod.acfcalc.mperacf`.

    Parameters
    ----------
    x
        Input vector or matrix-like object.
    lagmax
        Number of lags to compute, or ``None`` for automatic selection.
    typevalue
        ``"correlation"`` or ``"covariance"`` (prefixes allowed).
    doplot
        If ``True``, plot the result.
    naaction
        Missing-data policy (``"fail"``, ``"pass"``, or callable).
    demean
        If ``True``, subtract column means before estimation.
    **plotkwargs
        Plot options forwarded to the plotting helper.

    Returns
    -------
    AcfResult
        Robust ACF/ACOVF estimate.
    """

    return mperacf(
        x=x,
        lagmax=lagmax,
        typevalue=typevalue,
        doplot=doplot,
        naaction=naaction,
        demean=demean,
        **plotkwargs,
    )


def CovCorPer(x: ArrayLike, typevalue: str = "correlation") -> NDArray:
    """R-style alias for :func:`acfmperiod.acfcalc.covcorper`.

    Parameters
    ----------
    x
        Input vector or matrix-like object.
    typevalue
        ``"correlation"`` or ``"covariance"`` (prefixes allowed).

    Returns
    -------
    NDArray
        Classical lag-0 covariance or correlation matrix.
    """

    return covcorper(x, typevalue=typevalue)


def CovCorMPer(x: ArrayLike, typevalue: str = "correlation") -> NDArray:
    """R-style alias for :func:`acfmperiod.acfcalc.covcormper`.

    Parameters
    ----------
    x
        Input vector or matrix-like object.
    typevalue
        ``"correlation"`` or ``"covariance"`` (prefixes allowed).

    Returns
    -------
    NDArray
        Robust lag-0 covariance or correlation matrix.
    """

    return covcormper(x, typevalue=typevalue)


def Gmat(n: int) -> NDArray:
    """R-style alias for :func:`acfmperiod.acfcalc.gmat`.

    Parameters
    ----------
    n
        Matrix dimension and series length.

    Returns
    -------
    NDArray
        Unitary Fourier matrix with shape ``(n, n)``.
    """

    return gmat(n)


__all__ = [
    "AcfResult",
    "CrossPeriodogramResult",
    "NAAction",
    "perioreg",
    "mperioreg",
    "crossperiodogram",
    "mcrossperiodogram",
    "peracf",
    "mperacf",
    "covcorper",
    "covcormper",
    "gmat",
    "PerioReg",
    "MPerioReg",
    "CrossPeriodogram",
    "MCrossPeriodogram",
    "PerACF",
    "MPerACF",
    "CovCorPer",
    "CovCorMPer",
    "Gmat",
]
