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';

let $mentioned-index := doc('abaevdict_index/mentioned_en.xml')
let $lang-index := doc('abaevdict_index/langnames.xml')

let $xml := 
<csv>
{
  for $lang in $mentioned-index/lang-index[1]/lang
  let $lang-id as xs:string := $lang/@id
  let $lang-rec := $lang-index/csv[1]/record[code=$lang-id]
  let $lang-ru as xs:string := $lang-rec/ru[1]/text()
  let $lang-en as xs:string := $lang-rec/en[1]/text()
  let $lang-lat as xs:float := $lang-rec/lat[1]/text()
  let $lang-long as xs:float := $lang-rec/long[1]/text()
  let $lang-family as xs:string := $lang-rec/family[1]/text()
  let $lang-genus as xs:string := $lang-rec/genus[1]/text()
  
  for $word in $lang/word
  let $text as xs:string:= $word/@text
  
  for $entry in $word/entry
  let $entry-id as xs:string := $entry/@id
  let $entry-text as xs:string := abv-m:entry-form-by-id($entry-id)
  let $entry-gloss as xs:string := string-join(doc(`abaevdict_en/xml/{$entry-id}.xml`)/tei:entry[1]/tei:sense//text())
  let $entry-dialect as xs:string := doc(`abaevdict_en/xml/{$entry-id}.xml`)/tei:entry[1]/string(@xml:lang)
  let $word-gloss as xs:string := string-join($entry/ref/gloss/string(@text), '; ')
  return <record>
            <word.text>{$text}</word.text>
            <word.gloss>{$word-gloss}</word.gloss>  
            <lang.id>{$lang-id}</lang.id>
            <lang.ru>{$lang-ru}</lang.ru>
            <lang.en>{$lang-en}</lang.en>
            <lang.family>{$lang-family}</lang.family>
            <lang.genus>{$lang-genus}</lang.genus>            
            <entry.id>{$entry-id}</entry.id>
            <entry.text>{$entry-text}</entry.text>
            <entry.sense>{$entry-gloss}</entry.sense>
            <entry.dialect>{$entry-dialect}</entry.dialect>
            <lang.lat>{$lang-lat}</lang.lat>
            <lang.long>{$lang-long}</lang.long>
         </record>
}
</csv>

return csv:serialize($xml, {'header': true()})