--[=[
Modulo usato prevalentemente per lo script delle forma flesse.

Questo modulo riesce a riconoscere la radice di una parola dalla desinenza separandole
e rendendole riconoscibili da uno script js per poter essere usate successivamente

#################################################################################
Function "get_inserted_text" originally writed by Egor Skriptunoff on stackoverflow.com 
and licensed under cc by-sa 3.0 with attribution required. 

URL: https://stackoverflow.com/a/41514182
#################################################################################

]=]
local getArgs = require('Modulo:Arguments').getArgs

local p = {}

function p.get_inserted_text(old, new, frame)
   local prv = {}
   for o = 0, #old do
      prv[o] = ""
   end
   for n = 1, #new do
      local nxt = {[0] = new:sub(1, n)}
      local nn = new:sub(n, n)
      for o = 1, #old do
         local result
         if nn == old:sub(o, o) then
            result = prv[o-1]
         else
            result = prv[o]..nn
            if #nxt[o-1] <= #result then
               result = nxt[o-1]
            end
         end
         nxt[o] = result
      end
      prv = nxt
   end
   return prv[#old]
end

function p.Differenze(frame)
local args = getArgs(frame)

mascSing = args[1] or args.m
mascPlu = args[2] or args.mp
femmSing = args[3] or args.f
femmPlu = args[4] or args.fp


desMascPlu = (p.get_inserted_text(mascSing,  mascPlu))
desFemmSing = (p.get_inserted_text(mascSing, femmSing))
desFemmPlu = (p.get_inserted_text(mascSing, femmPlu))
desMascSing = (p.get_inserted_text(mascPlu, mascSing))
radice = (p.get_inserted_text(desMascPlu, mascPlu))

	return '<span id="FFdms" style="display:none">' .. desMascSing ..'</span><span id="FFdmp" style="display:none">' .. desMascPlu ..'</span><span id="FFdfs" style="display:none">' ..desFemmSing.. '</span><span id="FFdfp" style="display:none">'..desFemmPlu.. '</span><span id="FFrad" style="display:none">' ..radice ..'</span>' 
end

return p