local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local table = _tl_compat and _tl_compat.table or table; local doc = require("cerulean.doc")
local parser = require("cerulean.parser")
local render_builders = require("cerulean.render_builders")
local render_context = require("cerulean.render_context")
local trivia_doc = require("cerulean.trivia_doc")
local type_doc = require("cerulean.type_doc")



local inline_stmt_doc = {}

function inline_stmt_doc.render_return_doc(
   ctx, stmt)

   if stmt.exps == nil or #stmt.exps == 0 then
      local parts = { doc.text("return") }
      trivia_doc.append_inline(parts, parser.inline_trivia(stmt, "after_opener"))
      return doc.concat(parts)
   end

   local return_exp_doc = render_builders.build_exp_list_doc(ctx, stmt.exps)
   local last_exp = stmt.exps[#stmt.exps]
   local last_tc = render_builders.trailing_comment_doc(last_exp)
   local stmt_doc = render_builders.with_leading_inline_trivia(
   stmt, doc.text("return "), return_exp_doc)

   return doc.concat({ stmt_doc, last_tc })
end

function inline_stmt_doc.render_assignment_doc(
   ctx, stmt)

   local function build_var_doc(var)
      local parts = {}
      trivia_doc.append_leading(parts, parser.inline_trivia(var, "leading"))
      table.insert(parts, ctx.render_expr(var))
      trivia_doc.append_inline(parts, parser.inline_trivia(var, "before_separator"))
      return doc.concat(parts)
   end
   local var_docs = {}
   for _, var in ipairs(stmt.vars) do
      table.insert(var_docs, build_var_doc(var))
   end
   local vars_doc = render_builders.build_comma_separated_docs(var_docs)
   local last_var = stmt.vars[#stmt.vars]
   local last_var_tc = render_builders.trailing_comment_doc(last_var)
   local exps_doc = render_builders.build_exp_list_doc(ctx, stmt.exps)
   local eq_prefix
   if #(last_var.trailing_comments or {}) > 0 then
      eq_prefix = doc.concat(
      { vars_doc, last_var_tc, doc.indent_hardline(doc.text("= ")) })

   else
      eq_prefix = doc.concat({ vars_doc, doc.text(" = ") })
   end
   return render_builders.with_leading_inline_trivia(stmt, eq_prefix, exps_doc)
end

local function build_var_text(var_node)
   local var_text = var_node.tk
   if var_node.attribute ~= nil then
      var_text = var_text .. " <" .. var_node.attribute .. ">"
   end
   return var_text
end

local function build_vars_doc(stmt, keyword)
   local parts = { doc.text(keyword .. " ") }
   for index, var_node in ipairs(stmt.vars) do
      trivia_doc.append_leading(parts, parser.inline_trivia(var_node, "leading"))
      table.insert(parts, doc.text(build_var_text(var_node)))
      trivia_doc.append_inline(
      parts, parser.inline_trivia(var_node, "before_separator"))

      if index < #stmt.vars then
         table.insert(parts, doc.text(", "))
      end
   end
   return doc.concat(parts)
end

function inline_stmt_doc.render_declaration_doc(
   ctx, stmt, keyword)

   local vars_doc = build_vars_doc(stmt, keyword)
   local last_var = stmt.vars[#stmt.vars]
   local last_var_tc = render_builders.trailing_comment_doc(last_var)
   local has_last_var_tc = #(last_var.trailing_comments or {}) > 0
   local prefix_doc
   if stmt.decltuple == nil or #stmt.decltuple.tuple == 0 then
      prefix_doc = vars_doc
   else
      prefix_doc = doc.concat(
      { vars_doc, doc.text(": "), type_doc.render(stmt.decltuple) })

   end

   if stmt.exps == nil or #stmt.exps == 0 then
      if has_last_var_tc then
         return doc.concat({ prefix_doc, last_var_tc })
      end
      return prefix_doc
   end

   local function build_init_doc(exp_doc)
      local eq_prefix
      if has_last_var_tc then
         eq_prefix = doc.concat(
         { prefix_doc, last_var_tc, doc.indent_hardline(doc.text("= ")) })

      else
         eq_prefix = doc.concat({ prefix_doc, doc.text(" = ") })
      end
      return render_builders.with_leading_inline_trivia(stmt, eq_prefix, exp_doc)
   end

   if #stmt.exps == 1 then
      return build_init_doc(ctx.render_expr(stmt.exps[1]))
   end

   return build_init_doc(render_builders.build_exp_list_doc(ctx, stmt.exps))
end

return inline_stmt_doc
