import*as i from"./../../../core/common/common.js";import*as n from"./../../../core/host/host.js";import*as u from"./../../../core/root/root.js";var r,a=new Map,d=class h extends EventTarget{setting;#t="default";computedStyleOfHTML=i.Lazy.lazy(()=>window.getComputedStyle(document.documentElement));#s=new Set([document]);#o;#n;#e=()=>this.#h();#i=()=>this.fetchColorsAndApplyHostTheme();constructor(e){super(),this.setting=e,this.#o=window.matchMedia("(prefers-color-scheme: dark)"),this.#n=window.matchMedia("(forced-colors: active)"),this.#o.addEventListener("change",this.#e),this.#n.addEventListener("change",this.#e),e.addChangeListener(this.#e),n.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(n.InspectorFrontendHostAPI.Events.ColorThemeChanged,this.#i)}#a(){this.#o.removeEventListener("change",this.#e),this.#n.removeEventListener("change",this.#e),this.setting.removeChangeListener(this.#e),n.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener(n.InspectorFrontendHostAPI.Events.ColorThemeChanged,this.#i)}static hasInstance(){return typeof r<"u"}static instance(e={forceNew:null,setting:null}){let{forceNew:o,setting:t}=e;if(!r||o){if(!t)throw new Error(`Unable to create theme support: setting must be provided: ${new Error().stack}`);r&&r.#a(),r=new h(t)}return r}addDocumentToTheme(e){this.#s.add(e),this.#c(e)}getComputedValue(e,o=null){let t=a.get(o);t||(t=new Map,a.set(o,t));let s=t.get(e);if(!s){let c=o?window.getComputedStyle(o):this.computedStyleOfHTML();if(typeof c=="symbol")throw new Error(`Computed value for property (${e}) could not be found on documentElement.`);s=c.getPropertyValue(e).trim(),s&&t.set(e,s)}return s}themeName(){return this.#t}#h(){for(let e of this.#s)this.#r(e)}#r(e){let o=window.matchMedia("(forced-colors: active)").matches,t=window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"default",s=this.setting.get()==="systemPreferred"||o;this.#t=s?t:this.setting.get(),e.documentElement.classList.toggle("theme-with-dark-background",this.#t==="dark");let c=i.Settings.moduleSetting("chrome-theme-colors").get();if(u.Runtime.hostConfig.isOffTheRecord===!0)e.documentElement.classList.toggle("baseline-grayscale",!0);else if(c){let m=getComputedStyle(e.body).getPropertyValue("--user-color-source");e.documentElement.classList.toggle("baseline-default",m==="baseline-default"),e.documentElement.classList.toggle("baseline-grayscale",m==="baseline-grayscale")}else e.documentElement.classList.toggle("baseline-grayscale",!0);a.clear(),this.dispatchEvent(new l)}static clearThemeCache(){a.clear()}fetchColorsAndApplyHostTheme(){for(let e of this.#s)this.#c(e)}#c(e){let o=i.Settings.moduleSetting("chrome-theme-colors").get();if(n.InspectorFrontendHost.InspectorFrontendHostInstance.isHostedMode()||!o){this.#r(e);return}let t=e.querySelector("link[href*='//theme/colors.css']"),s=e.createElement("link");s.setAttribute("href",`devtools://theme/colors.css?sets=ui,chrome&version=${new Date().getTime().toString()}`),s.setAttribute("rel","stylesheet"),s.setAttribute("type","text/css"),s.onload=()=>{t&&t.remove(),this.#r(e)},e.body.appendChild(s)}},l=class h extends Event{static eventName="themechange";constructor(){super(h.eventName,{bubbles:!0,composed:!0})}};export{l as ThemeChangeEvent,d as ThemeSupport};
//# sourceMappingURL=theme_support.js.map
