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
import watchdog.version

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") and watchdog.version.VERSION_MAJOR < 5:
    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,
    DirMovedEvent,
    FileMovedEvent,
    DirCreatedEvent,
    FileCreatedEvent,
    DirModifiedEvent,
    FileModifiedEvent,
    DirDeletedEvent,
    FileDeletedEvent,
    FileClosedEvent,
    FileSystemEventHandler,
    EVENT_TYPE_MOVED,
    EVENT_TYPE_DELETED,
    EVENT_TYPE_CREATED,
    EVENT_TYPE_MODIFIED,
    EVENT_TYPE_CLOSED,
)

FILESYSTEM_EVENT_FILTER = [  # ignore events that do not denote changes
    DirMovedEvent,
    FileMovedEvent,
    DirCreatedEvent,
    FileCreatedEvent,
    DirModifiedEvent,
    FileModifiedEvent,
    DirDeletedEvent,
    FileDeletedEvent,
    FileClosedEvent,
    # FileClosedNoWriteEvent,  # no-write handle close can't be a change
    # FileOpenedEvent,  # open is not a change yet even if it is for writing
]

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