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

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

declare variable $page:items as xs:integer := 20;

declare %rest:path("{$lang}/dict/page{$p}") 
        (: %rest:GET :) 
        %output:method("html")
        %output:html-version('5')
        function page:dict($lang, $p) {
  let $pagetotal := ceiling(count(collection('abaevdict_en')) div $page:items)
  let $sorted := abv-m:sort-collection(collection(`abaevdict_{$lang}`))
  return 
  <html>
    <head>
      <script src="/static/jquery-3.7.1.min.js">
      </script>    
      <script src="/static/infinite-scroll.pkgd.min.js">
      </script>
      <script src="/static/plotly-3.0.1.min.js" charset="utf-8"></script>
      
      <script src="/static/abaev.js"></script>
    
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.classless.min.css"/>
      <link rel="stylesheet" href="{web:create-url('/static/abaev-html.css')}"/>
      <title>Abaev</title>
    </head>
    <body>
      <header>
        <nav>
          <ul>
            <li><strong>Abaevdict</strong></li>
          </ul>
          <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Dictionary</a></li>
            <li><a href="#">Index</a></li>
            <li><a href="#">References</a></li>
          </ul>
        </nav>
      </header>
      <button id="open-leftbar">
        {html:doc('../static/chevron-right.svg')}
      </button>
      <button id="close-leftbar">
        {html:doc('../static/chevron-left.svg')}
      </button>
      <aside id="leftbar">
        <input type="text" id="filter-entries" placeholder="Quick filter…"/>
        <nav id="entrylist">
          <ul>
          {for $doc in $sorted
           return <li id="link_{$doc/tei:entry[1]/@xml:id}">
                    <a href="by-id/{$doc/tei:entry[1]/@xml:id}">
                    {$doc/tei:entry[1]/tei:form[1]/tei:orth[1]/text()}
                    </a>
                  </li>
          }
          </ul>
        </nav>
      </aside>
      <main>
        {for $doc at $i in $sorted
         where $i > ($p - 1) * $page:items and $i <= $p * $page:items
         let $html := xslt:transform($doc, 
                             doc('../xsl/abaev2html.xsl'), {'lang': $lang})
         return $html}
      </main>
      <footer>
        <nav class="pagination">
        <ul>
          <li>
            {if ($p > 1) then <a href="page{$p - 1}">Previous page</a> else ()}
          </li>
        </ul>
        <!--   <ul><li>Page {$p} of {$pagetotal}</li></ul> -->
        <ul>
          <li>
            {if ($p < $pagetotal) then 
            <a class="pagination__next" href="page{$p + 1}">Next page</a> else ()}
          </li>
        </ul>
        </nav>
      </footer>
      
      <dialog id="modal_map">
        <article>
          <header><button class="abv-close-map" aria-label="Close" rel="prev"></button>
          <p>Map</p>
          </header>
          <p>
            <div id="map_display" style="width:100%;height:80%;"></div>
          </p>
          <footer><button class="abv-close-map">Close</button></footer>
        </article>
      </dialog>
    </body>
  </html>
};

declare %rest:path("{$lang}/dict/by-id/{$id}") 
        (: %rest:GET :) 
        %output:method("html")
        %output:html-version('5')
        function page:by-id($lang, $id) {
          
  let $seq := abv-m:sort-collection(collection(`abaevdict_{$lang}`))
  let $doc-index := index-of($seq, $seq[tei:entry[@xml:id=`{$id}`]])
  let $pagenum := ceiling($doc-index div $page:items)
  return web:redirect(`../page{$pagenum}`, {}, web:decode-url($id))
};

declare %rest:path("{$lang}/dict/map-info/{$entry}") 
        %rest:produces("application/json")
        %rest:GET function page:map-info($lang, $entry) {
        let $ments := abv-m:make-geomap(doc(`abaevdict_{$lang}/{$entry}.xml`),
                                      $lang)
        return json:serialize($ments, {'format': 'xquery'})
};