import pathlib
import tempfile
import zipfile


def extract_archvie(archive_path: pathlib.Path) -> pathlib.Path:
    """
    Extract a user data archive to a temporary directory.

    If the user supplied a path to an already unzipped archive, this is a no-op and will
    return the same path. Otherwise, the archive is unzipped to a system temporary
    directory and that path is returned.
    """
    if archive_path.is_dir():
        return archive_path

    if archive_path.is_file():
        temp_dir = tempfile.mkdtemp()

        with zipfile.ZipFile(archive_path) as zfile:
            zfile.extractall(temp_dir)

        return pathlib.Path(temp_dir)

    raise ValueError(f"{archive_path} is not a directory or zip file")