/* shinyStorePlus v1.4 | (c) Written by Obi Obianom, www.obianom.com | all rights reserved */

const db=new Dexie("shinystoreplusmiscdara"),observeonce="observeonce";async function cleanupOldEntries(){try{let e=new Date;e.setDate(e.getDate()-30),await db.inputData.where("timestamp").below(e).delete()}catch(a){console.error("Error cleaning up old entries:",a)}}async function saveToIndexedDB(e){try{await db.inputData.add({inputname:e,value:!0,timestamp:new Date})}catch(a){console.error("Error saving data:",a)}}async function clearIndexedDB(e){try{await db.inputData.where("inputname").equals(e).delete(),console.log("Cleared data from database")}catch(a){console.error("Error clearing data:",a)}}db.version(1).stores({inputData:"++id, inputname, value, timestamp"}),$(document).on("shiny:connected",function(e){Shiny.addCustomMessageHandler("stpobserveEventsOnce",async function(e){if("restart"==e&&clearIndexedDB(observeonce),"start"==e)try{await cleanupOldEntries();let a=await db.inputData.where("inputname").equals(observeonce).last();a?Shiny.setInputValue("shinystoreplusalreadyExecuted",!0):(Shiny.setInputValue("shinystoreplusalreadyExecuted",!1),saveToIndexedDB(observeonce))}catch(t){console.error("Error loading data:",t)}})});
