-- Wrapper for mw.text Lua functions
--   https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Text_library

mwtext = {}

function table.ipairs(t)
  ret = {}
  for key, value in ipairs(t) do
    ret[key] = value
  end
  return ret
end

function table.pop(t,x)
  ret = t[x]
  t[x] = nil
  return ret
end

function boolean( str )
  -- Interpret string as boolean
  -- * Returns False if nil, "false" or "0"; returns True otherwise
  return not (str == nil or str == "false" or str == "0")
end

function mwtext.decode(frame)
  -- ( s, decodeNamedEntities )
  
  s = frame.args['s'] or frame.args[1]
  decodeNamedEntities = frame.args['decodeNamedEntities'] or frame.args[2]
  
  return mw.text.decode( s, decodeNamedEntities )
end

function mwtext.encode(frame)
  -- ( s, charset )
  
  s = frame.args['s'] or frame.args[1]
  charset = frame.args['charset'] or frame.args[2]
  
  return mw.text.encode( s, charset )
end

function mw.text.killMarkers(frame)
  -- ( s )

  s = frame.args['s'] or frame.args[1]
  
  return mw.text.killMarkers( s )
end

function mwtext.listToText(frame)
  -- ( separator, conjunction, [separatorasconjunction, useparent, ] ... )
  
  separator = frame.args['separator']
  conjunction = frame.args['conjunction']
  separatorasconjunction = boolean(frame.args['separatorasconjunction'])
  useparent = boolean(frame.args['useparent'])
  	
  if separatorasconjunction then
  	conjunction = separator
  end
  
  if not useparent then
  	list = table.ipairs(frame.args)
  else
  	list = table.ipairs(frame:getParent().args)
  end
  
  return mw.text.listToText( list, separator, conjunction )
end

function mwtext.nowiki(frame)
  -- ( s )
  
  s = frame.args['s'] or frame.args[1]
  
  return mw.text.nowiki(s)
end

function mwtext.split(frame)
  -- ( s, pattern, plain )
  
  s = frame.args['s'] or frame.args[1]
  pattern = frame.args['pattern'] or frame.args[2]
  plain = frame.args['plain'] or frame.args[3]
  
  return mw.text.split( s, pattern, plain )
end

function mwtext.gsplit(frame)
  -- ( s, pattern, plain )
  
  s = frame.args['s'] or frame.args[1]
  pattern = frame.args['pattern'] or frame.args[2]
  plain = frame.args['plain'] or frame.args[3]
  
  return mw.text.gsplit( s, pattern, plain )
end

function mwtext.tag(frame)
  -- ( tagname, tagcontent, ... )
  
  attrs = mw.clone(frame.args)
  name = table.pop(attrs, 'tagname')
  content = table.pop(attrs, 'tagcontent')
  
  return mw.text.tag( name, attrs, content )
end

function mwtext.trim(frame)
  -- ( s, charset )

  s = frame.args['s']
  charset = frame.args['charset']
  
  return mw.text.trim( s, charset )
end

function mwtext.truncate(frame)
  -- ( text, length, ellipsis, adjustLength )

  text = frame.args['text'] or frame.args[1]
  length = frame.args['length'] or frame.args[2]
  ellipsis = frame.args['ellipsis'] or frame.args[3]
  adjustLength = frame.args['adjustLength'] or frame.args[4]
  
  return mw.text.truncate( text, length, ellipsis, adjustLength )
end

function mwtext.unstripNoWiki(frame)
  -- ( s )
  
  s = frame.args['s'] or frame.args[1]
  
  return mw.text.unstripNoWiki( s )
end

function mwtext.unstrip(frame)
  -- ( s )

  s = frame.args['s'] or frame.args[1]
  
  return mw.text.unstrip( s )
end

return mwtext