import{t as e}from"./BdOa7B91.js";import{$n as t,Cn as n,Lt as r,M as i,Rt as a,f as o,o as s,t as c,vt as l}from"./DLvf9lie.js";function u(){let u=i(),d=r(),f=s(),{t:p}=a(),m=t([]),h=t(!1),g=n(()=>m.value.filter(e=>e.type!==`merge`&&e.type!==`script`)),_=n(()=>m.value.filter(e=>e.type===`merge`)),v=n(()=>m.value.filter(e=>e.type===`script`)),y=t(void 0),b=async()=>{h.value=!0;try{m.value=await u.listProfiles();let e=m.value.find(e=>e.active&&e.type!==`merge`&&e.type!==`script`);e?y.value=e.id:m.value.some(e=>e.active===!1)&&(y.value=void 0)}finally{h.value=!1}},x=async e=>{await u.createProfile(e),await b()},S=async e=>{await x({name:e,type:`merge`})},C=async e=>{await x({name:e,type:`script`,content:l})},w=async(e,t)=>{await u.duplicateProfile(e,t),await b()},T=async e=>{await u.deleteProfile(e),await b()},E=async(e,t)=>{await u.importProfile(e,t),await b()},D=async t=>{try{return await u.refreshProfile(t),await b(),c.success(p(`profilesRefreshed`)),!0}catch(t){return c.error(p(`profilesRefreshFailed`),{description:e(t)}),!1}},O=async t=>{try{return await u.refreshAndActivateProfile(t),await d.invalidateQueries({queryKey:o.config}),await b(),c.success(p(`profilesRefreshed`)),!0}catch(t){return c.error(p(`profilesRefreshFailed`),{description:e(t)}),!1}},k=async(e,t)=>{await u.updateProfile(e,{content:t}),await b()},A=async(e,t)=>{await u.updateProfile(e,{updateInterval:t}),await b()},j=e=>u.getProfile(e),M=e=>u.validateProfile(e),N=async e=>{let t=await u.activateProfile(e);return y.value=e,await d.invalidateQueries({queryKey:o.config}),await b(),await f.closeAllConnections(),t},P=async()=>{if(!y.value){c.info(p(`profilesMergeNoActiveBase`));return}await u.activateProfile(y.value),await d.invalidateQueries({queryKey:o.config})};return{profiles:m,baseProfiles:g,mergeProfiles:_,scriptProfiles:v,activeBaseId:y,loading:h,refresh:b,create:x,createMerge:S,createScript:C,duplicate:w,remove:T,importUrl:E,refreshRemote:D,refreshAndApply:O,save:k,saveMerge:async(t,n)=>{try{await u.updateProfile(t,{content:n}),await b(),await P()}catch(t){c.error(p(`profilesMergeUpdateFailed`),{description:e(t)})}},saveScript:async(t,n)=>{try{await u.updateProfile(t,{content:n}),await b(),await P()}catch(t){c.error(p(`profilesScriptUpdateFailed`),{description:e(t)})}},setEnabled:async(t,n)=>{try{await u.updateProfile(t,{enabled:n}),await b(),await P()}catch(t){c.error(p(`profilesMergeUpdateFailed`),{description:e(t)})}},setScriptEnabled:async(t,n)=>{try{await u.updateProfile(t,{enabled:n}),await b(),await P()}catch(t){c.error(p(`profilesScriptUpdateFailed`),{description:e(t)})}},setUpdateInterval:A,load:j,validate:M,activate:N}}export{u as t};