import { takeLatest as e, select as t } from "redux-saga/effects"
import { getCoreBackend as a } from "../../backend"
import { apiCall as r } from "../../api/core"
import { taskErrorSaga as i } from "../error"
import { getIdentityProfile as o, setIdentityProfile as n } from "./identity.reducer"
import { hasVisibleChars as m } from "../lib/string"
const d = (e) => {
const { base64: t, metadata: a } = e || {}
return t && a ? { pointer: { inlined: t }, metadata: a } : null
},
l = (e) => (e || "").trim()
export function* identityUserProfileUpdate({ payload: e }) {
try {
const i = yield t(o),
n = l(e.name) || i.displayName
if (!m(e.name)) throw new Error("Display name must contain at least one visible character")
const s = d(e.avatar?.small),
p = d(e.avatar?.medium),
f = s || p ? { small: s, medium: p } : i.avatar,
c = a()
yield r(c.updateIdentityProfile, { displayName: n, avatar: f })
} catch (e) {
yield i("userProfileIdentityUpdate", e)
}
}
export function* identityUserProfileSaga() {
yield e(n, identityUserProfileUpdate)
}