;; SX docs layout defcomps — fully self-contained via IO primitives.
;; Registered via register_sx_layout in __init__.py.

;; --- Main nav defcomp: static nav items from MAIN_NAV ---
;; @css aria-selected:bg-violet-200 aria-selected:text-violet-900

(defcomp ~sx-main-nav (&key section)
  (let* ((sc "aria-selected:bg-violet-200 aria-selected:text-violet-900")
         (items (list
           (dict :label "Docs" :href "/docs/introduction")
           (dict :label "Reference" :href "/reference/")
           (dict :label "Protocols" :href "/protocols/wire-format")
           (dict :label "Examples" :href "/examples/click-to-load")
           (dict :label "Essays" :href "/essays/sx-sucks")
           (dict :label "Specs" :href "/specs/"))))
    (<> (map (lambda (item)
      (~nav-link
        :href (get item "href")
        :label (get item "label")
        :is-selected (when (= (get item "label") section) "true")
        :select-colours sc))
      items))))

;; --- SX header row ---

(defcomp ~sx-header-row (&key nav child oob)
  (~menu-row-sx :id "sx-row" :level 1 :colour "violet"
    :link-href "/" :link-label "sx"
    :link-label-content (~sx-docs-label)
    :nav nav
    :child-id "sx-header-child"
    :child child
    :oob oob))

;; --- Sub-row for section pages ---

(defcomp ~sx-sub-row (&key sub-label sub-href sub-nav selected oob)
  (~menu-row-sx :id "sx-sub-row" :level 2 :colour "violet"
    :link-href sub-href :link-label sub-label
    :selected selected
    :nav sub-nav
    :oob oob))

;; ---------------------------------------------------------------------------
;; SX home layout (root + sx header)
;; ---------------------------------------------------------------------------

(defcomp ~sx-layout-full (&key section)
  (<> (~root-header-auto)
    (~sx-header-row :nav (~sx-main-nav :section section))))

(defcomp ~sx-layout-oob (&key section)
  (<> (~sx-header-row
        :nav (~sx-main-nav :section section)
        :oob true)
    (~clear-oob-div :id "sx-header-child")
    (~root-header-auto true)))

(defcomp ~sx-layout-mobile (&key section)
  (<> (~mobile-menu-section
        :label "sx" :href "/" :level 1 :colour "violet"
        :items (~sx-main-nav :section section))
    (~root-mobile-auto)))

;; ---------------------------------------------------------------------------
;; SX section layout (root + sx header + sub-row)
;; ---------------------------------------------------------------------------

(defcomp ~sx-section-layout-full (&key section sub-label sub-href sub-nav selected)
  (<> (~root-header-auto)
    (~sx-header-row
      :nav (~sx-main-nav :section section)
      :child (~sx-sub-row :sub-label sub-label :sub-href sub-href
                          :sub-nav sub-nav :selected selected))))

(defcomp ~sx-section-layout-oob (&key section sub-label sub-href sub-nav selected)
  (<> (~oob-header-sx :parent-id "sx-header-child"
        :row (~sx-sub-row :sub-label sub-label :sub-href sub-href
                          :sub-nav sub-nav :selected selected))
    (~sx-header-row
      :nav (~sx-main-nav :section section)
      :oob true)
    (~root-header-auto true)))

(defcomp ~sx-section-layout-mobile (&key section sub-label sub-href sub-nav)
  (<>
    (when sub-nav
      (~mobile-menu-section
        :label (or sub-label section) :href sub-href :level 2 :colour "violet"
        :items sub-nav))
    (~mobile-menu-section
      :label "sx" :href "/" :level 1 :colour "violet"
      :items (~sx-main-nav :section section))
    (~root-mobile-auto)))
