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 delimited_list_doc = require("cerulean.delimited_list_doc")
local doc = require("cerulean.doc")
local parser = require("cerulean.parser")
local type_doc = require("cerulean.type_doc")

local signature_doc = {}

local function build_signature_item_doc(arg_node)
   local parts = {
      doc.text(arg_node.tk),
   }

   if arg_node.opt == true and arg_node.tk ~= "..." then
      table.insert(parts, doc.text("?"))
   end

   if arg_node.argtype ~= nil then
      table.insert(parts, doc.text(": "))
      table.insert(parts, type_doc.render(arg_node.argtype))
   end

   return doc.concat(parts)
end

local function argument_nodes(args)
   local result = {}
   for _, arg_node in ipairs(args) do
      if arg_node.kind == "argument" then
         table.insert(result, arg_node)
      end
   end
   return result
end



function signature_doc.render_signature_doc(args)
   return delimited_list_doc.render_parens(
   args, argument_nodes(args), build_signature_item_doc)

end

return signature_doc
