use super::{Error, Result, Settled, purge};
use crate::provider;
use crate::service::Warning;
use crate::state::{self, Key};
pub fn forget(settled: Settled, key: &Key) -> Result<(String, Vec<Warning>)> {
let Settled {
_exclusive,
mut state,
ctx,
} = settled;
let live_uuid = provider::of(key.provider)
.recorded_identity(&ctx)
.map(|found| found.account_id);
let signed_in = match (&live_uuid, state.get(key)) {
(Some(uuid), Some(account)) => &account.account_uuid == uuid,
_ => state.active_for(key.provider) == Some(key.label.as_str()),
};
if signed_in {
return Err(Error::CannotForgetActiveAccount { label: key.typed() });
}
let enrolled = state.labels(key.provider);
let account = state.remove(key).ok_or_else(|| Error::AccountUnknown {
label: key.typed(),
enrolled,
})?;
state::save(&ctx, &state)?;
crate::fault::point("forget.recorded");
crate::readings::forget(&ctx, &account.account_uuid);
crate::budget::forget(&ctx, &account.account_uuid);
crate::history::forget(&ctx, &account.account_uuid);
let pending = purge(&ctx, &mut state);
Ok((
account.email,
(pending > 0)
.then_some(Warning::ParksPendingRemoval(pending))
.into_iter()
.collect(),
))
}