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

;; --- Blog header (invisible row for blog-header-child swap target) ---

(defcomp ~layouts/header (&key oob)
  (~shared:layout/menu-row-sx :id "blog-row" :level 1
    :link-label-content (div)
    :child-id "blog-header-child" :oob oob))

;; --- Auto-fetching settings header macro ---

(defmacro ~blog-settings-header-auto (oob)
  (quasiquote
    (~shared:layout/menu-row-sx :id "root-settings-row" :level 1
      :link-href (url-for "settings.defpage_settings_home")
      :link-label-content (~header/admin-label)
      :nav (~layouts/settings-nav)
      :child-id "root-settings-header-child"
      :oob (unquote oob))))

;; --- Auto-fetching sub-settings header macro ---

(defmacro ~blog-sub-settings-header-auto (row-id child-id endpoint icon label oob)
  (quasiquote
    (~shared:layout/menu-row-sx :id (unquote row-id) :level 2
      :link-href (url-for (unquote endpoint))
      :link-label-content (~header/sub-settings-label
        :icon (str "fa fa-" (unquote icon))
        :label (unquote label))
      :child-id (unquote child-id)
      :oob (unquote oob))))

;; ---------------------------------------------------------------------------
;; Blog layout (root + blog header)
;; ---------------------------------------------------------------------------

(defcomp ~layouts/full ()
  (<> (~root-header-auto)
    (~layouts/header)))

(defcomp ~layouts/oob ()
  (<> (~layouts/header :oob true)
    (~shared:layout/clear-oob-div :id "blog-header-child")
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; Settings layout (root + settings header)
;; ---------------------------------------------------------------------------

(defcomp ~layouts/settings-layout-full ()
  (<> (~root-header-auto)
    (~blog-settings-header-auto)))

(defcomp ~layouts/settings-layout-oob ()
  (<> (~blog-settings-header-auto true)
    (~shared:layout/clear-oob-div :id "root-settings-header-child")
    (~root-header-auto true)))

(defcomp ~layouts/settings-layout-mobile ()
  (~layouts/settings-nav))

;; ---------------------------------------------------------------------------
;; Cache layout (root + settings + cache sub-header)
;; ---------------------------------------------------------------------------

(defcomp ~layouts/cache-layout-full ()
  (<> (~root-header-auto)
    (~blog-settings-header-auto)
    (~blog-sub-settings-header-auto
      "cache-row" "cache-header-child"
      "settings.defpage_cache_page" "refresh" "Cache")))

(defcomp ~layouts/cache-layout-oob ()
  (<> (~blog-sub-settings-header-auto
        "cache-row" "cache-header-child"
        "settings.defpage_cache_page" "refresh" "Cache" true)
    (~shared:layout/clear-oob-div :id "cache-header-child")
    (~blog-settings-header-auto true)
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; Snippets layout (root + settings + snippets sub-header)
;; ---------------------------------------------------------------------------

(defcomp ~layouts/snippets-layout-full ()
  (<> (~root-header-auto)
    (~blog-settings-header-auto)
    (~blog-sub-settings-header-auto
      "snippets-row" "snippets-header-child"
      "snippets.defpage_snippets_page" "puzzle-piece" "Snippets")))

(defcomp ~layouts/snippets-layout-oob ()
  (<> (~blog-sub-settings-header-auto
        "snippets-row" "snippets-header-child"
        "snippets.defpage_snippets_page" "puzzle-piece" "Snippets" true)
    (~shared:layout/clear-oob-div :id "snippets-header-child")
    (~blog-settings-header-auto true)
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; Menu Items layout (root + settings + menu-items sub-header)
;; ---------------------------------------------------------------------------

(defcomp ~layouts/menu-items-layout-full ()
  (<> (~root-header-auto)
    (~blog-settings-header-auto)
    (~blog-sub-settings-header-auto
      "menu_items-row" "menu_items-header-child"
      "menu_items.defpage_menu_items_page" "bars" "Menu Items")))

(defcomp ~layouts/menu-items-layout-oob ()
  (<> (~blog-sub-settings-header-auto
        "menu_items-row" "menu_items-header-child"
        "menu_items.defpage_menu_items_page" "bars" "Menu Items" true)
    (~shared:layout/clear-oob-div :id "menu_items-header-child")
    (~blog-settings-header-auto true)
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; Tag Groups layout (root + settings + tag-groups sub-header)
;; ---------------------------------------------------------------------------

(defcomp ~layouts/tag-groups-layout-full ()
  (<> (~root-header-auto)
    (~blog-settings-header-auto)
    (~blog-sub-settings-header-auto
      "tag-groups-row" "tag-groups-header-child"
      "blog.tag_groups_admin.defpage_tag_groups_page" "tags" "Tag Groups")))

(defcomp ~layouts/tag-groups-layout-oob ()
  (<> (~blog-sub-settings-header-auto
        "tag-groups-row" "tag-groups-header-child"
        "blog.tag_groups_admin.defpage_tag_groups_page" "tags" "Tag Groups" true)
    (~shared:layout/clear-oob-div :id "tag-groups-header-child")
    (~blog-settings-header-auto true)
    (~root-header-auto true)))

;; ---------------------------------------------------------------------------
;; Tag Group Edit layout (root + settings + tag-groups sub-header with id)
;; ---------------------------------------------------------------------------

(defcomp ~layouts/tag-group-edit-layout-full ()
  (<> (~root-header-auto)
    (~blog-settings-header-auto)
    (~shared:layout/menu-row-sx :id "tag-groups-row" :level 2
      :link-href (url-for "blog.tag_groups_admin.defpage_tag_group_edit"
                          :id (request-view-args "id"))
      :link-label-content (~header/sub-settings-label
        :icon "fa fa-tags" :label "Tag Groups")
      :child-id "tag-groups-header-child")))

(defcomp ~layouts/tag-group-edit-layout-oob ()
  (<> (~shared:layout/menu-row-sx :id "tag-groups-row" :level 2
        :link-href (url-for "blog.tag_groups_admin.defpage_tag_group_edit"
                            :id (request-view-args "id"))
        :link-label-content (~header/sub-settings-label
          :icon "fa fa-tags" :label "Tag Groups")
        :child-id "tag-groups-header-child"
        :oob true)
    (~shared:layout/clear-oob-div :id "tag-groups-header-child")
    (~blog-settings-header-auto true)
    (~root-header-auto true)))

;; --- Settings nav links — uses IO primitives ---

(defcomp ~layouts/settings-nav ()
  (let* ((sc (select-colours))
         (links (list
           (dict :endpoint "menu_items.defpage_menu_items_page" :icon "fa fa-bars" :label "Menu Items")
           (dict :endpoint "snippets.defpage_snippets_page" :icon "fa fa-puzzle-piece" :label "Snippets")
           (dict :endpoint "blog.tag_groups_admin.defpage_tag_groups_page" :icon "fa fa-tags" :label "Tag Groups")
           (dict :endpoint "settings.defpage_cache_page" :icon "fa fa-refresh" :label "Cache"))))
    (<> (map (lambda (lnk)
      (~shared:layout/nav-link
        :href (url-for (get lnk "endpoint"))
        :icon (get lnk "icon")
        :label (get lnk "label")
        :select-colours (or sc "")))
      links))))

;; --- Editor panel wrapper ---

(defcomp ~layouts/editor-panel (&key parts)
  (<> parts))
