import { call as o, put as e, take as t } from "redux-saga/effects"
import r from "hypercore-id-encoding"
import { getPayApi as i } from "../../backend"
import { taskErrorSaga as n } from "../error"
import { userCustomNotification as a, NOTIFICATION_CODE as c } from "../notification"
import { channelFromEventEmmitter as d } from "../lib/external-src"
import {
setPayRequest as m,
setPaySuccess as p,
setPayError as y,
setInvoice as s,
} from "./pay.reducer"
export function* paySubmitSaga(t) {
try {
yield e(m())
const {
payload: { swarmId: n, amount: a, description: c },
} = t,
d = r.decode(n),
y = i()
if (!y.connections.get(d)) throw new Error("User not connected to the room")
const l = yield o([y, y.pay], d, a, c)
l.status.paid ? yield e(p(l)) : yield e(s(l.invoice))
} catch (o) {
;(yield e(y(o.message)), yield n("paySubmitSaga", o, { logOnly: !0 }))
}
}
export function* payReceiveSaga() {
let o
try {
const r = i()
for (o = d(r, "payment-received"); ; ) {
const {
tx: { amt: r, description: i },
} = yield t(o)
yield e(a({ code: c.PAYMENT_RECEIVED, amount: r, description: i }))
}
} finally {
o && o.close()
}
}