(define
  search-languages
  (list
    "Python"
    "JavaScript"
    "TypeScript"
    "Rust"
    "Go"
    "Java"
    "C"
    "C++"
    "Ruby"
    "Elixir"
    "Haskell"
    "Clojure"
    "Scala"
    "Kotlin"
    "Swift"
    "Zig"
    "OCaml"
    "Lua"
    "Perl"
    "PHP"))

(define value-select-data {:Languages (list "Python" "JavaScript" "Rust" "Go") :Databases (list "PostgreSQL" "Redis" "SQLite" "MongoDB") :Frameworks (list "Quart" "FastAPI" "React" "Svelte")})

(define
  taken-emails
  (list "admin@example.com" "test@example.com" "user@example.com"))

(define tab-content {:tab2 "Here are the details. Version: 1.0.0, Build: 2024-01-15, Engine: sx" :tab1 "Welcome to the Overview tab. This is the default tab content loaded via sx-get." :tab3 "Recent history: Initial release, Added component caching, Wire format v2"})

(define kbd-actions {:s "Search panel activated" :n "New item created" :h "Help panel opened"})

(define
  anim-colors
  (list
    "bg-violet-100"
    "bg-emerald-100"
    "bg-blue-100"
    "bg-amber-100"
    "bg-rose-100"))

(define edit-row-defaults {:1 {:stock "142" :id "1" :price "19.99" :name "Widget A"} :2 {:stock "89" :id "2" :price "24.50" :name "Widget B"} :3 {:stock "305" :id "3" :price "12.00" :name "Widget C"} :4 {:stock "67" :id "4" :price "45.00" :name "Widget D"}})

(define bulk-user-defaults {:1 {:email "alice@example.com" :status "active" :id "1" :name "Alice Chen"} :2 {:email "bob@example.com" :status "inactive" :id "2" :name "Bob Rivera"} :3 {:email "carol@example.com" :status "active" :id "3" :name "Carol Zhang"} :5 {:email "eve@example.com" :status "active" :id "5" :name "Eve Larsson"} :4 {:email "dan@example.com" :status "inactive" :id "4" :name "Dan Okafor"}})

(defhandler
  ex-click
  :path "/sx/(geography.(hypermedia.(example.(api.click))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((now (helper "now" "%Y-%m-%d %H:%M:%S")))
    (<>
      (~examples/click-result :time now)
      (~docs/oob-code
        :target-id "click-comp"
        :text (helper "component-source" "~examples/click-result"))
      (~docs/oob-code
        :target-id "click-wire"
        :text (str "(~examples/click-result :time \"" now "\")")))))

(defhandler
  ex-form
  :path "/sx/(geography.(hypermedia.(example.(api.form))))"
  :method :post
  :csrf false
  :returns "element"
  (&key)
  (let
    ((name (helper "request-form" "name" "")))
    (<>
      (~examples/form-result :name name)
      (~docs/oob-code
        :target-id "form-comp"
        :text (helper "component-source" "~examples/form-result"))
      (~docs/oob-code
        :target-id "form-wire"
        :text (str "(~examples/form-result :name \"" name "\")")))))

(defhandler
  ex-poll
  :path "/sx/(geography.(hypermedia.(example.(api.poll))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((prev (helper "state-get" "ex-poll-n" 0)))
    (let
      ((n (+ prev 1)))
      (helper "state-set!" "ex-poll-n" n)
      (let
        ((now (helper "now" "%H:%M:%S")) (count (if (< n 10) n 10)))
        (<>
          (~examples/poll-result :time now :count count)
          (~docs/oob-code
            :target-id "poll-comp"
            :text (helper "component-source" "~examples/poll-result"))
          (~docs/oob-code
            :target-id "poll-wire"
            :text (str "(~examples/poll-result :time \"" now "\" :count " count ")")))))))

(defhandler
  ex-delete
  :path "/sx/(geography.(hypermedia.(example.(api.(delete.<sx:item_id>)))))"
  :method :delete
  :csrf false
  :returns "element"
  (&key item-id)
  (<>
    (~docs/oob-code
      :target-id "delete-comp"
      :text (helper "component-source" "~examples/delete-row"))
    (~docs/oob-code
      :target-id "delete-wire"
      :text "(empty — row removed by outerHTML swap)")))

