// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

//! Resolution reader extension trait.
//!
//! This module provides [`ResolutionReadExt`], for querying resolution mappings
//! from `QueryClient`.

use {
  crate::{
    core::{Ident, SymbolPath, Value, Visibility},
    partitions::{
      records::ResolutionEntry, resolution::SymbolResolution, symbols::Symbols,
    },
  },
  laburnum::database::{
    HasPartition, RecordRef,
    query::QueryClient,
    storage::Partitions,
  },
};

/// Extension trait for reading resolution mappings from partitions.
///
/// Provides typed methods for querying reference-to-definition mappings written
/// during the resolution stage.
pub trait ResolutionReadExt<P: Partitions> {
  /// Look up a resolution by exact sort key.
  fn resolution_at_path<V, I, Path, S>(
    &self,
    sort_key: &Path,
  ) -> Option<ResolutionEntry<V, I, Path, S>>
  where
    V: Value<I>,
    I: Ident,
    Path: SymbolPath,
    S: Visibility,
    P::Stores: HasPartition<SymbolResolution<V, I, Path, S>>;

  /// Query resolutions with a sort key prefix.
  fn resolutions_with_prefix<V, I, Path, S>(
    &self,
    prefix: &Path,
  ) -> Vec<(Path, ResolutionEntry<V, I, Path, S>)>
  where
    V: Value<I>,
    I: Ident,
    Path: SymbolPath,
    S: Visibility,
    P::Stores: HasPartition<SymbolResolution<V, I, Path, S>>;

  /// Follow a resolution entry's target handle to retrieve the full symbol
  /// shape.
  fn resolve_resolution_target<V, I, Path, S>(
    &self,
    entry: &ResolutionEntry<V, I, Path, S>,
  ) -> Option<RecordRef<'_, Symbols<V, I, Path, S>>>
  where
    V: Value<I> + 'static,
    I: Ident,
    Path: SymbolPath + 'static,
    S: Visibility,
    P::Stores: HasPartition<Symbols<V, I, Path, S>>;
}

impl<P: Partitions> ResolutionReadExt<P> for QueryClient<P> {
  fn resolution_at_path<V, I, Path, S>(
    &self,
    sort_key: &Path,
  ) -> Option<ResolutionEntry<V, I, Path, S>>
  where
    V: Value<I>,
    I: Ident,
    Path: SymbolPath,
    S: Visibility,
    P::Stores: HasPartition<SymbolResolution<V, I, Path, S>>,
  {
    self.index_get::<SymbolResolution<V, I, Path, S>>(sort_key)
  }

  fn resolutions_with_prefix<V, I, Path, S>(
    &self,
    prefix: &Path,
  ) -> Vec<(Path, ResolutionEntry<V, I, Path, S>)>
  where
    V: Value<I>,
    I: Ident,
    Path: SymbolPath,
    S: Visibility,
    P::Stores: HasPartition<SymbolResolution<V, I, Path, S>>,
  {
    self.index_range::<SymbolResolution<V, I, Path, S>>(prefix)
  }

  fn resolve_resolution_target<V, I, Path, S>(
    &self,
    entry: &ResolutionEntry<V, I, Path, S>,
  ) -> Option<RecordRef<'_, Symbols<V, I, Path, S>>>
  where
    V: Value<I> + 'static,
    I: Ident,
    Path: SymbolPath + 'static,
    S: Visibility,
    P::Stores: HasPartition<Symbols<V, I, Path, S>>,
  {
    let handle = entry.target.handle()?;
    self.get::<Symbols<V, I, Path, S>>(handle.content_hash())
  }
}
