defmodule Outlook.Translations.Basic do

  alias Outlook.InternalTree.InternalNode
  alias Outlook.InternalTree.TranslationUnit

  def internal_tree_to_tunit_map(tree) do
    collect_translation_units(tree)
    |> Enum.map(fn tunit -> {tunit.nid, tunit} end)
    |> Enum.into(%{})
  end

  defp collect_translation_units([%InternalNode{type: :element} = node | rest]) do
    collect_translation_units(node.content) ++ collect_translation_units(rest)
  end

  defp collect_translation_units([%TranslationUnit{} = tunit | rest]) do
    [tunit | collect_translation_units(rest)]
  end

  defp collect_translation_units([_|rest]) do
    [] ++ collect_translation_units(rest)
  end

  defp collect_translation_units([]), do: []
end