(defhandler
  ex-edit-form
  :path "/sx/(geography.(hypermedia.(example.(api.edit))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((value (helper "request-arg" "value" "")))
    (<>
      (~examples/inline-edit-form :value value)
      (~docs/oob-code
        :target-id "edit-comp"
        :text (helper "component-source" "~examples/inline-edit-form"))
      (~docs/oob-code
        :target-id "edit-wire"
        :text (str "(~examples/inline-edit-form :value \"" value "\")")))))

(defhandler
  ex-edit-save
  :path "/sx/(geography.(hypermedia.(example.(api.edit))))"
  :method :post
  :csrf false
  :returns "element"
  (&key)
  (let
    ((value (helper "request-form" "value" "")))
    (<>
      (~examples/inline-view :value value)
      (~docs/oob-code
        :target-id "edit-comp"
        :text (helper "component-source" "~examples/inline-view"))
      (~docs/oob-code
        :target-id "edit-wire"
        :text (str "(~examples/inline-view :value \"" value "\")")))))

(defhandler
  ex-edit-cancel
  :path "/sx/(geography.(hypermedia.(example.(api.edit-cancel))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((value (helper "request-arg" "value" "")))
    (<>
      (~examples/inline-view :value value)
      (~docs/oob-code
        :target-id "edit-comp"
        :text (helper "component-source" "~examples/inline-view"))
      (~docs/oob-code
        :target-id "edit-wire"
        :text (str "(~examples/inline-view :value \"" value "\")")))))

(defhandler
  ex-oob
  :path "/sx/(geography.(hypermedia.(example.(api.oob))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((now (helper "now" "%H:%M:%S")))
    (<>
      (p (~tw :tokens "text-emerald-600 font-medium") "Box A updated!")
      (p (~tw :tokens "text-sm text-stone-500") (str "at " now))
      (div
        :id "oob-box-b"
        :sx-swap-oob "innerHTML"
        (p
          (~tw :tokens "text-violet-600 font-medium")
          "Box B updated via OOB!")
        (p (~tw :tokens "text-sm text-stone-500") (str "at " now)))
      (~docs/oob-code
        :target-id "oob-wire"
        :text (str
          "(<> (p ... \"Box A updated!\") (div :id \"oob-box-b\" :sx-swap-oob \"innerHTML\" (p ... \"Box B updated!\")))")))))

(defhandler
  ex-lazy
  :path "/sx/(geography.(hypermedia.(example.(api.lazy))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((now (helper "now" "%H:%M:%S")))
    (<>
      (~examples/lazy-result :time now)
      (~docs/oob-code
        :target-id "lazy-comp"
        :text (helper "component-source" "~examples/lazy-result"))
      (~docs/oob-code
        :target-id "lazy-wire"
        :text (str "(~examples/lazy-result :time \"" now "\")")))))

(defhandler
  ex-scroll
  :path "/sx/(geography.(hypermedia.(example.(api.scroll))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((page (helper "request-arg" "page" "2")))
    (let
      ((pg (parse-int page)) (start (+ (* (- (parse-int page) 1) 5) 1)))
      (<>
        (map
          (fn
            (i)
            (div
              (~tw
                :tokens "px-4 py-3 border-b border-stone-100 text-sm text-stone-700")
              (str "Item " i " — loaded from page " page)))
          (range start (+ start 5)))
        (if
          (<= (+ pg 1) 6)
          (div
            :id "scroll-sentinel"
            :sx-get (str
              "/sx/(geography.(hypermedia.(example.(api.scroll))))?page="
              (+ pg 1))
            :sx-trigger "intersect once"
            :sx-target "#scroll-items"
            :sx-swap "beforeend"
            (~tw :tokens "p-3 text-center text-stone-400 text-sm")
            "Loading more...")
          (div
            (~tw :tokens "p-3 text-center text-stone-500 text-sm font-medium")
            "All items loaded."))
        (~docs/oob-code
          :target-id "scroll-wire"
          :text (str "(items for page " page " + sentinel)"))))))

