import { put as e, select as r, takeLatest as t, takeLeading as a } from "redux-saga/effects"
import { getCoreBackend as i } from "../../../backend"
import { apiCall as o } from "../../../api/core"
import { taskErrorSaga as c } from "../../error"
import {
setBackupCreateErrorMsg as d,
askBackupCreateSecretWords as n,
getBackupCreateSecretWords as y,
setBackupCreateCheckWords as m,
setBackupCreateSecretPhrase as s,
submitBackupCreate as l,
resetBackupCreateCheckWords as p,
createAndSubmitBackup as u,
} from "../identity.reducer"
import { createCheckWords as f } from "../identity.model"
import { SECRET_PHRASE_WORDS_COUNT as g } from "../identity.constants"
export function* createBackupWordsSaga() {
try {
if ((yield r(y)).length > 0) return
const t = i(),
a = yield o(t.getRecoveryPhrase)
if (!a) return
yield e(s(a))
} catch (e) {
yield c("createBackupWordsSaga", e)
}
}
export function* submitBackupCreateSaga(t) {
try {
if ((yield r(y)).length !== g)
throw new Error(
"It looks like you have the wrong words. Remember, the order and spelling of the words matter.",
)
const a = i()
if (t?.payload?.deviceName)
try {
yield o(a.updateIdentityDevice, { name: t.payload.deviceName })
} catch (e) {
yield c("submitBackupCreateSaga: deviceNameChange", e)
}
;(yield o(a.confirmRecoveryPhrase, { persist: !1 }), yield e(u(!0)))
} catch (r) {
;(yield c("submitBackupCreateSaga", r, { logOnly: !0 }), yield e(d(r.message)))
}
}
export function* resetBackupCreateCheckWordsSaga() {
const t = yield r(y)
yield e(m(f(t)))
}
export function* backupCreateSaga() {
;(yield a(n, createBackupWordsSaga),
yield a(l, submitBackupCreateSaga),
yield t(p, resetBackupCreateCheckWordsSaga))
}