"""
LSB (Least Significant Bit) image embedding backend.

Wraps the existing spatial-domain LSB functions in steganography.py.
"""

from __future__ import annotations

from typing import Any


class LSBBackend:
    """Spatial-domain LSB embedding for lossless image formats."""

    @property
    def mode(self) -> str:
        return "lsb"

    @property
    def carrier_type(self) -> str:
        return "image"

    def is_available(self) -> bool:
        return True  # Only needs Pillow, which is always present

    def embed(
        self,
        data: bytes,
        carrier: bytes,
        key: bytes,
        *,
        progress_file: str | None = None,
        **options: Any,
    ) -> tuple[bytes, Any]:
        from ..steganography import _embed_lsb

        bits_per_channel = options.get("bits_per_channel", 1)
        output_format = options.get("output_format", None)
        stego_bytes, stats, ext = _embed_lsb(
            data, carrier, key, bits_per_channel, output_format, progress_file
        )
        # Attach output extension to stats for callers that need it
        stats.output_extension = ext  # type: ignore[attr-defined]
        return stego_bytes, stats

    def extract(
        self,
        carrier: bytes,
        key: bytes,
        *,
        progress_file: str | None = None,
        **options: Any,
    ) -> bytes | None:
        from ..steganography import _extract_lsb

        bits_per_channel = options.get("bits_per_channel", 1)
        return _extract_lsb(carrier, key, bits_per_channel)

    def calculate_capacity(self, carrier: bytes, **options: Any) -> int:
        from ..steganography import calculate_capacity

        bits_per_channel = options.get("bits_per_channel", 1)
        return calculate_capacity(carrier, bits_per_channel)