(defhandler
  ex-progress-start
  :path "/sx/(geography.(hypermedia.(example.(api.progress-start))))"
  :method :post
  :csrf false
  :returns "element"
  (&key)
  (let
    ((prev-job (helper "state-get" "ex-job-counter" 0)))
    (let
      ((n (+ prev-job 1)))
      (helper "state-set!" "ex-job-counter" n)
      (let
        ((job-id (str "job-" n)))
        (helper "state-set!" (str "ex-job-" job-id) 0)
        (<>
          (~examples/progress-status :percent 0 :job-id job-id)
          (~docs/oob-code
            :target-id "progress-comp"
            :text (helper "component-source" "~examples/progress-status"))
          (~docs/oob-code
            :target-id "progress-wire"
            :text (str "(~examples/progress-status :percent 0 :job-id \"" job-id "\")")))))))

(defhandler
  ex-progress-status
  :path "/sx/(geography.(hypermedia.(example.(api.progress-status))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((job-id (helper "request-arg" "job" "")))
    (let
      ((current (helper "state-get" (str "ex-job-" job-id) 0)))
      (let
        ((next (if (>= (+ current (random-int 15 30)) 100) 100 (+ current (random-int 15 30)))))
        (helper "state-set!" (str "ex-job-" job-id) next)
        (<>
          (~examples/progress-status :percent next :job-id job-id)
          (~docs/oob-code
            :target-id "progress-comp"
            :text (helper "component-source" "~examples/progress-status"))
          (~docs/oob-code
            :target-id "progress-wire"
            :text (str
              "(~examples/progress-status :percent "
              next
              " :job-id \""
              job-id
              "\")")))))))

(defhandler
  ex-search
  :path "/sx/(geography.(hypermedia.(example.(api.search))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((q (helper "request-arg" "q" "")))
    (let
      ((results (if (= q "") search-languages (filter (fn (lang) (contains? (lower lang) (lower q))) search-languages))))
      (<>
        (~examples/search-results :items results :query q)
        (~docs/oob-code
          :target-id "search-comp"
          :text (helper "component-source" "~examples/search-results"))
        (~docs/oob-code
          :target-id "search-wire"
          :text (str "(~examples/search-results :items (list ...) :query \"" q "\")"))))))

(defhandler
  ex-validate
  :path "/sx/(geography.(hypermedia.(example.(api.validate))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((email (helper "request-arg" "email" "")))
    (cond
      (= email "")
      (<>
        (~examples/validation-error :message "Email is required")
        (~docs/oob-code
          :target-id "validate-comp"
          :text (helper "component-source" "~examples/validation-error"))
        (~docs/oob-code
          :target-id "validate-wire"
          :text "(~examples/validation-error :message \"Email is required\")"))
      (not (contains? email "@"))
      (<>
        (~examples/validation-error :message "Invalid email format")
        (~docs/oob-code
          :target-id "validate-comp"
          :text (helper "component-source" "~examples/validation-error"))
        (~docs/oob-code
          :target-id "validate-wire"
          :text "(~examples/validation-error :message \"Invalid email format\")"))
      (some (fn (e) (= (lower e) (lower email))) taken-emails)
      (<>
        (~examples/validation-error :message (str email " is already taken"))
        (~docs/oob-code
          :target-id "validate-comp"
          :text (helper "component-source" "~examples/validation-error"))
        (~docs/oob-code
          :target-id "validate-wire"
          :text (str
            "(~examples/validation-error :message \""
            email
            " is already taken\")")))
      :else (<>
        (~examples/validation-ok :email email)
        (~docs/oob-code
          :target-id "validate-comp"
          :text (helper "component-source" "~examples/validation-ok"))
        (~docs/oob-code
          :target-id "validate-wire"
          :text (str "(~examples/validation-ok :email \"" email "\")"))))))

(defhandler
  ex-validate-submit
  :path "/sx/(geography.(hypermedia.(example.(api.validate-submit))))"
  :method :post
  :csrf false
  :returns "element"
  (&key)
  (let
    ((email (helper "request-form" "email" "")))
    (if
      (or (= email "") (not (contains? email "@")))
      (p
        (~tw :tokens "text-sm text-rose-600 mt-2")
        "Please enter a valid email.")
      (p
        (~tw :tokens "text-sm text-emerald-600 mt-2")
        (str "Form submitted with: " email)))))

