from __future__ import annotations from html import escape from typing import Any from urllib.parse import urlencode from render import layout, money, selected_options_markup WISHLIST_INTRO_PATH = "/hz/wishlist/intro" WISHLIST_INDEX_PATH = "/hz/wishlist/ls" WISHLIST_CREATE_PATH = "/hz/wishlist/create" WISHLIST_RENAME_PATH = "/hz/wishlist/rename" WISHLIST_DELETE_PATH = "/hz/wishlist/delete" WISHLIST_ADD_CHOOSER_PATH = "/hz/wishlist/add" WISHLIST_ADD_ITEM_PATH = "/hz/wishlist/add-item" WISHLIST_REMOVE_ITEM_PATH = "/hz/wishlist/remove-item" WISHLIST_MOVE_TO_CART_PATH = "/hz/wishlist/move-to-cart" WISHLIST_ASSET_ROOT = ( "/static/assets/source-current/2026-07-22/lists-intro" ) WISHLIST_STYLESHEET = "/static/wishlist.css?v=20260722-1" def _wishlist_layout( title: str, body: str, cart_count: int, *, account_name: str | None = None, body_class: str = "", ) -> str: return layout( title, body, cart_count, account_name=account_name, body_class=f"wishlist-surface {body_class}".strip(), extra_head=( f'' ), ) def _signin_href(return_to: str = WISHLIST_INDEX_PATH) -> str: query = urlencode({"openid.return_to": return_to}) return f"/ap/signin?{escape(query, quote=True)}" def _detail_href(list_id: int | str) -> str: query = urlencode({"listID": str(list_id)}) return f"{WISHLIST_INDEX_PATH}?{escape(query, quote=True)}" def wishlist_entry_form( product: dict[str, Any], selected_options: dict[str, str], *, button_label: str = "Add to List", css_class: str = "add-to-list-form", ) -> str: """Build the PDP/search entry form that opens the authenticated chooser. ``data-product-option-field`` lets the existing PDP option controller keep this form's complete selection synchronized with visible variant controls. The form deliberately contains no browser-supplied price or offer metadata. """ asin = escape(str(product["asin"]), quote=True) option_fields = "".join( f'' for label, value in selected_options.items() ) return f"""
{option_fields}
""" def wishlist_intro_page(cart_count: int) -> str: """Render the anonymous Your Lists introduction at desktop and mobile widths.""" signin_href = _signin_href() desktop_benefits = ( ( "save-money.png", "Save money", "Keep products together and check back when prices change.", ), ( "shop-with-friends.png", "Shop with friends", "Keep gift ideas together for birthdays, holidays, and everyday shopping.", ), ( "stay-organized.png", "Stay organized", "Build as many lists as you need and find every saved item quickly.", ), ) desktop_benefit_markup = "".join( f"""

{escape(title)}

{escape(copy)}

""" for image_name, title, copy in desktop_benefits ) desktop_registries = ( ( "baby-registry.jpg", "Baby Registry", "Get ready for your new arrival with everything in one place.", "/gp/browse.html?node=16115931011", ), ( "wedding-registry.jpg", "Wedding Registry", "Create a registry that makes gifting simple for everyone.", "/gp/browse.html?node=16115931011", ), ( "custom-gift.jpg", "Gift List", "Save gift ideas for birthdays, holidays, and other celebrations.", "/gp/browse.html?node=16115931011", ), ) desktop_registry_markup = "".join( f""" {escape(title)}{escape(copy)} """ for image_name, title, copy, href in desktop_registries ) mobile_benefits = ( ("Save time", "Add products now and come back whenever you're ready."), ("Give great gifts", "Keep thoughtful gift ideas together in one place."), ("Check price changes", "See the latest offer each time you open your list."), ("Check current deals", "Keep your favorite products together for easy deal checking."), ) mobile_benefit_markup = "".join( f"""
  • {escape(title)}

    {escape(copy)}

  • """ for title, copy in mobile_benefits ) body = f"""

    Lists & Registries

    Save products you love, keep ideas organized, and plan gifts for the people who matter.

    Why use Lists?

    {desktop_benefit_markup}
    Sign In

    Gift Registries

    {desktop_registry_markup}

    Shopping List

    Save everyday items, compare choices, and buy them when you're ready.

    Wish List

    Collect gift ideas for family, friends, and special occasions.

    Create a Registry

    Baby RegistryBaby Registry Wedding RegistryWedding Registry
    """ return _wishlist_layout( "Amazon Lists", body, cart_count, body_class="wishlist-intro-body", ) def _list_card(wishlist: dict[str, Any]) -> str: list_id = int(wishlist["list_id"]) name = escape(str(wishlist["name"])) item_count = int(wishlist.get("item_count", 0)) item_copy = "1 item" if item_count == 1 else f"{item_count} items" default_badge = ( 'Default' if bool(wishlist.get("is_default")) else "" ) return f"""
    {name}{item_copy} {default_badge}
    """ def wishlist_index_page( wishlists: list[dict[str, Any]], cart_count: int, account_name: str, *, status: str = "", ) -> str: """Render the authenticated list index and the create-list affordance.""" list_markup = "".join(_list_card(wishlist) for wishlist in wishlists) status_markup = ( f'
    {escape(status)}
    ' if status else "" ) body = f"""

    Your Account › Your Lists

    Your Lists

    {status_markup}

    Your lists

    {list_markup}
    """ return _wishlist_layout( "Your Lists", body, cart_count, account_name=account_name, body_class="wishlist-app-body", ) def _list_rail(wishlists: list[dict[str, Any]], active_list_id: int) -> str: links = "".join( f""" {escape(str(wishlist['name']))}{int(wishlist.get('item_count', 0))} """ for wishlist in wishlists ) return f""" """ def _wishlist_item(item: dict[str, Any], list_id: int) -> str: item_id = int(item["item_id"]) asin = escape(str(item["asin"]), quote=True) title = escape(str(item["title"])) image_path = escape(str(item["image_path"]), quote=True) href = escape(str(item["canonical_path"]), quote=True) available_to_cart = bool(item.get("available_to_cart")) raw_price = item.get("price_minor") currency = str(item.get("currency") or "USD") options = selected_options_markup( item, css_class="wishlist-item-options" ) if available_to_cart and isinstance(raw_price, int): offer_markup = f"{money(raw_price, currency)}" cart_control = f"""
    """ availability_copy = '

    In Stock

    ' else: offer_markup = 'Offer unavailable' cart_control = '' availability_copy = '

    No verified local offer for this product.

    ' return f"""
    {escape(str(item['title']), quote=True)}
    {title} {options} {availability_copy}

    Added to this List

    {offer_markup} {cart_control}
    """ def wishlist_detail_page( wishlist: dict[str, Any], wishlists: list[dict[str, Any]], cart_count: int, account_name: str, *, status: str = "", ) -> str: """Render one authenticated list with account-owned item controls.""" list_id = int(wishlist["list_id"]) name = escape(str(wishlist["name"])) items = wishlist.get("items") if not isinstance(items, list): items = [] if items: item_markup = "".join(_wishlist_item(item, list_id) for item in items) else: item_markup = """

    This List is empty

    Browse products and choose Add to List on a product page.

    Continue shopping
    """ status_markup = ( f'
    {escape(status)}
    ' if status else "" ) default_copy = ( 'Default List' if bool(wishlist.get("is_default")) else "" ) delete_control = ( "" if len(wishlists) <= 1 else f"""
    """ ) body = f"""
    {status_markup}
    {_list_rail(wishlists, list_id)}

    {name}

    {default_copy}

    {len(items)} {'item' if len(items) == 1 else 'items'}

    More
    {delete_control}
    {item_markup}
    """ return _wishlist_layout( str(wishlist["name"]), body, cart_count, account_name=account_name, body_class="wishlist-app-body", ) def wishlist_add_chooser_page( product: dict[str, Any], selected_options: dict[str, str], wishlists: list[dict[str, Any]], cart_count: int, account_name: str, ) -> str: """Render a signed-in list chooser without embedding a client price.""" asin = escape(str(product["asin"]), quote=True) title = escape(str(product["title"])) image_path = escape(str(product["image_path"]), quote=True) default_list = next( (wishlist for wishlist in wishlists if wishlist.get("is_default")), wishlists[0] if wishlists else None, ) option_fields = "".join( f'' for label, value in selected_options.items() ) option_markup = selected_options_markup( {"selected_options": selected_options}, css_class="wishlist-chooser-options", ) list_choices = "".join( f""" """ for wishlist in wishlists ) if not list_choices: list_choices = '

    No List is available for this account.

    ' body = f"""
    Your Lists

    Add to List

    {escape(str(product['title']), quote=True)}
    {title}{option_markup}
    {option_fields}
    Choose a List{list_choices}
    Cancel
    """ return _wishlist_layout( "Add to List", body, cart_count, account_name=account_name, body_class="wishlist-chooser-body", )