import os
from pathlib import Path
import sys
from typing import Dict, Tuple, Union

import pytest

from byteb4rb1e.testing.pytest import get_current_test

_SITE_PACKAGE_COUNTER: Dict[str, int] = {}


@pytest.fixture
def current_test() -> Tuple[Path, str]:
    """
    """
    return get_current_test()


@pytest.fixture
def mock_system_site_package_dir(tmp_path):
    global _SITE_PACKAGE_COUNTER

    package_id = _SITE_PACKAGE_COUNTER.setdefault(tmp_path, 0)
    _SITE_PACKAGE_COUNTER[tmp_path] += 1

    sys_path = tmp_path / str(package_id)

    def _create(name: str) -> Path:
        pkg_path = sys_path / name.replace('.', os.path.sep)

        pkg_path.mkdir(parents=True)

        (pkg_path / "__init__.py").touch()

        sys.path.insert(0, str(sys_path))

        return pkg_path

    yield _create

    # cleanup sys.path after test
    if str(sys_path) in sys.path:
        sys.path.remove(str(sys_path))