(defhandler
  ex-values
  :path "/sx/(geography.(hypermedia.(example.(api.values))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((cat (helper "request-arg" "category" "")))
    (let
      ((items (get value-select-data cat (list))))
      (<>
        (if
          (empty? items)
          (option :value "" "No items")
          (map (fn (i) (option :value i i)) items))
        (~docs/oob-code
          :target-id "values-wire"
          :text (str "(options for \"" cat "\")"))))))

(defhandler
  ex-reset-submit
  :path "/sx/(geography.(hypermedia.(example.(api.reset-submit))))"
  :method :post
  :csrf false
  :returns "element"
  (&key)
  (let
    ((msg (helper "request-form" "message" "(empty)"))
      (now (helper "now" "%H:%M:%S")))
    (<>
      (~examples/reset-message :message msg :time now)
      (~docs/oob-code
        :target-id "reset-comp"
        :text (helper "component-source" "~examples/reset-message"))
      (~docs/oob-code
        :target-id "reset-wire"
        :text (str "(~examples/reset-message :message \"" msg "\" :time \"" now "\")")))))

(defhandler
  ex-editrow-form
  :path "/sx/(geography.(hypermedia.(example.(api.(editrow.<sx:row_id>)))))"
  :method :get
  :returns "element"
  (&key row-id)
  (let
    ((default (get edit-row-defaults row-id {:stock "0" :id row-id :price "0" :name ""})))
    (let
      ((row (helper "state-get" (str "ex-row-" row-id) default)))
      (<>
        (~examples/edit-row-form
          :id (get row "id")
          :name (get row "name")
          :price (get row "price")
          :stock (get row "stock"))
        (~docs/oob-code
          :target-id "editrow-comp"
          :text (helper "component-source" "~examples/edit-row-form"))
        (~docs/oob-code
          :target-id "editrow-wire"
          :text (str "(~examples/edit-row-form :id \"" (get row "id") "\" ...)"))))))

(defhandler
  ex-editrow-save
  :path "/sx/(geography.(hypermedia.(example.(api.(editrow.<sx:row_id>)))))"
  :method :post
  :csrf false
  :returns "element"
  (&key row-id)
  (let
    ((name (helper "request-form" "name" ""))
      (price (helper "request-form" "price" "0"))
      (stock (helper "request-form" "stock" "0")))
    (helper "state-set!" (str "ex-row-" row-id) {:stock stock :id row-id :price price :name name})
    (<>
      (~examples/edit-row-view :id row-id :name name :price price :stock stock)
      (~docs/oob-code
        :target-id "editrow-comp"
        :text (helper "component-source" "~examples/edit-row-view"))
      (~docs/oob-code
        :target-id "editrow-wire"
        :text (str "(~examples/edit-row-view :id \"" row-id "\" ...)")))))

(defhandler
  ex-editrow-cancel
  :path "/sx/(geography.(hypermedia.(example.(api.(editrow-cancel.<sx:row_id>)))))"
  :method :get
  :returns "element"
  (&key row-id)
  (let
    ((default (get edit-row-defaults row-id {:stock "0" :id row-id :price "0" :name ""})))
    (let
      ((row (helper "state-get" (str "ex-row-" row-id) default)))
      (<>
        (~examples/edit-row-view
          :id (get row "id")
          :name (get row "name")
          :price (get row "price")
          :stock (get row "stock"))
        (~docs/oob-code
          :target-id "editrow-comp"
          :text (helper "component-source" "~examples/edit-row-view"))
        (~docs/oob-code
          :target-id "editrow-wire"
          :text (str "(~examples/edit-row-view :id \"" (get row "id") "\" ...)"))))))

