{"version":3,"sources":["src/app/services/theme.service.ts"],"sourcesContent":["import {Injectable} from '@angular/core';\n\nexport enum ThemeType {\n default = 'default',\n dark = 'dark',\n}\n\n@Injectable({\n providedIn: 'root',\n})\nexport class ThemeService {\n currentTheme = ThemeType.default;\n\n public init() {\n const theme = localStorage.getItem('theme');\n\n if (theme) {\n this.currentTheme = JSON.parse(theme);\n this.loadTheme(true).then();\n } else {\n this.currentTheme = ThemeType.default;\n this.loadTheme(true).then();\n }\n }\n\n private reverseTheme(theme: string): ThemeType {\n return theme === ThemeType.dark ? ThemeType.default : ThemeType.dark;\n }\n\n private removeUnusedTheme(theme: ThemeType): void {\n document.documentElement.classList.remove(theme);\n\n const removedThemeStyle = document.getElementById(theme);\n if (removedThemeStyle) {\n document.head.removeChild(removedThemeStyle);\n }\n }\n\n private loadCss(href: string, id: string): Promise {\n return new Promise((resolve, reject) => {\n const style = document.createElement('link');\n style.rel = 'stylesheet';\n style.href = href;\n style.id = id;\n style.onload = resolve;\n style.onerror = reject;\n document.head.append(style);\n });\n }\n\n public loadTheme(firstLoad = true): Promise {\n const theme = this.currentTheme;\n\n if (firstLoad) {\n document.documentElement.classList.add(theme);\n }\n\n return new Promise((resolve, reject) => {\n this.loadCss(`${theme}.css`, theme).then(\n (e) => {\n if (!firstLoad) {\n document.documentElement.classList.add(theme);\n }\n this.removeUnusedTheme(this.reverseTheme(theme));\n resolve(e);\n },\n (e) => reject(e),\n );\n });\n }\n\n public toggleTheme(): Promise {\n this.currentTheme = this.reverseTheme(this.currentTheme);\n localStorage.setItem('theme', JSON.stringify(this.currentTheme));\n return this.loadTheme(false);\n }\n}\n"],"names":["ThemeType","ThemeService","constructor","currentTheme","default","init","theme","localStorage","getItem","JSON","parse","loadTheme","then","reverseTheme","dark","removeUnusedTheme","document","documentElement","classList","remove","removedThemeStyle","getElementById","head","removeChild","loadCss","href","id","Promise","resolve","reject","style","createElement","rel","onload","onerror","append","firstLoad","add","e","toggleTheme","setItem","stringify","factory","ɵfac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