import sys


def _generate_sub_moved_events(src_dir_path, dest_dir_path):
    if False:
        yield


def _generate_sub_created_events(src_dir_path):
    if False:
        yield


import watchdog.events

setattr(watchdog.events, "generate_sub_moved_events", _generate_sub_moved_events)
setattr(watchdog.events, "generate_sub_created_events", _generate_sub_created_events)

if sys.platform.startswith("win"):
    import watchdog.observers.read_directory_changes

    # Disable the directory-move wait hack for the API deficiency. We'll handle it in our own queue.
    setattr(watchdog.observers.read_directory_changes, "WATCHDOG_TRAVERSE_MOVED_DIR_DELAY", 0)


from watchdog.observers import Observer
from watchdog.observers.polling import PollingObserver, PollingObserverVFS

_ignored_observers = [PollingObserver, PollingObserverVFS]
try:
    from watchdog.observers.kqueue import KqueueObserver

    _ignored_observers.append(KqueueObserver)
except Exception:
    pass

if Observer in _ignored_observers:
    Observer = None
else:
    _mod = sys.modules[Observer.__module__]
    if getattr(_mod, "generate_sub_moved_events") is not _generate_sub_moved_events:
        raise RuntimeError("Failed to monkey patch watchdog move synthetic event generators")
    if (
        hasattr(_mod, "generate_sub_created_events")
        and getattr(_mod, "generate_sub_created_events") is not _generate_sub_created_events
    ):
        raise RuntimeError("Failed to monkey patch watchdog create synthetic event generators")

# Additional imports for convenience. (importing them from the lib could interfere with the monkey-patching)
from watchdog.events import (
    FileSystemEvent,
    FileSystemMovedEvent,
    EVENT_TYPE_MOVED,
    EVENT_TYPE_DELETED,
    EVENT_TYPE_CREATED,
    EVENT_TYPE_MODIFIED,
    EVENT_TYPE_CLOSED,
)

__all__ = [
    "Observer",
    "FileSystemEvent",
    "FileSystemMovedEvent",
    "EVENT_TYPE_MOVED",
    "EVENT_TYPE_DELETED",
    "EVENT_TYPE_CREATED",
    "EVENT_TYPE_MODIFIED",
    "EVENT_TYPE_CLOSED",
]
