/* OK ITHAKI V249470 - ANDROID APPASSETS API BRIDGE */
(function(){
  'use strict';
  if(window.__ITHAKI_V249470_API_BRIDGE_INSTALLED__)return;
  window.__ITHAKI_V249470_API_BRIDGE_INSTALLED__=true;
  var CANONICAL='https://ok-ithaki.gr';
  function clean(v){v=String(v||'').trim();try{v=decodeURIComponent(v)}catch(e){}return v.replace(/\/+$/,'')}
  function query(name){try{return new URL(location.href).searchParams.get(name)||''}catch(e){return''}}
  var apiBase=clean(query('server')||query('api')||query('base')||CANONICAL)||CANONICAL;
  var token=String(query('token')||'OKITHAKI_PUBLIC_BETA_TOKEN_SERVER_ONLY_2026');
  var localAsset=location.protocol==='file:'||location.hostname==='appassets.androidplatform.net';
  function pathOf(raw){try{return new URL(String(raw),location.href).pathname||''}catch(e){return''}}
  function shouldRewrite(raw){
    if(!localAsset)return false;
    try{
      var u=new URL(String(raw),location.href),p=u.pathname||'';
      return (u.origin===location.origin||String(raw).charAt(0)==='/') && (/^\/api(?:\/|$)/.test(p)||p==='/health'||p==='/mobile_test'||/^\/ws(?:\/|$)/.test(p));
    }catch(e){return false}
  }
  function absolute(raw){
    if(!shouldRewrite(raw))return String(raw);
    try{var u=new URL(String(raw),location.href);return apiBase+u.pathname+u.search+u.hash}catch(e){return apiBase+String(raw)}
  }
  function apiRequest(raw){return /^\/api(?:\/|$)/.test(pathOf(raw))}
  var originalFetch=window.fetch&&window.fetch.bind(window);
  if(originalFetch){
    window.fetch=function(input,init){
      var raw=typeof input==='string'||input instanceof URL?String(input):String(input&&input.url||'');
      var rewritten=absolute(raw),changed=rewritten!==raw;
      var nextInit=init?Object.assign({},init):{};
      if(changed&&apiRequest(raw)){
        var h=new Headers((init&&init.headers)||(input&&input.headers)||undefined);
        if(!h.has('X-ITHAKI-TOKEN'))h.set('X-ITHAKI-TOKEN',token);
        nextInit.headers=h;
      }
      if(input instanceof Request){
        if(!changed&&!init)return originalFetch(input);
        try{return originalFetch(new Request(rewritten,input),nextInit)}catch(e){return originalFetch(rewritten,nextInit)}
      }
      return originalFetch(changed?rewritten:input,nextInit);
    };
  }
  if(window.XMLHttpRequest){
    var open=XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open=function(method,url){
      var args=[].slice.call(arguments);args[1]=absolute(url);this.__ithakiApi469=apiRequest(url)&&args[1]!==String(url);return open.apply(this,args);
    };
    var send=XMLHttpRequest.prototype.send;
    XMLHttpRequest.prototype.send=function(body){try{if(this.__ithakiApi469)this.setRequestHeader('X-ITHAKI-TOKEN',token)}catch(e){}return send.call(this,body)};
  }
  if(navigator.sendBeacon){var beacon=navigator.sendBeacon.bind(navigator);navigator.sendBeacon=function(url,data){return beacon(absolute(url),data)}}
  if(window.EventSource){var ES=window.EventSource;window.EventSource=function(url,opt){return new ES(absolute(url),opt)};window.EventSource.prototype=ES.prototype}
  if(window.WebSocket){
    var WS=window.WebSocket;
    window.WebSocket=function(url,protocols){
      var v=String(url||'');
      if(localAsset&&v.charAt(0)==='/')v=apiBase.replace(/^http:/,'ws:').replace(/^https:/,'wss:')+v;
      return protocols===undefined?new WS(v):new WS(v,protocols);
    };window.WebSocket.prototype=WS.prototype;
  }
  window.ITHAKI_API_BASE=apiBase;
  window.__ITHAKI_CANONICAL_API_BASE_V249470__=apiBase;window.ITHAKI_ABSOLUTE_MEDIA_URL_V249470=function(raw){try{var u=new URL(String(raw||''),location.href);return /^\/api\/media\/file\//.test(u.pathname)?CANONICAL+u.pathname+u.search+u.hash:String(raw||'')}catch(e){return String(raw||'').indexOf('/api/media/file/')===0?CANONICAL+String(raw||''):String(raw||'')}};
  try{document.documentElement.setAttribute('data-ithaki-api-bridge-v249470',apiBase)}catch(e){}
})();
