;; Market layout defcomps — fully self-contained via IO primitives.
;; Registered via register_sx_layout in layouts.py.

;; ---------------------------------------------------------------------------
;; Auto-fetching market header macro
;; ---------------------------------------------------------------------------

(defmacro ~market-header-auto (oob)
  "Market header row using (market-header-ctx)."
  (quasiquote
    (let ((__mctx (market-header-ctx)))
      (~menu-row-sx :id "market-row" :level 2
        :link-href (get __mctx "link-href")
        :link-label-content (~market-shop-label
          :title (get __mctx "market-title")
          :top-slug (get __mctx "top-slug")
          :sub-div (get __mctx "sub-slug"))
        :nav (~market-desktop-nav-from-data
          :categories (get __mctx "categories")
          :hx-select (get __mctx "hx-select")
          :select-colours (get __mctx "select-colours")
          :all-href (get __mctx "all-href")
          :all-active (get __mctx "all-active")
          :admin-href (get __mctx "admin-href"))
        :child-id "market-header-child"
        :oob (unquote oob)))))

;; ---------------------------------------------------------------------------
;; OOB clear helpers
;; ---------------------------------------------------------------------------

(defcomp ~market-clear-oob ()
  "Clear OOB divs for browse level."
  (<>
    (~clear-oob-div :id "product-admin-row")
    (~clear-oob-div :id "product-admin-header-child")
    (~clear-oob-div :id "product-row")
    (~clear-oob-div :id "product-header-child")
    (~clear-oob-div :id "market-admin-row")
    (~clear-oob-div :id "market-admin-header-child")
    (~clear-oob-div :id "post-admin-row")
    (~clear-oob-div :id "post-admin-header-child")))

(defcomp ~market-clear-oob-admin ()
  "Clear OOB divs for admin level."
  (<>
    (~clear-oob-div :id "product-admin-row")
    (~clear-oob-div :id "product-admin-header-child")
    (~clear-oob-div :id "product-row")
    (~clear-oob-div :id "product-header-child")))

;; ---------------------------------------------------------------------------
;; Browse layout: root + post + market (self-contained)
;; ---------------------------------------------------------------------------

(defcomp ~market-browse-layout-full ()
  (<> (~root-header-auto)
    (~header-child-sx
      :inner (<> (~post-header-auto nil)
               (~market-header-auto nil)))))

(defcomp ~market-browse-layout-oob ()
  (<> (~post-header-auto true)
    (~oob-header-sx :parent-id "post-header-child"
      :row (~market-header-auto nil))
    (~market-clear-oob)
    (~root-header-auto true)))

(defcomp ~market-browse-layout-mobile ()
  (let ((__mctx (market-header-ctx)))
    (get __mctx "mobile-nav")))

;; ---------------------------------------------------------------------------
;; Market admin layout: root + post + market + post-admin (self-contained)
;; ---------------------------------------------------------------------------

(defcomp ~market-admin-layout-full (&key selected)
  (<> (~root-header-auto)
    (~header-child-sx
      :inner (<> (~post-header-auto nil)
               (~market-header-auto nil)
               (~post-admin-header-auto nil selected)))))

(defcomp ~market-admin-layout-oob (&key selected)
  (<> (~market-header-auto true)
    (~oob-header-sx :parent-id "market-header-child"
      :row (~post-admin-header-auto nil selected))
    (~market-clear-oob-admin)
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; Parameterized defcomps — used by renders.py (non-defpage routes)
;; ---------------------------------------------------------------------------

;; Product layout: root + post + market + product
(defcomp ~market-product-layout-full (&key post-header market-header product-header)
  (<> (~root-header-auto)
    (~header-child-sx :inner (<> post-header market-header product-header))))

;; Product admin layout: root + post + market + product + admin
(defcomp ~market-product-admin-layout-full (&key post-header market-header product-header admin-header)
  (<> (~root-header-auto)
    (~header-child-sx :inner (<> post-header market-header product-header admin-header))))

;; OOB wrappers
(defcomp ~market-oob-wrap (&key parts)
  (<> parts))

;; Content wrappers
(defcomp ~market-content-padded (&key content)
  (<> content (div :class "pb-8")))
