--[[
ScholarlyMeta – normalize author/affiliation meta variables
Copyright (c) 2017-2021 Albert Krewinkel, Robert Winkler
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 utils = require("pandoc.utils")
local stringify = utils.stringify

local M = {}

local function has_id(id)
  return function(x)
    return x.id == id
  end
end

local function resolve_institutes(institute, known_institutes)
  local unresolved_institutes
  if institute == nil then
    unresolved_institutes = {}
  elseif type(institute) == "string" or type(institute) == "number" then
    unresolved_institutes = { institute }
  else
    unresolved_institutes = institute
  end
  local result = List:new({})
  for i, inst in ipairs(unresolved_institutes) do
    local intermed_val = known_institutes:find_if(has_id(stringify(inst)))
    if intermed_val then
      result[i] = pandoc.MetaString(tostring(intermed_val.index))
    else
      result[i] = pandoc.MetaString(tostring(inst))
    end
  end
  return result
end
M.resolve_institutes = resolve_institutes

return M

