"""
A platform independent file lock that supports the with-statement.

.. autodata:: filelock.__version__
    :no-value:

.. autodata:: filelock.OwnerMode

.. autodata:: filelock.CompromiseReason

"""

from __future__ import annotations

from typing import TYPE_CHECKING, cast

if TYPE_CHECKING:
    from importlib.machinery import ModuleSpec

__spec__ = cast("ModuleSpec", __spec__)

__lazy_modules__ = [
    "importlib",
    f"{__spec__.parent}._api",
    f"{__spec__.parent}._descriptor",
    f"{__spec__.parent}._error",
]

import sys
import warnings
from importlib import import_module
from typing import TYPE_CHECKING, Final

# Keeps ``filelock.version`` the string 4.0.12 exposed rather than the submodule.
from filelock.version import version

from ._api import (
    AcquireReturnProxy,
    BaseFileLock,
    CloseErrorPolicy,
    ContextErrorPolicy,
    LockOptions,
)
from ._descriptor import (
    lock_descriptor,
    unlock_descriptor,
)
from ._error import (
    LeaseSettingsMismatch,
    SoftFileLockLifetimeWarning,
    SoftFileLockProtocolError,
    Timeout,
)
from ._unix import UnixFileLock
from ._unix import (
    has_fcntl as _has_fcntl,
)
from ._windows import WindowsFileLock

if TYPE_CHECKING:
    from filelock import asyncio
    from filelock.asyncio import (
        AsyncAcquireReturnProxy,
        AsyncSoftFileLease,
        AsyncSoftFileLock,
        AsyncStrictSoftFileLock,
        AsyncUnixFileLock,
        AsyncWindowsFileLock,
        BaseAsyncFileLock,
    )

    from ._async_read_write import (
        AsyncAcquireReadWriteReturnProxy,
        AsyncReadWriteLock,
    )
    from ._lease import (
        CompromiseReason,
        LeaseCompromise,
        SoftFileLease,
    )
    from ._marker import (
        MarkerSoftFileLock,
        OwnerMode,
        OwnerRecord,
    )
    from ._read_write import ReadWriteLock
    from ._soft import SoftFileLock
    from ._soft_rw import (
        AsyncAcquireSoftReadWriteReturnProxy,
        AsyncSoftReadWriteLock,
        SoftReadWriteLock,
    )
    from ._strict import (
        StrictSoftFileClaim,
        StrictSoftFileClaimState,
        StrictSoftFileLock,
    )

#: version of the project as a string
__version__: Final[str] = version

#: ``True`` when :func:`fcntl.flock` is available. Off Windows, :data:`FileLock` then resolves to
#: :class:`UnixFileLock`, otherwise to :class:`SoftFileLock`. Always ``False`` on Windows.
#:
#: :meta hide-value:
has_fcntl: Final[bool] = _has_fcntl  # simplint: ignore[module-variable-case]  # public API


if sys.platform == "win32":  # pragma: win32 cover
    _FileLock: type[BaseFileLock] = WindowsFileLock
else:  # pragma: win32 no cover # ruff:ignore[collapsible-else-if]  # the else carries the win32 no-cover pragma
    if has_fcntl:
        _FileLock: type[BaseFileLock] = UnixFileLock
    else:
        from ._soft import SoftFileLock

        _FileLock = SoftFileLock
        warnings.warn("only soft file lock is available", stacklevel=2)

if TYPE_CHECKING:
    #: Alias for the lock, which should be used for the current platform.
    FileLock = SoftFileLock
    #: Alias for the async lock, which should be used for the current platform.
    AsyncFileLock = AsyncSoftFileLock
else:
    #: Alias for the lock, which should be used for the current platform.
    FileLock = _FileLock

