!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self)["@designbycode/theme-manager"]=t()}(this,(function(){"use strict";var e=Object.defineProperty,t=(t,r,n)=>((t,r,n)=>r in t?e(t,r,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[r]=n)(t,"symbol"!=typeof r?r+"":r,n);return class{constructor(e=[]){t(this,"db",null),t(this,"mediaQuery"),t(this,"themes"),this.mediaQuery=window.matchMedia("(prefers-color-scheme: dark)"),this.themes=new Set(["system","dark","light",...e]),this.initDB()}initDB(){const e=indexedDB.open("theme-db",1);e.onupgradeneeded=e=>{var t;this.db=e.target.result;const r=null==(t=this.db)?void 0:t.createObjectStore("theme",{keyPath:"id"});null==r||r.createIndex("id","id",{unique:!0})},e.onsuccess=e=>{this.db=e.target.result,this.getCurrentTheme()}}getCurrentTheme(){if(this.db){this.db.transaction("theme","readonly").objectStore("theme").get("theme").onsuccess=e=>{const t=e.target.result;this.currentTheme=t?t.value:"system"}}}get currentTheme(){return document.documentElement.getAttribute("data-theme")||"system"}set currentTheme(e){this.changeTheme(e),this.updateAriaCurrent(e)}changeTheme(e="system"){if(this.themes.has(e)||(e="system"),Array.from(this.themes).forEach((e=>{document.documentElement.classList.remove(e)})),"system"===e){const e=this.mediaQuery.matches?"dark":"light";document.documentElement.classList.add(e),document.documentElement.setAttribute("data-theme",e),this.saveTheme(e)}else document.documentElement.classList.add(e),document.documentElement.setAttribute("data-theme",e),this.saveTheme(e);return e}saveTheme(e){if(this.db){const t=this.db.transaction("theme","readwrite").objectStore("theme").put({id:"theme",value:e});t.onsuccess=()=>{},t.onerror=e=>{console.error("Error saving theme to IndexedDB:",e.target.error)}}}initButtons(){var e;null==(e=document.querySelectorAll("[data-theme-name]"))||e.forEach((e=>{e.addEventListener("click",(e=>{const t=e.target.dataset.themeName;this.currentTheme=t||"system"}))})),this.updateAriaCurrent(this.currentTheme)}updateAriaCurrent(e){var t;null==(t=document.querySelectorAll("[data-theme-name]"))||t.forEach((t=>{const r=t;r.dataset.themeName===e?r.setAttribute("aria-current","true"):r.removeAttribute("aria-current")}))}intToggleButton(){document.querySelectorAll("[data-theme-toggle]").forEach((e=>{e.addEventListener("click",(()=>{let e;e="light"===document.documentElement.getAttribute("data-theme")?"dark":"light",this.currentTheme=e}))}))}}}));