defmodule OutlookWeb.HtmlDocComponent do

   use Phoenix.Component
  # use OutlookWeb, :html
  import OutlookWeb.CoreComponents
  import Phoenix.HTML

  alias Phoenix.LiveView.JS

  attr :tree, :list, required: true
  attr :tunit_tag, :atom, default: :span

  def render_doc(assigns) do
    ~H"""
    <.dnode :for={node <- @tree} node={node} tunit_tag={@tunit_tag} />
    """
  end

  def dnode(%{node: %{status: _}} = assigns) do
    ~H"""
      <.dynamic_tag name={@tunit_tag} nid={@node.nid} {Map.get(@node.eph, :attributes, %{})}
      ><%= @node.content |> raw %></.dynamic_tag>
    """
  end

  def dnode(assigns) when assigns.node.name == "img" do
    ~H"""
      <img {@node.attributes |> Map.merge(Map.get(@node.eph, :attributes, %{})) |> Map.merge(%{nid: @node.nid})}>
    """
  end

  def dnode(assigns) when assigns.node.name == "br", do: ~H"<br>"
  def dnode(assigns) when assigns.node.name == "hr", do: ~H"<hr>"

  def dnode(assigns) when assigns.node.type == :element do
    ~H"""
      <.dynamic_tag name={@node.name} nid={@node.nid} {Map.get(@node.eph, :attributes, %{})}
      ><.dnode :for={child_node <- @node.content} node={child_node} tunit_tag={@tunit_tag}
      /></.dynamic_tag>
    """
  end

  def dnode(assigns) when assigns.node.type == :text do
    ~H"<%= @node.content %>"
  end

  def dnode(assigns) when assigns.node.type == :comment do
    ~H"<!--<%= @node.content |> raw %>-->"
  end
end
