# DO NOT EDIT THIS FILE!
#
# This file is generated from the WebDriver BiDi specification. If you need to make
# changes, edit the generator and regenerate all of the modules.
#
# WebDriver BiDi module: storage
from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any

from selenium.webdriver.common.bidi.common import command_builder

@dataclass
class PartitionKey:
    """PartitionKey."""

    user_context: str | None = None
    source_origin: str | None = None


@dataclass
class GetCookiesParameters:
    """GetCookiesParameters."""

    filter: Any | None = None
    partition: Any | None = None


@dataclass
class GetCookiesResult:
    """GetCookiesResult."""

    cookies: list[Any] = field(default_factory=list)
    partition_key: Any | None = None


@dataclass
class SetCookieParameters:
    """SetCookieParameters."""

    cookie: Any | None = None
    partition: Any | None = None


@dataclass
class SetCookieResult:
    """SetCookieResult."""

    partition_key: Any | None = None


@dataclass
class DeleteCookiesParameters:
    """DeleteCookiesParameters."""

    filter: Any | None = None
    partition: Any | None = None


@dataclass
class DeleteCookiesResult:
    """DeleteCookiesResult."""

    partition_key: Any | None = None


class BytesValue:
    """A string or base64-encoded bytes value used in cookie operations.

    This corresponds to network.BytesValue in the WebDriver BiDi specification,
    wrapping either a plain string or a base64-encoded binary value.
    """

    TYPE_STRING = "string"
    TYPE_BASE64 = "base64"

    def __init__(self, type: Any | None, value: Any | None) -> None:
        self.type = type
        self.value = value

    def to_bidi_dict(self) -> dict:
        return {"type": self.type, "value": self.value}

    def to_dict(self) -> dict:
        """Backward-compatible alias for to_bidi_dict()."""
        return self.to_bidi_dict()

class SameSite:
    """SameSite cookie attribute values."""

    STRICT = "strict"
    LAX = "lax"
    NONE = "none"
    DEFAULT = "default"

@dataclass
class StorageCookie:
    """A cookie object returned by storage.getCookies."""

    name: str | None = None
    value: Any | None = None
    domain: str | None = None
    path: str | None = None
    size: Any | None = None
    http_only: bool | None = None
    secure: bool | None = None
    same_site: Any | None = None
    expiry: Any | None = None

    @classmethod
    def from_bidi_dict(cls, raw: dict) -> StorageCookie:
        """Deserialize a wire-level cookie dict to a StorageCookie."""
        value_raw = raw.get("value")
        if isinstance(value_raw, dict):
            value: Any = BytesValue(value_raw.get("type"), value_raw.get("value"))
        else:
            value = value_raw
        return cls(
            name=raw.get("name"),
            value=value,
            domain=raw.get("domain"),
            path=raw.get("path"),
            size=raw.get("size"),
            http_only=raw.get("httpOnly"),
            secure=raw.get("secure"),
            same_site=raw.get("sameSite"),
            expiry=raw.get("expiry"),
        )

@dataclass
class CookieFilter:
    """CookieFilter."""

    name: str | None = None
    value: Any | None = None
    domain: str | None = None
    path: str | None = None
    size: Any | None = None
    http_only: bool | None = None
    secure: bool | None = None
    same_site: Any | None = None
    expiry: Any | None = None

    def to_bidi_dict(self) -> dict:
        """Serialize to the BiDi wire-protocol dict."""
        result: dict = {}
        if self.name is not None:
            result["name"] = self.name
        if self.value is not None:
            result["value"] = self.value.to_bidi_dict() if hasattr(self.value, "to_bidi_dict") else self.value
        if self.domain is not None:
            result["domain"] = self.domain
        if self.path is not None:
            result["path"] = self.path
        if self.size is not None:
            result["size"] = self.size
        if self.http_only is not None:
            result["httpOnly"] = self.http_only
        if self.secure is not None:
            result["secure"] = self.secure
        if self.same_site is not None:
            result["sameSite"] = self.same_site
        if self.expiry is not None:
            result["expiry"] = self.expiry
        return result

    def to_dict(self) -> dict:
        """Backward-compatible alias for to_bidi_dict()."""
        return self.to_bidi_dict()

@dataclass
class PartialCookie:
    """PartialCookie."""

    name: str | None = None
    value: Any | None = None
    domain: str | None = None
    path: str | None = None
    http_only: bool | None = None
    secure: bool | None = None
    same_site: Any | None = None
    expiry: Any | None = None

    def to_bidi_dict(self) -> dict:
        """Serialize to the BiDi wire-protocol dict."""
        result: dict = {}
        if self.name is not None:
            result["name"] = self.name
        if self.value is not None:
            result["value"] = self.value.to_bidi_dict() if hasattr(self.value, "to_bidi_dict") else self.value
        if self.domain is not None:
            result["domain"] = self.domain
        if self.path is not None:
            result["path"] = self.path
        if self.http_only is not None:
            result["httpOnly"] = self.http_only
        if self.secure is not None:
            result["secure"] = self.secure
        if self.same_site is not None:
            result["sameSite"] = self.same_site
        if self.expiry is not None:
            result["expiry"] = self.expiry
        return result

    def to_dict(self) -> dict:
        """Backward-compatible alias for to_bidi_dict()."""
        return self.to_bidi_dict()

