--[[
authors-block – affiliations block extension for quarto
Copyright (c) 2023 Lorenz A. Kapsner
Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
]]

local List = require("pandoc.List")
local from_utils = require("utils")
local normalize_affiliations = from_utils.normalize_affiliations
local normalize_authors = from_utils.normalize_authors
local from_authors = require("from_author_info_blocks")
local default_marks = from_authors.default_marks
local create_equal_contributors_block = from_authors.create_equal_contributors_block
local create_affiliations_blocks = from_authors.create_affiliations_blocks
local create_correspondence_blocks = from_authors.create_correspondence_blocks
local is_corresponding_author = from_authors.is_corresponding_author
local author_inline_generator = from_authors.author_inline_generator
local create_authors_inlines = from_authors.create_authors_inlines

function Pandoc(doc)
  local meta = doc.meta
  local body = List:new({})
  local mark = function(mark_name)
    return default_marks[mark_name]
  end

  -- Process CRediT roles
  if meta.authors then
    local credit_roles = List:new({})
    for i, author in ipairs(meta.authors) do
      if author.role then
        local roles = List:new({})
        for role, level in pairs(author.role) do
          roles:insert(pandoc.Str(role .. ": " .. stringify(level)))
        end
        if #roles > 0 then
          local author_name = stringify(author.name)
          credit_roles:insert(pandoc.Para({
            pandoc.Str(author_name .. ": "),
            pandoc.Str(table.concat(roles:map(stringify), ", ")),
          }))
        end
      end
    end
    if #credit_roles > 0 then
      body:insert(pandoc.Header(2, pandoc.Str("Author Contributions")))
      body:extend(credit_roles)
    end
  end

  body:extend(create_equal_contributors_block(meta.authors, mark) or {})
  body:extend(create_affiliations_blocks(meta.affiliations, meta) or {})
  body:extend(create_correspondence_blocks(meta.authors, mark) or {})
  body:extend(doc.blocks)

  for _i, author in ipairs(meta.authors) do
    author.test = is_corresponding_author(author)
  end

  meta.affiliations = normalize_affiliations(meta.affiliations or {})
  meta.author = meta.authors:map(normalize_authors(meta.affiliations))
  meta.author = pandoc.MetaInlines(create_authors_inlines(meta.author, mark, meta))
  meta.affiliations = nil

  return pandoc.Pandoc(body, meta)
end
