import{i as e}from"./useAnalytics-D7ict_xi.js";import{at as t}from"./src-CD6Jsigc.js";import{r as n,t as r}from"./axios-DSyjHw-i.js";import{t as i}from"./backendApi-BRM6WgjB.js";var a={thin:`100`,extralight:`200`,light:`300`,regular:`400`,normal:`400`,medium:`500`,semibold:`600`,bold:`700`,extrabold:`800`,black:`900`};function o(t){let{fetchJson:n,menuUrl:r,fallbackVariants:i=[`400`,`700`]}=t,o=new Map,s=[1e3,4e3,15e3],c=e=>new Promise(t=>setTimeout(t,e));function l(e){return{family:e,variants:[...i],files:{},category:`sans-serif`,kind:`webfonts#webfont`,menu:``}}async function u(e){let t=new FontFace(e.family,`url(${r(e)})`);await t.load(),document.fonts.add(t)}async function d(e,t){let n=[];for(let r of t.variants){let i=t.files[r];if(!i){console.warn(`URL not found for variant: ${r} of font ${e}`);continue}let o=`normal`,s=r;r.endsWith(`italic`)&&(o=`italic`,s=r.replace(`italic`,``),s===``&&(s=`regular`));let c=a[s]||s;if(!/^\d+$|^(normal|bold|lighter|bolder)$/.test(c)){console.warn(`Invalid font weight derived: '${c}' from variant '${r}' for font ${e}. Skipping.`);continue}let l=new FontFace(e,`url(${i})`,{weight:c,style:o});n.push(l.load().then(e=>{document.fonts.add(e)}).catch(t=>{console.error(`Failed to load variant ${r} for font ${e}:`,t)}))}await Promise.all(n)}async function f(e){let t=await n(`/fonts/${e.replace(` `,`+`)}`);if(!t||!t.family||!t.variants||!t.files)throw Error(`Incomplete font data received for "${e}"`);return r?await u(t):await d(e,t),t}return e(`font`,{state(){return{CachedFonts:{Arial:l(`Arial`)},GoogleFonts:[],LocalFonts:[],WebSafeFonts:[`Arial`,`Arial Black`,`Comic Sans MS`,`Courier New`,`Georgia`,`Impact`,`Lucida Console`,`Lucida Sans Unicode`,`Palatino Linotype`,`Tahoma`,`Times New Roman`,`Trebuchet MS`,`Verdana`],LoadingFonts:[]}},getters:{getFont:e=>t=>e.CachedFonts[t],getGoogleFonts:e=>e.GoogleFonts,getLocalFonts:e=>e.LocalFonts,getWebSafeFonts:e=>e.WebSafeFonts,getAllFonts:e=>[...e.GoogleFonts,...e.LocalFonts,...e.WebSafeFonts],hasFont:e=>t=>e.CachedFonts[t]!==void 0||e.WebSafeFonts.includes(t),isLoading:e=>t=>e.LoadingFonts.includes(t)},actions:{async fetchAllFontsAsync(){n(`/fonts`).then(e=>{let t=e;this.GoogleFonts=t.google,this.LocalFonts=t.local,this.WebSafeFonts=t.browser})},async fetchFontAsync(e){if(!e||e.trim().startsWith(`var(`))return this.CachedFonts.Arial;if(this.CachedFonts[e])return this.CachedFonts[e];let t=o.get(e);if(t)return t;let n=f(e);o.set(e,n),this.LoadingFonts.push(e);try{let t=await n;return this.CachedFonts[e]=t,t}catch(t){throw this.CachedFonts[e]=l(e),(async()=>{for(let t of s){await c(t);try{this.CachedFonts[e]=await f(e);return}catch{}}console.warn(`Font "${e}" unavailable after ${s.length+1} attempts, falling back.`)})(),t}finally{o.delete(e),this.LoadingFonts=this.LoadingFonts.filter(t=>t!==e)}}}})}var s=o({fetchJson:e=>i.get(e),menuUrl:e=>i.getCacheUrl(e.menu),fallbackVariants:[`100`,`200`,`300`,`400`,`500`,`600`,`700`,`800`,`900`]});function c(){let{t:e}=n({useScope:`global`});function a(e){if(!r.isAxiosError(e))return;let t=e.response?.data;return typeof t==`string`&&t.trim().length>0?t:void 0}async function o(n,s={}){let c=new FormData;c.append(`file`,n);try{let n=s.overwrite?`?overwrite=true`:``,r=await i.post(`style/set/import${n}`,c);return r.warnings.length>0?t.warning(e(`styleeditor.import.partial.title`),{description:e(`styleeditor.import.partial.description`,{name:r.styleSetName,variants:r.variantsImported,skipped:r.warnings.length}),duration:1e4}):t.success(e(`styleeditor.import.success.title`),{description:e(`styleeditor.import.success.description`,{name:r.styleSetName,variants:r.variantsImported,assets:r.assetsImported}),duration:5e3}),r.warnings.forEach(e=>console.warn(`Style import warning:`,e)),s.onSuccess?.(),!0}catch(i){if(console.error(`Error importing style set:`,i),r.isAxiosError(i)&&i.response?.status===409&&!s.overwrite)return t.error(e(`styleeditor.import.conflict.title`),{description:a(i)??e(`styleeditor.import.conflict.description`),duration:15e3,action:{label:e(`styleeditor.import.conflict.action`),onClick:()=>{o(n,{...s,overwrite:!0})}}}),!1;let c=i instanceof Error&&i.message?i.message:e(`styleeditor.import.error.description`);return t.error(e(`styleeditor.import.error.title`),{description:a(i)??c,duration:8e3}),!1}}return{importStyleFile:o}}export{s as n,c as t};