class BrowsingContextPartitionDescriptor:
    """BrowsingContextPartitionDescriptor.

    The first positional argument is *context* (a browsing-context ID / window
    handle), mirroring how the class is used throughout the test suite:
    ``BrowsingContextPartitionDescriptor(driver.current_window_handle)``.
    """

    def __init__(self, context: Any = None, type: str = "context") -> None:
        self.context = context
        self.type = type

    def to_bidi_dict(self) -> dict:
        return {"type": "context", "context": self.context}

    def to_dict(self) -> dict:
        """Backward-compatible alias for to_bidi_dict()."""
        return self.to_bidi_dict()

@dataclass
class StorageKeyPartitionDescriptor:
    """StorageKeyPartitionDescriptor."""

    type: Any | None = "storageKey"
    user_context: str | None = None
    source_origin: str | None = None

    def to_bidi_dict(self) -> dict:
        """Serialize to the BiDi wire-protocol dict."""
        result: dict = {"type": "storageKey"}
        if self.user_context is not None:
            result["userContext"] = self.user_context
        if self.source_origin is not None:
            result["sourceOrigin"] = self.source_origin
        return result

    def to_dict(self) -> dict:
        """Backward-compatible alias for to_bidi_dict()."""
        return self.to_bidi_dict()

class Storage:
    """WebDriver BiDi storage module."""

    def __init__(self, conn) -> None:
        self._conn = conn

    def get_cookies(self, filter=None, partition=None):
        """Execute storage.getCookies and return a GetCookiesResult."""
        if filter and hasattr(filter, "to_bidi_dict"):
            filter = filter.to_bidi_dict()
        if partition and hasattr(partition, "to_bidi_dict"):
            partition = partition.to_bidi_dict()
        params = {
            "filter": filter,
            "partition": partition,
        }
        params = {k: v for k, v in params.items() if v is not None}
        cmd = command_builder("storage.getCookies", params)
        result = self._conn.execute(cmd)
        if result and "cookies" in result:
            cookies = [
                StorageCookie.from_bidi_dict(c)
                for c in result.get("cookies", [])
                if isinstance(c, dict)
            ]
            pk_raw = result.get("partitionKey")
            pk = (
                PartitionKey(
                    user_context=pk_raw.get("userContext"),
                    source_origin=pk_raw.get("sourceOrigin"),
                )
                if isinstance(pk_raw, dict)
                else None
            )
            return GetCookiesResult(cookies=cookies, partition_key=pk)
        return GetCookiesResult(cookies=[], partition_key=None)
    def set_cookie(self, cookie=None, partition=None):
        """Execute storage.setCookie."""
        if cookie and hasattr(cookie, "to_bidi_dict"):
            cookie = cookie.to_bidi_dict()
        if partition and hasattr(partition, "to_bidi_dict"):
            partition = partition.to_bidi_dict()
        params = {
            "cookie": cookie,
            "partition": partition,
        }
        params = {k: v for k, v in params.items() if v is not None}
        cmd = command_builder("storage.setCookie", params)
        result = self._conn.execute(cmd)
        if isinstance(result, dict):
            pk_raw = result.get("partitionKey")
            pk = (
                PartitionKey(
                    user_context=pk_raw.get("userContext"),
                    source_origin=pk_raw.get("sourceOrigin"),
                )
                if isinstance(pk_raw, dict)
                else None
            )
            return SetCookieResult(partition_key=pk)
        return result
    def delete_cookies(self, filter=None, partition=None):
        """Execute storage.deleteCookies."""
        if filter and hasattr(filter, "to_bidi_dict"):
            filter = filter.to_bidi_dict()
        if partition and hasattr(partition, "to_bidi_dict"):
            partition = partition.to_bidi_dict()
        params = {
            "filter": filter,
            "partition": partition,
        }
        params = {k: v for k, v in params.items() if v is not None}
        cmd = command_builder("storage.deleteCookies", params)
        result = self._conn.execute(cmd)
        if isinstance(result, dict):
            pk_raw = result.get("partitionKey")
            pk = (
                PartitionKey(
                    user_context=pk_raw.get("userContext"),
                    source_origin=pk_raw.get("sourceOrigin"),
                )
                if isinstance(pk_raw, dict)
                else None
            )
            return DeleteCookiesResult(partition_key=pk)
        return result