(defhandler
  ex-bulk
  :path "/sx/(geography.(hypermedia.(example.(api.bulk))))"
  :method :post
  :csrf false
  :returns "element"
  (&key)
  (let
    ((action (helper "request-arg" "action" "activate"))
      (ids (helper "request-form-list" "ids")))
    (let
      ((new-status (if (= action "activate") "active" "inactive")))
      (for-each
        (fn
          (uid)
          (let
            ((default (get bulk-user-defaults uid nil)))
            (let
              ((user (helper "state-get" (str "ex-bulk-" uid) default)))
              (when
                user
                (helper
                  "state-set!"
                  (str "ex-bulk-" uid)
                  (assoc user "status" new-status))))))
        ids)
      (let
        ((rows (map (fn (uid) (let ((default (get bulk-user-defaults uid {:email "" :status "active" :id uid :name ""}))) (let ((u (helper "state-get" (str "ex-bulk-" uid) default))) (~examples/bulk-row :id (get u "id") :name (get u "name") :email (get u "email") :status (get u "status"))))) (list "1" "2" "3" "4" "5"))))
        (<>
          rows
          (~docs/oob-code
            :target-id "bulk-comp"
            :text (helper "component-source" "~examples/bulk-row"))
          (~docs/oob-code
            :target-id "bulk-wire"
            :text (str "(updated " (len ids) " users to " new-status ")")))))))

(defhandler
  ex-swap-log
  :path "/sx/(geography.(hypermedia.(example.(api.swap-log))))"
  :method :post
  :csrf false
  :returns "element"
  (&key)
  (let
    ((mode (helper "request-arg" "mode" "beforeend"))
      (prev-swap (helper "state-get" "ex-swap-n" 0))
      (now (helper "now" "%H:%M:%S")))
    (let
      ((n (+ prev-swap 1)))
      (helper "state-set!" "ex-swap-n" n)
      (<>
        (div
          (~tw :tokens "px-3 py-2 text-sm text-stone-700")
          (str "[" now "] " mode " (#" n ")"))
        (span
          :id "swap-counter"
          :sx-swap-oob "innerHTML"
          (~tw :tokens "self-center text-sm text-stone-500")
          (str "Count: " n))
        (~docs/oob-code
          :target-id "swap-wire"
          :text (str "(entry + oob counter: " n ")"))))))

(defhandler
  ex-dashboard
  :path "/sx/(geography.(hypermedia.(example.(api.dashboard))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((now (helper "now" "%H:%M:%S")))
    (<>
      (div
        :id "dash-header"
        (~tw :tokens "p-3 bg-violet-50 rounded mb-3")
        (h4 (~tw :tokens "font-semibold text-violet-800") "Dashboard Header")
        (p (~tw :tokens "text-sm text-violet-600") (str "Generated at " now)))
      (div
        :id "dash-stats"
        (~tw :tokens "grid grid-cols-3 gap-3 mb-3")
        (div
          (~tw :tokens "p-3 bg-emerald-50 rounded text-center")
          (p (~tw :tokens "text-2xl font-bold text-emerald-700") "142")
          (p (~tw :tokens "text-xs text-emerald-600") "Users"))
        (div
          (~tw :tokens "p-3 bg-blue-50 rounded text-center")
          (p (~tw :tokens "text-2xl font-bold text-blue-700") "89")
          (p (~tw :tokens "text-xs text-blue-600") "Orders"))
        (div
          (~tw :tokens "p-3 bg-amber-50 rounded text-center")
          (p (~tw :tokens "text-2xl font-bold text-amber-700") "$4.2k")
          (p (~tw :tokens "text-xs text-amber-600") "Revenue")))
      (div
        :id "dash-footer"
        (~tw :tokens "p-3 bg-stone-50 rounded")
        (p (~tw :tokens "text-sm text-stone-500") (str "Last updated: " now)))
      (~docs/oob-code
        :target-id "filter-wire"
        :text (str
          "(<> (div :id \"dash-header\" ...) (div :id \"dash-stats\" ...) (div :id \"dash-footer\" ...))")))))

(defhandler
  ex-tabs
  :path "/sx/(geography.(hypermedia.(example.(api.(tabs.<sx:tab>)))))"
  :method :get
  :returns "element"
  (&key tab)
  (let
    ((content (get tab-content tab (get tab-content "tab1"))))
    (<>
      content
      (div
        :id "tab-buttons"
        :sx-swap-oob "innerHTML"
        (~tw :tokens "flex border-b border-stone-200")
        (~examples/tab-btn
          :tab "tab1"
          :label "Overview"
          :active (if (= tab "tab1") "true" "false"))
        (~examples/tab-btn
          :tab "tab2"
          :label "Details"
          :active (if (= tab "tab2") "true" "false"))
        (~examples/tab-btn
          :tab "tab3"
          :label "History"
          :active (if (= tab "tab3") "true" "false")))
      (~docs/oob-code
        :target-id "tabs-wire"
        :text (str "(content for " tab " + oob tab buttons")))))

