from __future__ import annotations

from pathlib import Path

from virtualenv.seed.wheels.util import Wheel

BUNDLE_FOLDER = Path('/usr/share/python-wheels')
BUNDLE_SUPPORT = {
    "3.8": {
        "pip": "pip-25.0.1-py3-none-any.whl",
        "setuptools": "setuptools-75.3.2-py3-none-any.whl",
        "wheel": "wheel-0.45.1-py3-none-any.whl",
    },
    "3.9": {
        "pip": "pip-25.1.1-py3-none-any.whl",
        "setuptools": "setuptools-80.3.1-py3-none-any.whl",
    },
    "3.10": {
        "pip": "pip-25.1.1-py3-none-any.whl",
        "setuptools": "setuptools-80.3.1-py3-none-any.whl",
    },
    "3.11": {
        "pip": "pip-25.1.1-py3-none-any.whl",
        "setuptools": "setuptools-80.3.1-py3-none-any.whl",
    },
    "3.12": {
        "pip": "pip-25.1.1-py3-none-any.whl",
        "setuptools": "setuptools-80.3.1-py3-none-any.whl",
    },
    "3.13": {
        "pip": "pip-25.1.1-py3-none-any.whl",
        "setuptools": "setuptools-80.3.1-py3-none-any.whl",
    },
    "3.14": {
        "pip": "pip-25.1.1-py3-none-any.whl",
        "setuptools": "setuptools-80.3.1-py3-none-any.whl",
    },
}
MAX = "3.8"


# Debian specific: Update BUNDLE_SUPPORT to match pip wheels shipped in
# /usr/share/python-wheels for base install + pkg_resources.
def list_available_wheels(versions):
    import os
    bundle = {version: {} for version in versions}
    wheel_files = [Wheel.from_path(BUNDLE_FOLDER / fn)
                   for fn in os.listdir(BUNDLE_FOLDER)]
    # Sort wheels so the latest compatible version wins
    wheel_files.sort(key=lambda wheel: wheel.version_tuple)
    for wheel in wheel_files:
        if wheel.distribution in ['pip', 'setuptools', 'wheel']:
            for version in versions:
                if wheel.support_py(version):
                    bundle[version][wheel.distribution] = wheel.name
    return bundle


BUNDLE_SUPPORT = list_available_wheels(BUNDLE_SUPPORT.keys())
# End Debian specific


def get_embed_wheel(distribution, for_py_version):
    # Debian specific: Point at the appropriate wheel package
    wheel = BUNDLE_SUPPORT.get(for_py_version, {}).get(distribution)
    if wheel is None:
        raise Exception((
                "Wheel for {} for Python {} is unavailable. "
                "apt install python{}-{}-whl"
            ).format(
                distribution,
                for_py_version,
                '2' if for_py_version == '2.7' else '3',
                distribution,
            ))
    # End Debian specific

    mapping = BUNDLE_SUPPORT.get(for_py_version, {}) or BUNDLE_SUPPORT[MAX]
    wheel_file = mapping.get(distribution)
    if wheel_file is None:
        return None
    path = BUNDLE_FOLDER / wheel_file
    return Wheel.from_path(path)


__all__ = [
    "BUNDLE_FOLDER",
    "BUNDLE_SUPPORT",
    "MAX",
    "get_embed_wheel",
]