# Importing asyncio and sqlite3 costs more than the rest of the package, so names that need them load on first access.
_LAZY_ATTRIBUTES: Final[dict[str, str]] = {
    "AsyncAcquireReadWriteReturnProxy": "._async_read_write",
    "AsyncAcquireReturnProxy": ".asyncio",
    "AsyncAcquireSoftReadWriteReturnProxy": "._soft_rw",
    "AsyncFileLock": ".asyncio",
    "AsyncReadWriteLock": "._async_read_write",
    "AsyncSoftFileLease": ".asyncio",
    "AsyncSoftFileLock": ".asyncio",
    "AsyncSoftReadWriteLock": "._soft_rw",
    "AsyncStrictSoftFileLock": ".asyncio",
    "AsyncUnixFileLock": ".asyncio",
    "AsyncWindowsFileLock": ".asyncio",
    "BaseAsyncFileLock": ".asyncio",
    "CompromiseReason": "._lease",
    "LeaseCompromise": "._lease",
    "MarkerSoftFileLock": "._marker",
    "OwnerMode": "._marker",
    "OwnerRecord": "._marker",
    "ReadWriteLock": "._read_write",
    "SoftFileLease": "._lease",
    "SoftFileLock": "._soft",
    "SoftReadWriteLock": "._soft_rw",
    "StrictSoftFileClaim": "._strict",
    "StrictSoftFileClaimState": "._strict",
    "StrictSoftFileLock": "._strict",
    "asyncio": ".asyncio",
}


def __getattr__(
    name: str,
) -> object:  # simplint: ignore[imprecise-annotation]  # lazy attributes span several types
    if (module_name := _LAZY_ATTRIBUTES.get(name)) is None:
        msg = f"module {__name__!r} has no attribute {name!r}"
        raise AttributeError(msg)
    try:
        module = import_module(module_name, __name__)
    except ImportError as error:
        # Only a missing sqlite3 module (the package, CPython's _sqlite3, PyPy's _sqlite3_cffi) leaves the read-write
        # locks None; a frozen application that left out a filelock module gets the error that names it.
        sqlite_missing = (error.name or "").startswith(("sqlite3", "_sqlite3"))
        if module_name not in {"._read_write", "._async_read_write"} or not sqlite_missing:
            raise
        value = None  # pragma: lacks sqlite3
    else:
        if name == "asyncio":
            value = module
        else:
            value = getattr(module, f"Async{_FileLock.__name__}" if name == "AsyncFileLock" else name)
    globals()[name] = value
    return value


def __dir__() -> list[str]:
    return sorted({*globals(), *_LAZY_ATTRIBUTES})


__all__ = [
    "AcquireReturnProxy",
    "AsyncAcquireReadWriteReturnProxy",
    "AsyncAcquireReturnProxy",
    "AsyncAcquireSoftReadWriteReturnProxy",
    "AsyncFileLock",
    "AsyncReadWriteLock",
    "AsyncSoftFileLease",
    "AsyncSoftFileLock",
    "AsyncSoftReadWriteLock",
    "AsyncStrictSoftFileLock",
    "AsyncUnixFileLock",
    "AsyncWindowsFileLock",
    "BaseAsyncFileLock",
    "BaseFileLock",
    "CloseErrorPolicy",
    "CompromiseReason",
    "ContextErrorPolicy",
    "FileLock",
    "LeaseCompromise",
    "LeaseSettingsMismatch",
    "LockOptions",
    "MarkerSoftFileLock",
    "OwnerMode",
    "OwnerRecord",
    "ReadWriteLock",
    "SoftFileLease",
    "SoftFileLock",
    "SoftFileLockLifetimeWarning",
    "SoftFileLockProtocolError",
    "SoftReadWriteLock",
    "StrictSoftFileClaim",
    "StrictSoftFileClaimState",
    "StrictSoftFileLock",
    "Timeout",
    "UnixFileLock",
    "WindowsFileLock",
    "__version__",
    "asyncio",
    "has_fcntl",
    "lock_descriptor",
    "unlock_descriptor",
]
