declare default element namespace "http://www.tei-c.org/ns/1.0";
declare namespace tei = "http://www.tei-c.org/ns/1.0";
declare namespace abv = "http://ossetic-studies.org/ns/abaevdict";

import module namespace abv-m = 'http://ossetic-studies.org/ns/abaevdict-mod' at './abv-mod.xqm';

declare function local:genLookupEntry($doc as document-node()) 
as element()?
{
  let $node := $doc/tei:TEI/tei:text/tei:body/tei:entry
  let $phon := normalize-space(data($node/tei:form[1]/tei:orth[1]))
  
  return element entry {
    attribute xml:id { $node/@xml:id },
    attribute filename { db:path($doc) },
    if ($node/@n) then attribute n {$node/@n} else (),
    text { $phon }
  }
};

let $entries := for $doc in collection('abaevdict')
                return local:genLookupEntry($doc)
                
let $subentries := for $doc in collection('abaevdict')
                  for $re in $doc//re
                  let $etext := normalize-space(data($re/form[1]/orth[1]))
                  where $etext != '' and not($etext = $entries/text())
                        and not(starts-with($etext,','))
                        and string-to-codepoints($etext)[1] != string-to-codepoints(' ')
                  return <re corresp="{$doc/tei:TEI/tei:text/tei:body/tei:entry/@xml:id}">{$etext}</re>

let $tbl := <table>
{for $e in ($entries,$subentries)
order by abv-m:sortKey($e/text())
  return $e}
</table>

return db:put('abaevdict_index', $tbl, 'lookup.xml')