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

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

(: MAIN DICTIONARY VIEW COMPONENT :)

(: GET INDIVIDUAL ENTRY :)

declare %rest:path("{$lang}/entries/{$id}")
        %rest:query-param("query", "{$query}")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function entries:entry($lang as xs:string, $id as xs:string, $query as xs:string?) {
          if (count($query) = 0) then
            doc(`abaevdict_{$lang}/html/{$id}.html`)
          else
            let $xml := doc(`abaevdict_{$lang}/xml/{$id}.xml`)/tei:entry[1]
            let $marked := ft:mark(
              xquery:eval(
                `declare namespace tei = "http://www.tei-c.org/ns/1.0";
                 declare namespace abv = "http://ossetic-studies.org/ns/abaevdict";
                 .[{$query}]`, {'': $xml})
            )
            return abv-m:make-html($marked,$lang)
        };
        
declare %rest:path("/entries/{$id}")
        %rest:query-param("query", "{$query}")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function entries:entry($id as xs:string, $query as xs:string?) {
          entries:entry('en', $id, $query)
        };        

(: GET ENTRIES CATALOG :)

declare %rest:path("{$lang}/entries")
        %rest:query-param("per-chunk", "{$per-chunk}", 5)
        %rest:query-param("query", "{$query}")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function entries:entries($lang as xs:string, 
                                 $per-chunk as xs:integer,
                                 $query as xs:string?) {
                  let $docs := abv-m:collection($lang,'xml')/tei:entry
                  let $chosen := if($query) then 
                    xquery:eval(`declare namespace tei = "http://www.tei-c.org/ns/1.0";
                             declare namespace abv = "http://ossetic-studies.org/ns/abaevdict";
                             .[{$query}]`, 
                    {'': $docs})/string(@xml:id)
                  for tumbling window $w in abv-m:lookups()
                    start at $s when true()
                    only end at $e when $e - $s eq ($per-chunk - 1)
                  return
                    <div class="abv-chunk">
                      {
                        for $entry in $w
                        let $id := $entry/@xml:id
                        return
                        <div class="abv-lex"
                         hx-get="/{$lang}/entries/{$id}"
                         hx-target="find article"
                         hx-trigger="intersect once from:closest .abv-chunk"
                         hx-swap="outerHTML focus-scroll:true">
                         {if ($id = $chosen) then 
                          attribute hx-vals {
                            `{'{'}"query": "{$query}"{'}'}`
                          }}                        
                          {
                            <article id="{$id}" 
                                     class="abv-entry">
                            </article>
                          }
                        </div>
                      }
                    </div>
        };
        
declare %rest:path("/entries")
        %rest:query-param("per-chunk", "{$per-chunk}", 5)
        %rest:query-param("query", "{$query}")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function entries:entries($per-chunk as xs:integer,
                                 $query as xs:string?) {
                  entries:entries('en', $per-chunk, $query)
        };        
        
(: LIST OF ENTRIES FOR MENU :)
        
declare %rest:path("{$lang}/entrylist")
        %rest:query-param("filter", "{$filter}")
        %rest:query-param("subentries", "{$subentries}", false())
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5') 
        function entries:entrylist($lang as xs:string, 
                                   $filter as xs:string?,
                                   $subentries as xs:boolean?) {
            <nav id="entrylist">
              <ul>
              {for $e in abv-m:lookups($subentries)
              let $txt := $e/text()
              let $id := if (name($e) = 'entry') then $e/@xml:id else $e/@corresp
              return 
                if ($txt contains text {`{$filter}.*`} using wildcards at start) then
                <li>
                {
                  if (name($e) = 're') then 
                    attribute class {'abv-menu-re'} else (),
                  <a href="#{$id}">
                    {$e/text()}
                  </a>
                }
                </li>
              }
              </ul>
            </nav>
        };
        
declare %rest:path("entrylist")
        %rest:query-param("filter", "{$filter}")
        %rest:query-param("subentries", "{$subentries}", false())
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5') 
        function entries:entrylist($filter as xs:string?,
                                   $subentries as xs:boolean?) {
            entries:entrylist('en',$filter,$subentries)
        };
        
(: QUICK ENTRY LIST SIDEBAR :)

declare function entries:sidebar($lang as xs:string) {
        (<button id="close-leftbar"
        hx-on-click="document.getElementById('leftbar').style.display = 'none'; 
                     document.getElementById('open-leftbar').style.display = 'block'; 
                     document.getElementById('close-leftbar').style.display = 'none'">
          {html:doc('../../../static/chevron-left.svg')}
        </button>,
        <button id="open-leftbar" 
        hx-on-click="document.getElementById('leftbar').style.display = 'block'; 
                     document.getElementById('open-leftbar').style.display = 'none'; 
                     document.getElementById('close-leftbar').style.display = 'block'">
          {html:doc('../../../static/chevron-right.svg')}
        </button>,
        <aside id="leftbar">
          <fieldset>
            <input type="text" id="filter-entries" name="filter"
            hx-get="./entrylist" hx-target="#entrylist" 
            hx-swap="outerHTML"
            hx-trigger="input changed"
            placeholder="{if ($lang = 'ru') then 'Быстрый фильтр' else 'Quick filter…'}"/>
            <label>
              <input id="show-re" 
                     type="checkbox" 
                     role="switch" 
                     name="subentries" value="1"
                     hx-get="./entrylist" hx-target="#entrylist"
                     hx-trigger="change"
                     hx-swap="outerHTML"/>
              {if ($lang = 'ru') then 'Производные' else 'Show subentries'}
            </label>
          </fieldset>
          <nav id="entrylist" 
               hx-get="./entrylist" 
               hx-swap="outerHTML"
               hx-trigger="load">
          </nav>
        </aside>)
};        