module namespace index = 'http://ossetic-studies.org/ns/abaevdict-site/index';

declare namespace tei = "http://www.tei-c.org/ns/1.0";

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

(: GET LANGUAGE LIST :)

declare %rest:path("{$lang}/index/languages")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function index:languages($lang as xs:string) {
          let $mlangs := doc(`abaevdict_index/langnames.xml`)/csv[1]/record
          for $mlang in $mlangs
              let $mlang-id := $mlang/code/text()
              let $mlang-name := if ($lang = 'ru') then $mlang/ru/text() else $mlang/en/text()
              where $mlang-name != ''
              order by $mlang-name
              return 
              <option value="{$mlang-id}">
              {
                $mlang-name
              }
              </option>
        };
        
declare %rest:path("/index/languages")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function index:languages() {
          index:languages('en')
        };        

(: FORM LIST :)

declare %rest:path("{$lang}/index/forms")
        %rest:query-param("flang", "{$flang}")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function index:forms($lang as xs:string, $flang as xs:string) {
          let $ments := doc(`abaevdict_index/mentioned_{$lang}.xml`)
                        /lang-index[1]/lang[@id=$flang]/word
          for $w in $ments
          let $txt := string($w/@text)
          return <option value="{$txt}">{$txt}</option>
        };  
        
declare %rest:path("index/forms")
        %rest:query-param("flang", "{$flang}")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function index:forms($flang as xs:string) {         
          index:forms('en',$flang)
        };
(: RELATED ENTRY LIST :)

declare %rest:path("{$lang}/index/entries")
        %rest:query-param("flang", "{$flang}")
        %rest:query-param("word", "{$word}")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function index:entries($lang as xs:string, 
                               $flang as xs:string,
                               $word as xs:string) {
          let $entries := doc(`abaevdict_index/mentioned_{$lang}.xml`)
                        /lang-index[1]/lang[@id=$flang]/word[@text=$word]/entry
          for $e in $entries
          let $id := $e/@id
          let $txt := abv-m:entry-form-by-id($id)
          let $query := `./@xml:id = '{$id}' and .//text() contains 
          text '{$word}' using diacritics sensitive`
          return <tr>
                  <td>{string-join(
                    distinct-values($e/ref/gloss/string(@text)),', ')}</td>
                  <td>
                    <a href="./dictionary?query={$query}#{$id}">{$txt}</a>
                  </td>
                  <td>
                    {
                      string-join(data(doc(`abaevdict_{$lang}/xml/{$id}.xml`)
                      /tei:entry[1]/tei:sense/(tei:sense|tei:sense/tei:sense|.)
                      /tei:cit/tei:quote),', ')
                    }
                  </td>
                 </tr>
        };   

declare %rest:path("index/entries")
        %rest:query-param("flang", "{$flang}")
        %rest:query-param("word", "{$word}")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function index:entries($flang as xs:string,
                               $word as xs:string) {
          index:entries('en',$flang,$word)
        };

(: THE MAIN INDEX VIEW :)

declare function index:content($lang as xs:string) {
<html>
<body>
<main>
<div class="index">
  <div class="langs">
    <label>{if ($lang = 'ru') then 'Языки' else 'Languages'}</label>
    <select id="abv-select-lang" size="99999" name="flang"
    hx-get='./index/forms' hx-target="#abv-select-word" hx-trigger="input">
    {index:languages($lang)}
    </select>
  </div>
  <div class="forms">
    <label>{if ($lang = 'ru') then 'Формы' else 'Forms'}</label>
    <select id="abv-select-word" size="99999" name="word"
    hx-get="./index/entries" hx-target="#abv-select-entry" hx-trigger="input"
    hx-include="#abv-select-lang">
    </select>
  </div>
  <div class="entries">
    <label>{if ($lang = 'ru') then 'Лексемы' else 'Lexemes'}</label>
    <table>
      <thead>
        <tr>
          <th scope="col">{if ($lang = 'ru') then 'Глоссы' else 'Gloss'}</th>
          <th scope="col">{if ($lang = 'ru') then 'Лексема' else 'Lemma'}</th>
          <th scope="col">{if ($lang = 'ru') then 'Перевод' else 'Translation'}</th>
        </tr>
      </thead>
      <tbody id="abv-select-entry">
      </tbody>
    </table>
  </div>
</div>
</main>
</body>
</html>  
};