/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/@supersami/rn-foreground-service@2.2.5/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
import{NativeModules,AppRegistry,DeviceEventEmitter,NativeEventEmitter,Alert}from"react-native";const ForegroundServiceModule=NativeModules.ForegroundService,NotificationConfig={},TaskConfig={};class ForegroundService{static registerForegroundTask(e,t){AppRegistry.registerHeadlessTask(e,(()=>t))}static async startService(e){return console.log("Start Service Triggered"),await ForegroundServiceModule.startService(e)}static async updateNotification(e){return console.log(" Update Service Triggered"),await ForegroundServiceModule.updateNotification(e)}static async cancelNotification(e){return console.log("Cancel Service Triggered"),await ForegroundServiceModule.cancelNotification({id:e})}static async stopService(){return console.log("Stop Service Triggered"),await ForegroundServiceModule.stopService()}static async stopServiceAll(){return await ForegroundServiceModule.stopServiceAll()}static async runTask(e){return await ForegroundServiceModule.runTask(e)}static async isRunning(){return await ForegroundServiceModule.isRunning()}}const randHashString=e=>"x".repeat(e).replace(/[xy]/g,(e=>{let t=16*Math.random()|0;return("x"==e?t:3&t|8).toString(16)}));let tasks={};const samplingInterval=500;let serviceRunning=!1;const deleteTask=e=>{delete tasks[e]},taskRunner=async()=>{try{if(!serviceRunning)return;const e=Date.now();let t=[];Object.entries(tasks).forEach((([r,n])=>{e>=n.nextExecutionTime&&(t.push(Promise.resolve(n.task()).then(n.onSuccess,n.onError)),n.onLoop?n.nextExecutionTime=e+n.delay:deleteTask(r))})),await Promise.all(t)}catch(e){console.log("Error in FgService taskRunner:",e)}},register=({config:{alert:e,onServiceErrorCallBack:t}})=>{if(!serviceRunning)return setupServiceErrorListener({alert:e,onServiceFailToStart:t}),ForegroundService.registerForegroundTask("myTaskName",taskRunner)},start=async({id:e,title:t=e,message:r="Foreground Service Running...",ServiceType:n,vibration:o=!1,visibility:i="public",icon:s="ic_notification",largeIcon:a="ic_launcher",importance:c="max",number:u="1",button:l=!1,buttonText:g="",buttonOnPress:v="buttonOnPress",button2:d=!1,button2Text:p="",button2OnPress:k="button2OnPress",mainOnPress:S="mainOnPress",progress:m,color:y,setOnlyAlertOnce:b})=>{try{serviceRunning?console.log("Foreground service is already running."):(await ForegroundService.startService({id:e,title:t,message:r,ServiceType:n,vibration:o,visibility:i,icon:s,largeIcon:a,importance:c,number:u,button:l,buttonText:g,buttonOnPress:v,button2:d,button2Text:p,button2OnPress:k,mainOnPress:S,progressBar:!!m,progressBarMax:m?.max,progressBarCurr:m?.curr,color:y,setOnlyAlertOnce:b}),serviceRunning=!0,await ForegroundService.runTask({taskName:"myTaskName",delay:500,loopDelay:500,onLoop:!0}))}catch(e){throw e}},update=async({id:e,title:t=e,message:r="Foreground Service Running...",ServiceType:n,vibration:o=!1,visibility:i="public",largeIcon:s="ic_launcher",icon:a="ic_launcher",importance:c="max",number:u="0",button:l=!1,buttonText:g="",buttonOnPress:v="buttonOnPress",button2:d=!1,button2Text:p="",button2OnPress:k="button2OnPress",mainOnPress:S="mainOnPress",progress:m,color:y,setOnlyAlertOnce:b})=>{try{await ForegroundService.updateNotification({id:e,title:t,message:r,ServiceType:n,vibration:o,visibility:i,largeIcon:s,icon:a,importance:c,number:u,button:l,buttonText:g,buttonOnPress:v,button2:d,button2Text:p,button2OnPress:k,mainOnPress:S,progressBar:!!m,progressBarMax:m?.max,progressBarCurr:m?.curr,setOnlyAlertOnce:b,color:y}),serviceRunning||(serviceRunning=!0,await ForegroundService.runTask({taskName:"myTaskName",delay:500,loopDelay:500,onLoop:!0}))}catch(e){throw e}},stop=()=>(serviceRunning=!1,ForegroundService.stopService()),stopAll=()=>(serviceRunning=!1,ForegroundService.stopServiceAll()),is_running=()=>serviceRunning,add_task=(e,{delay:t=5e3,onLoop:r=!0,taskId:n=randHashString(12),onSuccess:o=()=>{},onError:i=()=>{}})=>{const s=typeof e;if("function"!==s)throw`invalid task of type ${s}, expected a function or a Promise`;return tasks[n]||(tasks[n]={task:e,nextExecutionTime:Date.now(),delay:500*Math.ceil(t/500),onLoop:r,taskId:n,onSuccess:o,onError:i}),n},update_task=(e,{delay:t=5e3,onLoop:r=!0,taskId:n=randHashString(12),onSuccess:o=()=>{},onError:i=()=>{}})=>{const s=typeof e;if("function"!==s)throw`invalid task of type ${s}, expected a function or a Promise`;return tasks[n]={task:e,nextExecutionTime:Date.now(),delay:500*Math.ceil(t/500),onLoop:r,taskId:n,onSuccess:o,onError:i},n},remove_task=e=>deleteTask(e),is_task_running=e=>!!tasks[e],remove_all_tasks=()=>tasks={},get_task=e=>tasks[e],get_all_tasks=()=>tasks,eventListener=e=>{let t=DeviceEventEmitter.addListener("notificationClickHandle",e);return function(){t.remove()}},eventEmitter=new NativeEventEmitter(ForegroundServiceModule);export function setupServiceErrorListener({onServiceFailToStart:e,alert:t}){const r=eventEmitter.addListener("onServiceError",(r=>{t&&Alert.alert("Service Error",r),e&&e(),stop()}));return()=>{r.remove()}}const ReactNativeForegroundService={register:register,start:start,update:update,stop:stop,stopAll:stopAll,is_running:is_running,add_task:add_task,update_task:update_task,remove_task:remove_task,is_task_running:is_task_running,remove_all_tasks:remove_all_tasks,get_task:get_task,get_all_tasks:get_all_tasks,eventListener:eventListener};export default ReactNativeForegroundService;
//# sourceMappingURL=/sm/c7024e8ada12b8ef2a0c62f433a77cbca4818ab749b523ebd6e378d154eff16b.map