from pathlib import Path


def _process_logging(logging, n_optimizations):
    """Process paths to databases.

    `logging` can be a single value in which case it becomes an iterable with a single
    value to simplify the processing.

    `logging` can also be a `list` or a `tuple` in which case

    - a single path receives a number as a suffix for more than one optimizations.
    - everything else is parsed according to the following transformation rules.

    Every candidate value for `logging` is transformed according to the following rules.
    If `logging` is a `str`, it is converted to :class:`pathlib.Path` and a
    :class:`pathlib.Path` is left unchanged. Everything which evaluates to `False` is
    set to `False` which turns logging off.

    Raises
    ------
    ValueError
        If `n_logging` is not one and not `n_optimizations`.
    ValueError
        If there are identical paths in `logging`.

    Example
    -------
    >>> _process_logging(False, 1)
    [False]
    >>> _process_logging(False, 2)
    [False, False]
    >>> _process_logging([False], 2)
    [False, False]

    """
    if not isinstance(logging, (tuple, list)):
        if not logging:
            logging = [False] * n_optimizations
        elif not isinstance(logging, (str, Path)):
            raise ValueError("logging has to be a pathlib.Path, string or False")

        elif n_optimizations >= 2:
            path = Path(logging).absolute()
            logging = [
                path.parent / (path.stem + f"_{i}.db") for i in range(n_optimizations)
            ]
        else:
            logging = [Path(logging).absolute()]

    else:
        if len(logging) == 1:
            logging = logging * n_optimizations
        elif len(logging) == n_optimizations:
            pass
        else:
            raise ValueError(
                f"logging has {len(logging)} entries and there are {n_optimizations} "
                "optimizations. Cannot harmonize entries."
            )

    # replace everything that evaluates to False by an actual False
    logging = [path if path else False for path in logging]

    return logging