declare namespace site = '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';
import module namespace home = 'http://ossetic-studies.org/ns/abaevdict-site/home' at './components/home.xqm';
import module namespace entries = 'http://ossetic-studies.org/ns/abaevdict-site/entries' at './components/entries.xqm';
import module namespace index = 'http://ossetic-studies.org/ns/abaevdict-site/index' at './components/index.xqm';
import module namespace search = 'http://ossetic-studies.org/ns/abaevdict-site/search' at './components/search.xqm';

(: =============================================================== :)
(: ============================ FAVICON ========================== :)
(: =============================================================== :)

declare %rest:path("/favicon.ico")
        %rest:GET
        function site:favicon() {
          fetch:binary('../../static/favicon.ico')
        };

(: HTML HEAD :)

declare function site:head($title as xs:string) {
              <head>
                <title>{$title}</title>
                <script src="/static/htmx.min.js"></script>
                <script src="/static/plotly-3.0.1.min.js" charset="utf-8"></script>
                
                <link rel="stylesheet" href="/static/pico.classless.min.css"/>
                
                <link rel="stylesheet" href="/static/abaev-html.css"/>
              </head>  
};

(: HEADER :)

declare function site:header($lang as xs:string, $href-other as xs:string) {
        <header id="abv-header">
        <nav>
          <ul class="abv-brand">
            <li><img class="abv-logo" src="/static/Abaev_logo.png" alt="Abaevdict logo"></img>
            <strong>{
              switch($lang)
              case 'ru' return 'ИЭСОЯ'
              default return `Abaevdict`
            }</strong><i> β</i></li>            
            <li>{
              (element {if ($lang = 'ru') then 'mark' else 'a'}{
                attribute hx-on-click {"this.href = this.href + window.location.hash;"},
                attribute class {'abv-lang-link'},
                if ($lang != 'ru') then
                (attribute href {`{$href-other}`}) else (),
                'ru'
              }, ' / ',
              element {if ($lang = 'ru') then 'a' else 'mark'}{
                attribute hx-on-click {"this.href = this.href + window.location.hash;"},
                attribute class {'abv-lang-link'},
                if ($lang = 'ru') then 
                (attribute href {`{$href-other}`}) else (),
                'en'
              })
            }
            </li>
          </ul>
          <ul>
            <li><a href="./home">{
              switch($lang)
              case 'ru' return 'Главная'
              default return 'Home'
            }
            </a></li>
            <li><a href="./dictionary">{switch($lang)
              case 'ru' return 'Просмотр'
              default return 'Browse'
            }
            </a></li>
            <li><a href="./search">{switch($lang)
              case 'ru' return 'Поиск'
              default return 'Search'
            }
            </a></li>            
            <li><a href="./index">{switch($lang)
              case 'ru' return 'Указатель'
              default return 'Index'
            }</a></li>
            <li><a href="#">{switch($lang)
              case 'ru' return 'Литература'
              default return 'References'
            }</a></li>
          </ul>
        </nav>
      </header>
};

(: PATHS :)

(: ZERO PATH :)

declare %rest:path("{$lang}")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function site:null($lang as xs:string) {
          site:home($lang)
        };
        
declare %rest:path("/")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function site:null() {
          site:null('en')
        };          

(: HOME PAGE :)

declare %rest:path("{$lang}/home")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function site:home($lang as xs:string) {
          <html>
          {
            site:head(if ($lang = 'ru') then 'ИЭСОЯ В.И. Абаева — Главная'
            else 'Abaevdict — Main'),
            <body>
                {site:header($lang,`/{abv-m:invert-lang($lang)}/home`)}              
                <main>
                  {home:content($lang)}
                </main>            
            </body>
          }
          </html>          
        };
        
declare %rest:path("/home")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function site:home() {
          site:home('en')
        };        

(: DICTIONARY INTERFACE :)

declare %rest:path("{$lang}/dictionary")
        %rest:query-param("query", "{$query}")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function site:dictionary($lang as xs:string, $query as xs:string?) {
            <html>
            {
              site:head(if($lang = 'ru') then 'ИЭСОЯ В.И. Абаева — Просмотр'
              else 'Abaevdict — Browse'),
              <body>  
                {site:header($lang,`/{abv-m:invert-lang($lang)}/dictionary`)}                
                {entries:sidebar($lang)}
                <dialog id="modal_map"/>                
                <main id="abv-dict">
                  {entries:entries($lang,20,$query)}
                </main>
              </body>
            }
            </html>          
        };
        
declare %rest:path("dictionary")
        %rest:query-param("query", "{$query}")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function site:dictionary($query as xs:string?) {
            site:dictionary('en', $query)
        };        

(: INDEX INTERFACE :)

declare %rest:path("{$lang}/index")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function site:index($lang as xs:string) {
            <html>
            {
              site:head(if ($lang = 'ru') then 'ИЭСОЯ В.И. Абаева — Указатель'
              else 'Abaevdict — Index'),
              <body>  
                {site:header($lang,`/{abv-m:invert-lang($lang)}/index`)}              
                <main>
                  {index:content($lang)}
                </main>
              </body>
            }
            </html>             
        };

declare %rest:path("index")
        %rest:GET
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function site:index() {
          site:index('en')
        };
        
(: SEARCH INTERFACE :)

declare %rest:path("{$lang}/search")
        %rest:form-param("pattern", "{$pattern}")
        %rest:form-param("text", "{$text}")
        %rest:GET
        %rest:POST
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function site:search($lang as xs:string, 
                             $pattern as xs:string?, 
                             $text as xs:string?) {
            <html>
            {
              site:head(if ($lang = 'ru') then 'ИЭСОЯ В.И. Абаева — Поиск'
              else 'Abaevdict — Search'),
              <body>  
                {site:header($lang,`/{abv-m:invert-lang($lang)}/search`)}              
                <main>
                  {search:content($lang,$pattern,$text)}
                </main>
              </body>
            }
            </html>             
        };

declare %rest:path("search")
        %rest:form-param("pattern", "{$pattern}")
        %rest:form-param("text", "{$text}")
        %rest:GET
        %rest:POST
        %rest:produces("text/html")
        %output:method("html")
        %output:html-version('5')
        function site:search($pattern as xs:string?, 
                             $text as xs:string?) {
          site:search('en',$pattern,$text)
        };
        
0