import { setApiEnhancer as t, setApiPromiseEnhancer as o } from "../../api/core"
import { setApiSubscriptionLifecycleMethods as e } from "../lib/external-src"
import {
clearSyslogDataCmd as r,
loadSyslogDataCmd as s,
setSyslogState as n,
syslogAddEntryCmd as a,
} from "./syslog.actions"
import { MAX_LOG_ENTRIES as i } from "./syslog.constants"
import {
apiCallPromiseWithSyslog as c,
apiCallWithSyslog as m,
apiSubscriptionLifecycleMethods as l,
} from "./syslog.saga"
let p = new Array(i),
y = 0,
d = 0
const f = ["getStats"],
g = (t) => {
if (f.includes(t.method)) return
const { method: o, logType: e, ...r } = t
;((p[y] = { eventType: e, event: o || "unknown", timestamp: Date.now(), payload: r }),
(y = (y + 1) % i),
d < i && (d += 1))
},
h = () => {
const t = y % i
return [...p.slice(t, d), ...p.slice(0, t)].filter(Boolean)
},
u = () => {
p = new Array(i)
}
export const syslogMiddleware = (i) => (
t(m(g)),
o(c(g)),
e(l(g)),
(t) => (o) => {
if (a.match(o)) return (g(o.payload), o)
const e = t(o)
return (s.match(o) && i.dispatch(n(h())), r.match(o) && (u(), i.dispatch(n([]))), e)
}
)