(ns com.oly.static-sites.do-blog.pages.components.core
  (:require [reitit.frontend.easy :as rfe]
            [tachyons.buttons :refer [pill-grow]]
            [com.oly.static-sites.do-blog.pages.helpers.utils :refer [extract-tags format-date]]
            ))

(defn ui-tags [meta]
  [:div (map-indexed
         (fn [idx tag]
           [:span.mr2 {:key idx}
            [pill-grow
             {:href (rfe/href :archive {:tag tag})
              :text tag}]])
         (-> meta :filetags extract-tags))])

(defn article-list-author-media-flipped [title articles]
  [:section.mw7.center
   (when title [:h2.athelas.ph3.ph0-l title])
   (map-indexed (fn [idx {:keys [title relative-path description date author src alt thumbnail] :as article}]
                  [:article.pv4.bb.b--black-10.ph3.ph0-l
                   {:keys idx}
                   [:div.flex.flex-column.flex-row-ns
                    [:div.w-100.w-60-ns.pr3-ns.order-2.order-1-ns
                     [:a {:href (str (rfe/href :posts {:path ""}) relative-path)}
                      [:h1.f3.athelas.mt0.lh-title title]]
                     [:p.f5.f4-l.lh-copy.athelas description]]
                    (when thumbnail [:div.pl3-ns.order-1.order-2-ns.mb4.mb0-ns.w-100.w-40-ns
                                     [:img.db {:src thumbnail
                                               :alt alt}]])]
                   [:p.f6.lh-copy.gray.mv0 (when author (str "By " author))
                    (when date [:time.f6.db.gray (format-date date)])]])
                articles)])

(defn title-text-image [{:keys [title content] :as optional} tags]
  [:article.lh-copy.f4.mt-0
   [:h1.f2 title]
   (when tags
     [ui-tags tags])
   (when (:img-src optional)
     [:img.w-100.f5.measure {:src (:img-src optional) :alt (:img-alt optional)}])
   content])
