use super::*;

/// Failure yielded by [`Origin`].
#[derive(Debug)]
pub enum ResolutionError<L, P> {
    /// Usually comes from [`Resolver::resolve`].
    Lookup(L),
    /// Usually comes from [`Factory::deserialize`].
    Parse(P),
}

impl<L, P> ResolutionError<L, P> {
    /// Maps only the [`ResolutionError::Parse`] variant of the overall error.
    pub fn map_parse<Px>(self, f: impl FnOnce(P) -> Px) -> ResolutionError<L, Px> {
        match self {
            Self::Lookup(l) => ResolutionError::Lookup(l),
            Self::Parse(p) => ResolutionError::Parse(f(p)),
        }
    }
}

pub type LookupError<'a, Ctx> = <Ctx as Context<'a>>::LookupError;

/// See [`ResolutionResult`].
pub type ResolutionFailure<'a, Ctx, A> =
    ResolutionError<LookupError<'a, Ctx>, ParseErrorA<'a, Ctx, A>>;

/// Result yielded by [`Origin`].
pub type ResolutionResult<'a, Ctx, A> = Result<Rc<A>, ResolutionFailure<'a, Ctx, A>>;

/// Wrapped result returned by [`Origin`].
pub type Resolution<'a, Ctx, A> = Wrapped<'a, Ctx, ResolutionResult<'a, Ctx, A>>;

pub type HashResolutionResult<'a, Ctx> =
    Result<(Vec<u8>, Rc<dyn Resolver<'a, Ctx>>), LookupError<'a, Ctx>>;

/// Shorthand for the type of values returned by [`Resolver::resolve`].
pub type HashResolution<'a, Ctx> = Wrapped<'a, Ctx, HashResolutionResult<'a, Ctx>>;

/// Value accepted by [`Resolver::resolve`]. Includes index to make it order-sensitive.
#[derive(Clone, Copy, Debug)]
pub struct Address {
    pub point: Hash,
    /// Index of the point in the [`Mentionable::points_typed()`].
    pub index: usize,
}

/// Trait representing the "rainbow table" behaviour.
pub trait Resolver<'a, Ctx: Context<'a>>: 'a {
    /// Successfully returned value should be the inverse of the point passed
    /// with topology header ([`Mentionable::topology()`]) omitted.
    fn resolve(self: Rc<Self>, address: Address) -> HashResolution<'a, Ctx>;
}

pub trait ResolverMap<'a, Ctx: Context<'a>>: Resolver<'a, Ctx> {
    fn resolve_map<T>(
        self: Rc<Self>,
        address: Address,
        f: impl 'a + FnOnce(HashResolutionResult<'a, Ctx>) -> T,
    ) -> Wrapped<'a, Ctx, T> {
        Ctx::fmap(self.resolve(address), f)
    }
}

impl<'a, Ctx: Context<'a>, R: ?Sized + Resolver<'a, Ctx>> ResolverMap<'a, Ctx> for R {}