(defhandler
  ex-animate
  :path "/sx/(geography.(hypermedia.(example.(api.animate))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((idx (random-int 0 4)) (now (helper "now" "%H:%M:%S")))
    (let
      ((color (nth anim-colors idx)))
      (<>
        (~examples/anim-result :color color :time now)
        (~docs/oob-code
          :target-id "anim-comp"
          :text (helper "component-source" "~examples/anim-result"))
        (~docs/oob-code
          :target-id "anim-wire"
          :text (str "(~examples/anim-result :color \"" color "\" :time \"" now "\")"))))))

(defhandler
  ex-dialog
  :path "/sx/(geography.(hypermedia.(example.(api.dialog))))"
  :method :get
  :returns "element"
  (&key)
  (<>
    (~examples/dialog-modal
      :title "Confirm Action"
      :message "Are you sure you want to proceed? This is a demo dialog rendered entirely with sx components.")
    (~docs/oob-code
      :target-id "dialog-comp"
      :text (helper "component-source" "~examples/dialog-modal"))
    (~docs/oob-code
      :target-id "dialog-wire"
      :text "(~examples/dialog-modal :title \"Confirm Action\" :message \"...\")")))

(defhandler
  ex-dialog-close
  :path "/sx/(geography.(hypermedia.(example.(api.dialog-close))))"
  :method :get
  :returns "element"
  (&key)
  (<>
    (~docs/oob-code :target-id "dialog-wire" :text "(empty — dialog closed)")))

(defhandler
  ex-keyboard
  :path "/sx/(geography.(hypermedia.(example.(api.keyboard))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((key (helper "request-arg" "key" "")))
    (let
      ((action (get kbd-actions key (str "Unknown key: " key))))
      (<>
        (~examples/kbd-result :key key :action action)
        (~docs/oob-code
          :target-id "kbd-comp"
          :text (helper "component-source" "~examples/kbd-result"))
        (~docs/oob-code
          :target-id "kbd-wire"
          :text (str "(~examples/kbd-result :key \"" key "\" :action \"" action "\")"))))))

(defhandler
  ex-putpatch-edit-all
  :path "/sx/(geography.(hypermedia.(example.(api.putpatch-edit-all))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((p (helper "state-get" "ex-profile" {:email "ada@example.com" :role "Engineer" :name "Ada Lovelace"})))
    (<>
      (~examples/pp-form-full
        :name (get p "name")
        :email (get p "email")
        :role (get p "role"))
      (~docs/oob-code
        :target-id "pp-comp"
        :text (helper "component-source" "~examples/pp-form-full"))
      (~docs/oob-code
        :target-id "pp-wire"
        :text (str "(~examples/pp-form-full :name \"" (get p "name") "\" ...)")))))

(defhandler
  ex-putpatch
  :path "/sx/(geography.(hypermedia.(example.(api.putpatch))))"
  :method :put
  :csrf false
  :returns "element"
  (&key)
  (let
    ((name (helper "request-form" "name" ""))
      (email (helper "request-form" "email" ""))
      (role (helper "request-form" "role" "")))
    (helper "state-set!" "ex-profile" {:email email :role role :name name})
    (<>
      (~examples/pp-view :name name :email email :role role)
      (~docs/oob-code
        :target-id "pp-comp"
        :text (helper "component-source" "~examples/pp-view"))
      (~docs/oob-code
        :target-id "pp-wire"
        :text (str "(~examples/pp-view :name \"" name "\" ...)")))))

(defhandler
  ex-putpatch-cancel
  :path "/sx/(geography.(hypermedia.(example.(api.putpatch-cancel))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((p (helper "state-get" "ex-profile" {:email "ada@example.com" :role "Engineer" :name "Ada Lovelace"})))
    (<>
      (~examples/pp-view
        :name (get p "name")
        :email (get p "email")
        :role (get p "role"))
      (~docs/oob-code
        :target-id "pp-comp"
        :text (helper "component-source" "~examples/pp-view"))
      (~docs/oob-code
        :target-id "pp-wire"
        :text (str "(~examples/pp-view :name \"" (get p "name") "\" ...)")))))

