import{Lazy as e,Color as t}from"../common/common.js";import{NumberUtilities as o}from"../platform/platform.js";import{Runtime as r}from"../root/root.js";let s;class n{constructor(t){const o=window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"default";this._themeName="systemPreferred"===t.get()?o:t.get(),this._themableProperties=new Set(["color","box-shadow","text-shadow","outline-color","background-image","background-color","border-left-color","border-right-color","border-top-color","border-bottom-color","-webkit-border-image","fill","stroke"]),this._cachedThemePatches=new Map,this._setting=t,this._customSheets=new Set,this._computedRoot=e.lazy(()=>window.getComputedStyle(document.documentElement))}static hasInstance(){return void 0!==s}static instance(e={forceNew:null,setting:null}){const{forceNew:t,setting:o}=e;if(!s||t){if(!o)throw new Error("Unable to create theme support: setting must be provided: "+(new Error).stack);s=new n(o)}return s}getComputedValue(e){const t=this._computedRoot();if("symbol"==typeof t)throw new Error(`Computed value for property (${e}) could not be found on :root.`);return t.getPropertyValue(e)}hasTheme(){return"default"!==this._themeName}themeName(){return this._themeName}injectHighlightStyleSheets(e){this._injectingStyleSheet=!0,this._appendStyle(e,"ui/inspectorSyntaxHighlight.css"),"dark"===this._themeName&&this._appendStyle(e,"ui/inspectorSyntaxHighlightDark.css"),this._injectingStyleSheet=!1}_appendStyle(e,t){const o=r.cachedResources.get(t)||"";o||console.error(t+" not preloaded. Check module.json");let s=createElement("style");s.textContent=o,e.appendChild(s);const h=n.instance().themeStyleSheet(t,o);h&&(s=createElement("style"),s.textContent=h+"\n"+r.Runtime.resolveSourceURL(t+".theme"),e.appendChild(s))}injectCustomStyleSheets(e){for(const t of this._customSheets){const o=document.createElement("style");o.textContent=t,e.appendChild(o)}}isForcedColorsMode(){return window.matchMedia("(forced-colors: active)").matches}addCustomStylesheet(e){this._customSheets.add(e)}applyTheme(e){if(!this.hasTheme()||this.isForcedColorsMode())return;"dark"===this._themeName&&e.documentElement.classList.add("-theme-with-dark-background");const t=e.styleSheets,o=[];for(let e=0;e<t.length;++e){const r=t[e].href;r&&o.push(this._patchForTheme(r,t[e]))}o.push("/*# sourceURL=inspector.css.theme */");const r=e.createElement("style");r.textContent=o.join("\n"),e.head.appendChild(r)}themeStyleSheet(e,t){if(!this.hasTheme()||this._injectingStyleSheet||this.isForcedColorsMode())return"";let o=this._cachedThemePatches.get(e);if(!o){const r=document.createElement("style");r.textContent=t,document.body.appendChild(r);const{sheet:s}=r;if(!s)throw new Error("No sheet in stylesheet object");o=this._patchForTheme(e,s),document.body.removeChild(r)}return o}_patchForTheme(e,t){const o=this._cachedThemePatches.get(e);if(o)return o;try{const o=t.cssRules,r=[];for(let e=0;e<o.length;++e){if(o[e]instanceof CSSImportRule){r.push(this._patchForTheme(o[e].styleSheet.href,o[e].styleSheet));continue}const t=[],s=o[e].style,n=o[e].selectorText;for(let e=0;s&&e<s.length;++e)this._patchProperty(n,s,s[e],t);t.length&&r.push(o[e].selectorText+"{"+t.join("")+"}")}const s=r.join("\n");return this._cachedThemePatches.set(e,s),s}catch(e){return this._setting.set("default"),""}}_patchProperty(e,o,r,s){if(!this._themableProperties.has(r))return;const h=o.getPropertyValue(r);if(!h||"none"===h||"inherit"===h||"initial"===h||"transparent"===h)return;if("background-image"===r&&-1===h.indexOf("gradient"))return;if(-1!==e.indexOf("-theme-"))return;let c=n.ColorUsage.Unknown;if(0!==r.indexOf("background")&&0!==r.indexOf("border")||(c|=n.ColorUsage.Background),-1===r.indexOf("background")&&(c|=n.ColorUsage.Foreground),s.push(r),s.push(":"),/^var\(.*\)$/.test(h))s.push(h);else{const e=h.replace(t.Regex,"\0$1\0").split("\0");for(const t of e)s.push(this.patchColorText(t,c))}o.getPropertyPriority(r)&&s.push(" !important"),s.push(";")}patchColorText(e,o){const r=t.Color.parse(e);if(!r)return e;const s=this.patchColor(r,o);let n=s.asString(null);return n||(n=s.asString(s.hasAlpha()?t.Format.RGBA:t.Format.RGB)),n||e}patchColor(e,o){const r=e.hsla();this._patchHSLA(r,o);const s=[];return t.Color.hsl2rgb(r,s),new t.Color(s,e.format())}_patchHSLA(e,t){const r=e[0],s=e[1];let h=e[2];const c=e[3];switch(this._themeName){case"dark":{const e=t&n.ColorUsage.Background?.14:.58,o=t&n.ColorUsage.Foreground?.9:1;h=1-h,h<2*e?h=e+h/2:h>2*o-1&&(h=o-.5+h/2);break}}e[0]=o.clamp(r,0,1),e[1]=o.clamp(s,0,1),e[2]=o.clamp(h,0,1),e[3]=o.clamp(c,0,1)}}n.ColorUsage={Unknown:0,Foreground:1,Background:2};export{n as ThemeSupport};
