;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC Sucursal en España SL

(ns app.util.text.ui
  (:require
   [app.main.features :as features]
   [app.main.store :as st]
   [app.util.dom :as dom]))

(defn- get-element
  "Given a DOM node, returns the nearest Element, walking up from text nodes."
  [target]
  (if (and (some? target)
           (not= (.-nodeType target) js/Node.ELEMENT_NODE))
    (.-parentElement target)
    target))

(defn v1-closest-text-editor-content
  [target]
  (when-let [el (get-element target)]
    (.closest ^js el ".public-DraftEditor-content")))

(defn v2-closest-text-editor-content
  [target]
  (when-let [el (get-element target)]
    (.closest ^js el "[data-itype=\"editor\"]")))

(defn closest-text-editor-content
  [target]
  (if (features/active-feature? @st/state "text-editor/v2")
    (v2-closest-text-editor-content target)
    (v1-closest-text-editor-content target)))

(defn some-text-editor-content?
  [target]
  (some? (closest-text-editor-content target)))

(defn v1-get-text-editor-content
  []
  (dom/get-element-by-class "public-DraftEditor-content"))

(defn v2-get-text-editor-content
  []
  (dom/query "[data-itype=\"editor\"]"))

(defn v3-get-text-editor-content
  []
  (dom/get-element "text-editor-wasm-input"))

(defn get-text-editor-content
  []
  (cond
    (features/active-feature? @st/state "text-editor-wasm/v1")
    (v3-get-text-editor-content)

    (features/active-feature? @st/state "text-editor/v2")
    (v2-get-text-editor-content)

    :else
    (v1-get-text-editor-content)))