(defhandler
  ex-json-echo
  :path "/sx/(geography.(hypermedia.(example.(api.json-echo))))"
  :method :post
  :csrf false
  :returns "element"
  (&key)
  (let
    ((data (helper "request-json")) (ct (helper "request-content-type")))
    (let
      ((body (json-encode data)))
      (<>
        (~examples/json-result :body body :content-type ct)
        (~docs/oob-code
          :target-id "json-comp"
          :text (helper "component-source" "~examples/json-result"))
        (~docs/oob-code
          :target-id "json-wire"
          :text (str
            "(~examples/json-result :body \""
            body
            "\" :content-type \""
            ct
            "\")"))))))

(defhandler
  ex-echo-vals
  :path "/sx/(geography.(hypermedia.(example.(api.echo-vals))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((vals (helper "into" (list) (helper "request-args-all"))))
    (let
      ((filtered (filter (fn (pair) (and (not (= (first pair) "_")) (not (= (first pair) "sx-request")))) vals)))
      (let
        ((items (map (fn (pair) (str (first pair) ": " (nth pair 1))) filtered)))
        (<>
          (~examples/echo-result :label "values" :items items)
          (~docs/oob-code
            :target-id "vals-comp"
            :text (helper "component-source" "~examples/echo-result"))
          (~docs/oob-code
            :target-id "vals-wire"
            :text (str "(~examples/echo-result :label \"values\" :items (list ...))")))))))

(defhandler
  ex-echo-headers
  :path "/sx/(geography.(hypermedia.(example.(api.echo-headers))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((all-headers (helper "into" (list) (helper "request-headers-all"))))
    (let
      ((custom (filter (fn (pair) (starts-with? (first pair) "x-")) all-headers)))
      (let
        ((items (map (fn (pair) (str (first pair) ": " (nth pair 1))) custom)))
        (<>
          (~examples/echo-result :label "headers" :items items)
          (~docs/oob-code
            :target-id "vals-comp"
            :text (helper "component-source" "~examples/echo-result"))
          (~docs/oob-code
            :target-id "vals-wire"
            :text (str "(~examples/echo-result :label \"headers\" :items (list ...))")))))))

(defhandler
  ex-slow
  :path "/sx/(geography.(hypermedia.(example.(api.slow))))"
  :method :get
  :returns "element"
  (&key)
  (sleep 2000)
  (let
    ((now (helper "now" "%H:%M:%S")))
    (<>
      (~examples/loading-result :time now)
      (~docs/oob-code
        :target-id "loading-comp"
        :text (helper "component-source" "~examples/loading-result"))
      (~docs/oob-code
        :target-id "loading-wire"
        :text (str "(~examples/loading-result :time \"" now "\")")))))

(defhandler
  ex-slow-search
  :path "/sx/(geography.(hypermedia.(example.(api.slow-search))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((delay-ms (random-int 500 2000)))
    (sleep delay-ms)
    (let
      ((q (helper "request-arg" "q" "")))
      (<>
        (~examples/sync-result :query q :delay (str delay-ms))
        (~docs/oob-code
          :target-id "sync-comp"
          :text (helper "component-source" "~examples/sync-result"))
        (~docs/oob-code
          :target-id "sync-wire"
          :text (str "(~examples/sync-result :query \"" q "\" :delay \"" delay-ms "\")"))))))

(defhandler
  ex-flaky
  :path "/sx/(geography.(hypermedia.(example.(api.flaky))))"
  :method :get
  :returns "element"
  (&key)
  (let
    ((prev-flaky (helper "state-get" "ex-flaky-n" 0)))
    (let
      ((n (+ prev-flaky 1)))
      (helper "state-set!" "ex-flaky-n" n)
      (if
        (not (= (mod n 3) 0))
        (do (set-response-status 503) "")
        (<>
          (~examples/retry-result
            :attempt (str n)
            :message "Success! The endpoint finally responded.")
          (~docs/oob-code
            :target-id "retry-comp"
            :text (helper "component-source" "~examples/retry-result"))
          (~docs/oob-code
            :target-id "retry-wire"
            :text (str "(~examples/retry-result :attempt \"" n "\" ...)")))))))
