 (enable-console-print!)
 
 
-;;;; TODO filter out empty words
 ;;;; TODO precalc badness to hash ?
 
 


(defonce state (atom []))

(defn- set-state    [new-state] (swap! state (fn [] new-state)))
(defn- clear-state  [] (set-state []))
(defn- update-state [new-lines] (set-state (concat @state new-lines)))


;;;;
;;;; implementation

          (map inc (range (count words))) )))) ;; TODO (dec (count words)) ?


(defn- split-word
  "Split words that have length > max-len"
  [max-len word]
  (let [len (count word)]
    (if (<= len max-len)
      [word]
      (concat
        [(.substring word 0 max-len)]
        (split-word max-len (str " -" (.substring word max-len))) ))))
; (defn split-word-test [str]
  ; (let [res (split-word 11 str)]
  ; (println "test(" (count res) ")" res)))
; (split-word-test "aaabbbccc111")
; (split-word-test "aaabbbccc111aaabbbccc111")
; (split-word-test "aaabbbccc111aaabbbccc111aaabbbccc111")


(defn- prepare-paragraph
  [paragraph max-chars-in-line]
  (-> paragraph
         gstring/trim
         (.split #" +")
         (->> (mapv #(split-word max-chars-in-line %))
              flatten
              vec)))

(defn- prepare-text
  "Prepare whole text for justification.
   1) split by paragraphs / treat text as single text flow
   2) trim words
   @return vector of lines, where each line is vector of words"
  [text separate-paragraphs max-chars-in-line]
  (let [paragraphs (if separate-paragraphs
                        (seq (.split text "\n"))
                        [(clojure.string/replace text #"\n" " ")] )]
      (mapv #(prepare-paragraph % max-chars-in-line) paragraphs) ))
; (defn prepare-text-test [str]
  ; (let [res (prepare-text str true 11)]
  ; (println "pt-test(" (count res) ")" res "\n\t\t" (mapv count res) )))
; (prepare-text-test " aa ab ")
; (prepare-text-test "aaabbbccc111aaabbbccc111aaabbbccc111")
; (prepare-text-test "Lorem ipsum dolor sit amet, consectetur adipiscing elit")



;;;;
;;;; public interface
