!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["state-radio"]={})}(this,(function(e){"use strict";const t=function(e={}){let t=[],s=e.plugins||[],n={};s.length>0&&s.forEach((e=>{const t=e.name||"UnNamedPlugin";n[t]=e}));const r=e=>t[e].state,o=(e,s)=>{const n=t[e];let r=n.state;for(const t of n.activePlugins)if(t.getter){const e=t.getter.method(r,t.getter.options);r={...r,...e}}return((e,s)=>{((null==s?void 0:s.auto)??1)&&i(e,(()=>t[e].state));return t[e].state})(e,s)},i=(e,s)=>{t[e].subscribers.add(s)},a=e=>{t[e].subscribers.forEach((s=>s(t[e].state)))},c=(e,t)=>{let s=r(e),n=t;return"function"==typeof t&&(n=t(s)),u(e,n)},u=(e,s)=>{let n=t[e].state;return t[e].state=s,t[e].history.length<=10||(t[e].history=[]),t[e].history.push(n),a(e),t[e].state},l=async(e,s)=>{const n=t[e],o=d(n.middleWares,e);let i=r(e),a=s;"function"==typeof s&&(a=s(i));try{const t=await o(a);return u(e,t)}catch(c){}},d=(e,t)=>e.reduce(((e,t,s)=>async(s,...n)=>{try{const r=await e(s,...n);return await t(r,...n)}catch(r){throw r}}));return{channels:{getChannels:()=>t,getChannel:e=>t[e]?t[e]:null,addChannel:(e,s={})=>(t[e]={name:e,activePlugins:[],subscribers:/* @__PURE__ */new Set,state:s,middleWares:[],history:[],setState:s=>((e,s)=>{const n=t[e];let o=r(e),i=s;"function"==typeof s&&(i=s(o));for(const t of n.activePlugins)t.setter&&(i=t.setter.method(i,t.setter.options));return c(e,i)})(e,s),setStateAsync:t=>l(e,t),getState:t=>o(e,t),getHistory:()=>(e=>t[e].history)(e),addMiddleWares:(...s)=>((e,...s)=>{let n=t[e].middleWares;return t[e].middleWares=[...n,...s],t[e].middleWares})(e,...s),subscribe:t=>i(e,t),unSubscribe:s=>((e,s)=>{t[e].subscribers.delete(s)})(e,s),notifySubscribers:e=>a(e),usePlugin:s=>((e,s)=>{const r=n[s]||null;r&&(t[e].activePlugins.push(r),(r.exposes||[]).forEach((s=>{t[e][s.name]=(...n)=>s.method(t[e].state,...n)})))})(e,s)},t[e]),removeChannel:e=>{if(t[e]){const s=Object.keys(t).filter((t=>t!==e)).reduce(((e,s)=>(e[s]=t[s],e)),{});t=s}return t}}}};e.StateRadio=t,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}));