"""The deterministic guess for samples no decision covers.

Fixed pipeline helper; do not edit. The generator and its keys are shared with
``matlab/guessLabel.m`` and ``matlab/lcgStep.m``; both languages fill the same
blocks with the same labels.
"""

from __future__ import annotations

import numpy as np

from .lcg_step import lcg_step

_MODULUS = 1 << 32


def guess_label(neural: np.ndarray) -> int:
    """One label in {0, 1} for a block with no decision.

    ``floor()`` of eight samples (channel ``i % n_channels``), folded into a
    32-bit linear congruential generator (LCG). Same keys as ``guessLabel.m``.
    """
    n_samples = neural.shape[1]
    n_channels = neural.shape[0]
    n_elements = int(neural.size)
    state = 0
    for i in range(8):
        column = int(np.floor(i * (n_samples - 1) / 7))
        channel = i % n_channels
        state = lcg_step(state, float(neural[channel, column]), _MODULUS)
    state = lcg_step(state, float(n_elements), _MODULUS)
    state = lcg_step(state, 0.0, _MODULUS)
    return int((state // 65536) % 2)
