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