const _karing={debug:false,log:function(message,data=null){if(this.debug){console.log(`[Karing]${message}`,data)}},error:function(message,data=null){console.error(`[Karing]${message}`,data)},sleep:function(seconds){return new Promise(resolve=>setTimeout(resolve,seconds*1000))},available:function(){return(window.karing&&typeof window.karing.callHandler==='function')},version:async function(){try{const result=await this.callHandler('version');return result}catch(error){this.error('get version: ',error);return null}},callHandler:function(handler,...args){return new Promise((resolve,reject)=>{try{if(!this.available()){throw new Error('window.karing is not available');}window.karing.callHandler(handler,...args).then((result)=>{resolve(result)}).catch(()=>{const event=new Event('error');self.dispatchEvent(event);if(self.onerror){self.onerror(event)}reject(new Error(`Handler"${handler}"failed`))})}catch(error){reject(error)}})},closeWindow:async function(delayInSeconds=0){try{if(delayInSeconds>0){await new Promise((resolve)=>setTimeout(resolve,delayInSeconds*1000))}const result=await this.callHandler('close');this.log('Window closed:',result);return result}catch(error){this.error('Failed to close the window:',error);throw error;}},prepare:async function(pid,onComplete){try{const result=await this.callHandler('ispPrepare',String(pid));this.log('Prepare complete:',result);if(typeof onComplete==='function'){onComplete(result)}return result}catch(error){this.error('Error during preparation:',error);throw error;}},config:async function(pid,user,url,name){try{if([null,'',0,'None'].includes(pid)){pid=''}else{pid=String(pid)}const result=await this.callHandler('ispInstallConfig',pid,user,url,name);this.log('Configuration complete:',result);return result}catch(error){this.error(`Failed to configure subscription:${error}`);throw error;}},showMessage:function(message,autoClose=true,autoCloseDelay=10){const modalMessage=document.querySelector('#karing-modal-message');if(modalMessage){modalMessage.textContent=message}else{const modalHTML=`

${message}

`;const body=document.querySelector('body');body.insertAdjacentHTML('beforeend',modalHTML)}const confirmButton=document.querySelector('#karing-modal-confirm');confirmButton.addEventListener('click',()=>{const modalOverlay=document.querySelector('#karing-modal-overlay');if(modalOverlay){modalOverlay.remove()}if(autoClose){this.closeWindow()}});if(autoCloseDelay>0){setTimeout(()=>{const modalOverlay=document.querySelector('#karing-modal-overlay');if(modalOverlay){modalOverlay.remove()}},1000*autoCloseDelay)}},toast:async function(...args){return this.showMessage(...args)},get:function(url,options={}){const opts={...{'with_bearer':false,'author':'authorization',},...options};const token=localStorage.getItem(opts.author);const headers=new Headers();if(token){if(opts.with_bearer){token=`Bearer ${token}`}headers.append(opts.author,token)}return fetch(url,{method:'GET',headers:headers}).then(response=>{if(!response.ok){throw new Error('Request failed, status code: '+response.status);}return response.json()}).then(data=>{this.log('fetch data:',data);if(data){return data}else{this.log('fetch return empty!');return false}}).catch(error=>{this.error('Request failed: ',error);return false})}};