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}