var v=Object.create;var m=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var F=Object.getPrototypeOf,O=Object.prototype.hasOwnProperty;var A=(e,t)=>{for(var r in t)m(e,r,{get:t[r],enumerable:!0})},S=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of b(t))!O.call(e,o)&&o!==r&&m(e,o,{get:()=>t[o],enumerable:!(n=x(t,o))||n.enumerable});return e};var g=(e,t,r)=>(r=e!=null?v(F(e)):{},S(t||!e||!e.__esModule?m(r,"default",{value:e,enumerable:!0}):r,e)),C=e=>S(m({},"__esModule",{value:!0}),e);var $={};A($,{loadChromiumExtensions:()=>I,styles:()=>U});module.exports=C($);var f=require("electron/main"),E=require("url"),D=require("./error"),c=require("path"),a=require("fs/promises"),p=require("fs");const d=import("electron/main").then(e=>e.safeStorage);async function k(e,t){const r=import("fs/promises").then(n=>n.readFile);return e.protocol==="file:"?{read:(await r)(e.pathname,{signal:t})}:{download:fetch(e.href,t?{signal:t}:{})}}async function P(e,t,r=5){const n=/^@import .+?$/gm;if(!n.test(e))return e;const o=[];e.match(n)?.forEach(s=>{const w=/^@import (?:(?:url\()?["']?([^"';)]*)["']?)\)?;?/m.exec(s);if(w?.[0]===void 0||w[1]===void 0)return;const h=(0,E.resolve)(t.at(-1)??"",w[1]);if(t.includes(h)){o.push(Promise.reject(new Error("Circular reference in CSS imports are disallowed: "+h)));return}o.push(k(new URL(h)).then(u=>u.download?u.download.then(y=>y.text()):u.read.then(y=>y.toString())).then(u=>e=e.replace(s,u)).then(()=>t.push(h)))});const i=(await Promise.allSettled(o)).findIndex(({status:s})=>s==="rejected");if(i>=0){if(r>0)return console.warn("Couldn't resolve CSS theme imports, retrying..."),P(e,t,r-1);await o[i]}return n.test(e)?P(e,t,r):e}async function B(e){return(await d).isEncryptionAvailable()?(await d).encryptString(e.toString()):e.toString()}async function R(e,t){if(!(await d).isEncryptionAvailable()&&!e.isReady()&&process.platform!=="darwin"&&await e.whenReady(),!(await d).isEncryptionAvailable())return(await t).toString();if(!(await t).toString("utf-8").includes("\uFFFD"))throw new Error("One of loaded styles was not encrypted and could not be loaded.");return(await d).decryptString(await t)}async function j(e){const t={title:"Select a Discord theme to add to WebCord",properties:["multiSelections","openFile"],filters:[{name:"CSS stylesheet theme",extensions:["theme.css"]}]},r=e?await f.dialog.showOpenDialog(e,t):await f.dialog.showOpenDialog(t);if(r.canceled)return;const n=[];for(const o of r.filePaths){const l=(0,a.readFile)(o).then(s=>B(s)),i=(0,c.resolve)(f.app.getPath("userData"),"Themes",(0,c.basename)(o,".css"));if((0,c.resolve)(o)===i)return;n.push(l.then(s=>(0,a.writeFile)(i,s)))}await Promise.all(n)}async function W(e){const t=(0,c.resolve)(f.app.getPath("userData"),"Themes");await(0,a.mkdir)(t,{recursive:!0});async function r(){const n=[];for(const l of await(0,a.readdir)(t)){const i=(0,c.resolve)(t,l);!l.endsWith(".theme.css")&&(0,p.statSync)(i).isFile()&&n.push((0,a.readFile)(i).then(s=>[i,s]))}const o=[];for(const l of n)o.push(R(f.app,l.then(i=>i[1])).then(async i=>P(i,[(await l)[0]])).then(i=>i.replaceAll(/((?:--|color|background)[^:;{]*:(?![^:]*?!important)[^:;]*)(;|})/g,"$1 !important$2")).then(i=>e.insertCSS(i)));return Promise.all(o)}(0,p.watch)(t).once("change",()=>{e.reload()}),r().catch(D.commonCatches.print)}async function I(e){const t=(0,c.resolve)(f.app.getPath("userData"),"Extensions","Chrome"),r=[];await(0,a.mkdir)(t,{recursive:!0});for(const n of await(0,a.readdir)(t,{withFileTypes:!0}))n.isDirectory()&&e.isPersistent()&&r.push(e.extensions.loadExtension((0,c.resolve)(t,n.name)));return Promise.all(r)}const U=Object.freeze({load:W,add:j});0&&(module.exports={loadChromiumExtensions,styles});
