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

_      _                 _____  _                     _____   _
| |    (_)               / ____|| |                   |  __ \ | |
  ___ | |__   _  _ __   _   _ | (___  | |_  ___   _ __  ___ | |__) || | _   _  ___
/ __|| '_ \ | || '_ \ | | | | \___ \ | __|/ _ \ | '__|/ _ \|  ___/ | || | | |/ __|
 \__ \| | | || || | | || |_| | ____) || |_| (_) || |  |  __/| |     | || |_| |\__ \
 |___/|_| |_||_||_| |_| \__, ||_____/  \__|\___/ |_|   \___||_|     |_| \__,_||___/
                         __/ |
                        |___/


*/

function initialize(t){let n="app"+hashCode(t.appname),e=new Dexie(n);e.version(1).stores({shinyStoresPlus:"++id,app,var,type,value,created"}),retrieveAllInputs(n,e).then(function(t){t.length&&returnValues(t)}),"boolean"==typeof t.input&&t.input&&initializeAllInputs(n,e),"object"==typeof t.input&&t.input.length&&initializeAllInputs(n,e,t.input),"boolean"==typeof t.output&&t.output&&initializeAllOutputs(n,e)}function initdinput(t,n){let e="app"+hashCode(t.appname),u=new Dexie(e);u.version(1).stores({shinyStoresPlus:"++id,app,var,type,value,created"}),retrieveOneInput(e,u,n).then(function(t){console.clear(),initializeAllInputs(e,u,[n]),t.length&&returnValues(t)})}function initializeAllOutputs(t,n,e=null){$.each([".shiny-text-output.shiny-bound-output"],function(u,a){$(a).each(function(u,l){let o=0,r=this,s=$(r).attr("id"),p=$(r).html();$(r).on("DOMSubtreeModified",function(){p=$(r).html(),getInputById(t,s,a,n).then(function(u){null!=u&&(o=u.id),0==o?setInputById(t,s,a,p,e,n):updateInputById(t,s,a,p,o,e,n)})})})})}function initializeAllInputs(t,n,e=null){$.each(["input.shiny-bound-input","textarea.shiny-bound-input","select.shiny-bound-input","input.sw-switchInput"],function(u,a){$(a).each(function(u,l){let o=0,r=a,s=this,p=$(s).attr("id");null==p&&console.log($(s).attr("name")+"is undefined.");let d=$(s).val();$(s).change(function(){d="checkbox"==$(s).attr("type")?(a="checkbox.shiny-bound-input",$(s).is(":checked")):(a=r,$(s).val()),getInputById(t,p,a,n).then(function(u){null!=u&&(o=u.id),0==o?setInputById(t,p,a,d,e,n):updateInputById(t,p,a,d,o,e,n)})}),$(s).keyup(function(){a=r,d=$(s).val(),getInputById(t,p,a,n).then(function(u){null!=u&&(o=u.id),0==o?setInputById(t,p,a,d,e,n):updateInputById(t,p,a,d,o,e,n)})})})});let u=[],a=[];$.each(["div.checkbox-group-buttons:has(button.checkbtn)","div.shiny-bound-input:has(div.shiny-options-group)","div.shiny-bound-input:has(input[type='radio'])","div.shiny-bound-input:has(input[type='text'])"],function(l,o){$(o).each(function(l,r){let s=0,p=$(this).attr("id"),d=$(this).val();$(this).find('input[type="radio"]').change(function(){o="radio.shiny-bound-input",d=$(this).val(),getInputById(t,p,o,n).then(function(u){null!=u&&(s=u.id),0==s?setInputById(t,p,o,d,e,n):updateInputById(t,p,o,d,s,e,n)})}),u[l]=[],$(this).find('input[type="checkbox"]').each(function(u,a){o="checkboxgroup.shiny-bound-input",d=$(this).val();var l=$(this).attr("name");$(this).change(function(){d=[],document.getElementsByName(l).forEach(t=>{t.checked?d.push(t.value):d=removeItemOnce(d,t.value),d=[...new Set(d)]}),getInputById(t,p,o,n).then(function(u){null!=u&&(s=u.id),0==s?setInputById(t,p,o,d,e,n):updateInputById(t,p,o,d,s,e,n)})})});let c=$(this).find('input[type="text"]');1==c.length&&c.change(function(){o="dateinput.shiny-bound-input",d=$(this).val(),getInputById(t,p,o,n).then(function(u){null!=u&&(s=u.id),0==s?setInputById(t,p,o,d,e,n):updateInputById(t,p,o,d,s,e,n)})}),a[l]=[],2==c.length&&c.each(function(){o="dateinputrange.shiny-bound-input",$(this).change(function(){a[l]=[c.first().val(),c.first().next().next().val()],getInputById(t,p,o,n).then(function(u){null!=u&&(s=u.id),0==s?setInputById(t,p,o,a[l],e,n):updateInputById(t,p,o,a[l],s,e,n)})})})})})}function removeItemOnce(t,n){return -1<(n=t.indexOf(n))&&t.splice(n,1),t}function replaceNonAlphanumeric(t){return t.replace(/[^A-Za-z0-9]/g,"")}function timeIntVal(){return Math.floor(Date.now())}function hashCode(t){let n=0;if(0==t.length)return n;for(i=0;i<t.length;i++)n=(n<<5)-n+(ch=t.charCodeAt(i)),n&=n;return n}function retrieveAllInputs(t,n){return n.shinyStoresPlus.where({app:t}).toArray()}function retrieveOneInput(t,n,e){return n.shinyStoresPlus.where({app:t,var:e}).toArray()}function getInputById(t,n,e,u){let a=u.shinyStoresPlus.where({app:t,var:n});return a.toArray().then(function(t){1<t.length&&u.shinyStoresPlus.delete(t[1].id)}),a.first()}function setInputById(t,n,e,u,a,l){let o=!1;(null==a||a.includes(n))&&(o=!0),o&&l.shinyStoresPlus.add({app:t,var:n,type:e,value:u,created:timeIntVal()}).catch(function(t){console.error("Unable to update the input:"+t.stack)})}function updateInputById(t,n,e,u,a,l,o){let r=!1;(null==l||l.includes(n))&&(r=!0),r&&o.shinyStoresPlus.update(a,{app:t,var:n,value:u,created:timeIntVal()})}function clearDatabase(t){console.log("Clearing storage for "+t+"..."),new Dexie(t).delete()}function sendWarning(t){Shiny.setInputValue("transmittedWarningx0x0ssp",JSON.stringify(t))}function returnValues(t){Shiny.setInputValue("transmittedDatax0x",JSON.stringify(t))}function untilEl(t){return new Promise(n=>{if(document.querySelector(t))return n(document.querySelector(t));let e=new MutationObserver(u=>{document.querySelector(t)&&(e.disconnect(),n(document.querySelector(t)))});e.observe(document.body,{childList:!0,subtree:!0})})}Shiny.addCustomMessageHandler("retriever",function(t){void 0!==t&&initialize(t),Array.isArray(t.dinput)&&t.dinput.forEach(n=>{let e=(t,n,e,u,a)=>{let l=new MutationObserver(t=>{t.forEach(t=>{t.addedNodes.forEach(t=>{if(1===t.nodeType){let a=t.querySelector(`input[id${n?"^":"$"}="${n||e}"]`);a&&(console.log(a.id),initdinput(u,a.id))}})})});l.observe(document.querySelector(".container-fluid, .app-container"),{childList:!0,subtree:!0})},u=".container-fluid, .app-container";if(n.endsWith("*")){let a=n.slice(0,-1);e(u,a,null,t,a)}else if(n.startsWith("*")){let l=n.substring(1);e(u,null,l,t,l)}else untilEl(`#${n}`).then(e=>{initdinput(t,n)})})}),Shiny.addCustomMessageHandler("clearStorage",function(t){clearDatabase("app"+hashCode(t))}),$(document).on("shiny:connected",function(t){let n=new URL(window.location.href);var e={};n.searchParams.forEach(function(t,n){e[n]=t}),Shiny.setInputValue("sSP1locationParams",JSON.stringify(e))});
