defmodule Outlook.Hyphenation do

  def hyphenate(html, lang) do
    form = get_multipart_form(
      [
        {"api-token", System.get_env("HYPH_API_TOKEN")},
        {"hyph[lang]", String.downcase(lang)},
        {"hyph[text]", html},
      ]
    )

    case HTTPoison.request(
      :post,
      System.get_env("HYPH_URL"),
      form,
      get_multipart_headers()
    ) do
      {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
        body
      _ ->
        # this is poor but for now better than loss of all work
        html
    end
  end

  defp get_multipart_form fields do
    {:multipart, fields}
  end

  defp get_multipart_headers() do
   [
    "Content-Type": "multipart/form-data",
    "Transfer-Encoding": "chunked",
   ]
  end
end
