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 n;class s{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!==n}static instance(e={forceNew:null,setting:null}){const{forceNew:t,setting:o}=e;if(!n||t){if(!o)throw new Error("Unable to create theme support: setting must be provided: "+(new Error).stack);n=new s(o)}return n}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",{enableLegacyPatching:!0}),"dark"===this._themeName&&this._appendStyle(e,"ui/inspectorSyntaxHighlightDark.css",{enableLegacyPatching:!0}),this._injectingStyleSheet=!1}_appendStyle(e,t,o={enableLegacyPatching:!1}){const n=r.cachedResources.get(t)||"";n||console.error(t+" not preloaded. Check module.json");let h=document.createElement("style");if(h.textContent=n,e.appendChild(h),o.enableLegacyPatching){const o=s.instance().themeStyleSheet(t,n);o&&(h=document.createElement("style"),h.textContent=o+"\n"+r.Runtime.resolveSourceURL(t+".theme"),e.appendChild(h))}}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:n}=r;if(!n)throw new Error("No sheet in stylesheet object");o=this._patchForTheme(e,n),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){const t=o[e];if(t instanceof CSSImportRule){r.push(this._patchForTheme(t.styleSheet.href||"",t.styleSheet));continue}if(!(t instanceof CSSStyleRule))continue;const n=[],s=t.style,h=t.selectorText;for(let e=0;s&&e<s.length;++e)this._patchProperty(h,s,s[e],n);n.length&&r.push(t.selectorText+"{"+n.join("")+"}")}const n=r.join("\n");return this._cachedThemePatches.set(e,n),n}catch(e){return this._setting.set("default"),""}}_patchProperty(e,o,r,n){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=s.ColorUsage.Unknown;if(0!==r.indexOf("background")&&0!==r.indexOf("border")||(c|=s.ColorUsage.Background),-1===r.indexOf("background")&&(c|=s.ColorUsage.Foreground),n.push(r),n.push(":"),/^var\(.*\)$/.test(h))n.push(h);else{const e=h.replace(t.Regex,"\0$1\0").split("\0");for(const t of e)n.push(this.patchColorText(t,c))}o.getPropertyPriority(r)&&n.push(" !important"),n.push(";")}patchColorText(e,o){const r=t.Color.parse(e);if(!r)return e;const n=this.patchColor(r,o);let s=n.asString(null);return s||(s=n.asString(n.hasAlpha()?t.Format.RGBA:t.Format.RGB)),s||e}patchColor(e,o){const r=e.hsla();this._patchHSLA(r,o);const n=[];return t.Color.hsl2rgb(r,n),new t.Color(n,e.format())}_patchHSLA(e,t){const r=e[0],n=e[1];let h=e[2];const c=e[3];switch(this._themeName){case"dark":{const e=t&s.ColorUsage.Background?.14:0,o=t&s.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(n,0,1),e[2]=o.clamp(h,0,1),e[3]=o.clamp(c,0,1)}}s.ColorUsage={Unknown:0,Foreground:1,Background:2};export{s as ThemeSupport};
