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

//! Resolution partition.
//!
//! Stage 4 of the symbolique pipeline. Resolution maps references to their
//! target definitions:
//!
//! - **SymbolResolution**: Index-only partition with `ResolutionEntry` entries
//!   pointing to target definitions in the shared `Symbols` partition
//!
//! Resolution algorithms are user-defined - symbolique provides the partition
//! for storing results. No new shapes are created; resolution links existing
//! Reference/Import shapes to existing Definition shapes.

use {
  super::records::ResolutionEntry,
  crate::core::{
    Ident, SymbolPath, SymbolVisibility, Value, Visibility,
  },
  laburnum::{
    Ident as LaburnumIdent,
    database::{Partition, PartitionKey},
  },
  std::marker::PhantomData,
};

/// Symbol resolution partition (index-only).
///
/// Stores only index entries that reference target definitions in the shared
/// `Symbols` partition. Each entry contains the target path and a handle to
/// the target symbol shape.
pub struct SymbolResolution<V, I, P, S = SymbolVisibility>(
  PhantomData<(V, I, P, S)>,
);

impl<V, I, P, S> Default for SymbolResolution<V, I, P, S> {
  fn default() -> Self {
    Self(PhantomData)
  }
}

impl<V, I, P, S> Clone for SymbolResolution<V, I, P, S> {
  fn clone(&self) -> Self {
    *self
  }
}

impl<V, I, P, S> Copy for SymbolResolution<V, I, P, S> {}

impl<V, I, P, S> std::fmt::Debug for SymbolResolution<V, I, P, S> {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("SymbolResolution").finish()
  }
}

impl<V, I, P, S> PartitionKey for SymbolResolution<V, I, P, S>
where
  V: Value<I>,
  I: Ident,
  P: SymbolPath,
  S: Visibility,
{
  const KEY: LaburnumIdent = LaburnumIdent::new("symbolique::resolution");
}

impl<V, I, P, S> Partition for SymbolResolution<V, I, P, S>
where
  V: Value<I>,
  I: Ident,
  P: SymbolPath,
  S: Visibility,
{
  type Record = (); // Index-only
  type IndexEntry = ResolutionEntry<V, I, P, S>;
  type SortKey = P;
}

#[cfg(test)]
mod tests {
  use super::*;

  #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
  struct TestPath(String);

  impl laburnum::PartitionSortKey for TestPath {
    fn is_prefix_of(&self, other: &Self) -> bool {
      other.0.starts_with(&self.0)
    }

    fn resolve(&self, _resolver: &dyn laburnum::SourceResolver) -> String {
      self.0.clone()
    }
  }

  #[test]
  fn symbol_resolution_partition_key() {
    use crate::core::{DefaultValue, StringIdent};

    let _key = SymbolResolution::<DefaultValue, StringIdent, TestPath>::KEY;
  }

  #[test]
  fn symbol_resolution_is_index_only() {
    use crate::core::{DefaultValue, StringIdent};

    fn assert_record_is_unit<P: Partition<Record = ()>>() {}

    assert_record_is_unit::<
      SymbolResolution<DefaultValue, StringIdent, TestPath>,
    >();
  }
}
