use wasm_bindgen::prelude::*;
use crate::traversal;
use crate::wasm::predicate::Predicate;
use crate::wasm::traversal::Traversal;
use crate::wasm::types::{js_array_to_strings, js_to_u64, js_to_value};
#[wasm_bindgen(js_name = "__")]
pub struct AnonymousFactory;
#[wasm_bindgen(js_class = "__")]
impl AnonymousFactory {
#[wasm_bindgen(js_name = "identity")]
pub fn identity() -> Traversal {
Traversal::anonymous_with_step(traversal::IdentityStep)
}
#[wasm_bindgen(js_name = "start")]
pub fn start() -> Traversal {
Self::identity()
}
#[wasm_bindgen(js_name = "hasLabel")]
pub fn has_label(label: &str) -> Traversal {
Traversal::anonymous_with_step(traversal::HasLabelStep::single(label))
}
#[wasm_bindgen(js_name = "hasLabelAny")]
pub fn has_label_any(labels: JsValue) -> Result<Traversal, JsError> {
let label_vec = js_array_to_strings(labels)?;
Ok(Traversal::anonymous_with_step(
traversal::HasLabelStep::any(label_vec),
))
}
pub fn has(key: &str) -> Traversal {
Traversal::anonymous_with_step(traversal::HasStep::new(key))
}
#[wasm_bindgen(js_name = "hasValue")]
pub fn has_value(key: &str, value: JsValue) -> Result<Traversal, JsError> {
let v = js_to_value(value)?;
Ok(Traversal::anonymous_with_step(
traversal::HasValueStep::new(key, v),
))
}
#[wasm_bindgen(js_name = "hasWhere")]
pub fn has_where(key: &str, predicate: Predicate) -> Traversal {
Traversal::anonymous_with_step(traversal::HasWhereStep::new(key, predicate.into_inner()))
}
#[wasm_bindgen(js_name = "hasNot")]
pub fn has_not(key: &str) -> Traversal {
Traversal::anonymous_with_step(traversal::HasNotStep::new(key))
}
#[wasm_bindgen(js_name = "is")]
pub fn is_(predicate: Predicate) -> Traversal {
Traversal::anonymous_with_step(traversal::IsStep::new(predicate.into_inner()))
}
#[wasm_bindgen(js_name = "isEq")]
pub fn is_eq(value: JsValue) -> Result<Traversal, JsError> {
let v = js_to_value(value)?;
Ok(Traversal::anonymous_with_step(traversal::IsStep::eq(v)))
}
pub fn dedup() -> Traversal {
Traversal::anonymous_with_step(traversal::DedupStep::new())
}
#[wasm_bindgen(js_name = "dedupByKey")]
pub fn dedup_by_key(key: &str) -> Traversal {
Traversal::anonymous_with_step(traversal::DedupByKeyStep::new(key))
}
#[wasm_bindgen(js_name = "dedupByLabel")]
pub fn dedup_by_label() -> Traversal {
Traversal::anonymous_with_step(traversal::DedupByLabelStep::new())
}
#[wasm_bindgen(js_name = "dedupBy")]
pub fn dedup_by(sub: Traversal) -> Traversal {
let core_traversal = sub.into_core_traversal();
Traversal::anonymous_with_step(traversal::DedupByTraversalStep::new(core_traversal))
}
pub fn limit(n: JsValue) -> Result<Traversal, JsError> {
let num = js_to_u64(n)?;
Ok(Traversal::anonymous_with_step(traversal::LimitStep::new(
num as usize,
)))
}
pub fn skip(n: JsValue) -> Result<Traversal, JsError> {
let num = js_to_u64(n)?;
Ok(Traversal::anonymous_with_step(traversal::SkipStep::new(
num as usize,
)))
}
pub fn range(start: JsValue, end: JsValue) -> Result<Traversal, JsError> {
let s = js_to_u64(start)? as usize;
let e = js_to_u64(end)? as usize;
Ok(Traversal::anonymous_with_step(traversal::RangeStep::new(
s, e,
)))
}
pub fn tail() -> Traversal {
Traversal::anonymous_with_step(traversal::TailStep::new(1))
}
#[wasm_bindgen(js_name = "simplePath")]
pub fn simple_path() -> Traversal {
Traversal::anonymous_with_step(traversal::SimplePathStep::new())
}
#[wasm_bindgen(js_name = "cyclicPath")]
pub fn cyclic_path() -> Traversal {
Traversal::anonymous_with_step(traversal::CyclicPathStep::new())
}
pub fn out() -> Traversal {
Traversal::anonymous_with_step(traversal::OutStep::new())
}
#[wasm_bindgen(js_name = "outLabels")]
pub fn out_labels(labels: JsValue) -> Result<Traversal, JsError> {
let label_vec = js_array_to_strings(labels)?;
Ok(Traversal::anonymous_with_step(
traversal::OutStep::with_labels(label_vec),
))
}
#[wasm_bindgen(js_name = "in_")]
pub fn in_() -> Traversal {
Traversal::anonymous_with_step(traversal::InStep::new())
}
#[wasm_bindgen(js_name = "inLabels")]
pub fn in_labels(labels: JsValue) -> Result<Traversal, JsError> {
let label_vec = js_array_to_strings(labels)?;
Ok(Traversal::anonymous_with_step(
traversal::InStep::with_labels(label_vec),
))
}
pub fn both() -> Traversal {
Traversal::anonymous_with_step(traversal::BothStep::new())
}
#[wasm_bindgen(js_name = "bothLabels")]
pub fn both_labels(labels: JsValue) -> Result<Traversal, JsError> {
let label_vec = js_array_to_strings(labels)?;
Ok(Traversal::anonymous_with_step(
traversal::BothStep::with_labels(label_vec),
))
}
#[wasm_bindgen(js_name = "outE")]
pub fn out_e() -> Traversal {
Traversal::anonymous_with_step(traversal::OutEStep::new())
}
#[wasm_bindgen(js_name = "inE")]
pub fn in_e() -> Traversal {
Traversal::anonymous_with_step(traversal::InEStep::new())
}
#[wasm_bindgen(js_name = "bothE")]
pub fn both_e() -> Traversal {
Traversal::anonymous_with_step(traversal::BothEStep::new())
}
#[wasm_bindgen(js_name = "outV")]
pub fn out_v() -> Traversal {
Traversal::anonymous_with_step(traversal::OutVStep::new())
}
#[wasm_bindgen(js_name = "inV")]
pub fn in_v() -> Traversal {
Traversal::anonymous_with_step(traversal::InVStep::new())
}
#[wasm_bindgen(js_name = "bothV")]
pub fn both_v() -> Traversal {
Traversal::anonymous_with_step(traversal::BothVStep::new())
}
#[wasm_bindgen(js_name = "otherV")]
pub fn other_v() -> Traversal {
Traversal::anonymous_with_step(traversal::OtherVStep::new())
}
pub fn values(key: &str) -> Traversal {
Traversal::anonymous_with_step(traversal::ValuesStep::new(key))
}
#[wasm_bindgen(js_name = "valueMap")]
pub fn value_map() -> Traversal {
Traversal::anonymous_with_step(traversal::ValueMapStep::new())
}
#[wasm_bindgen(js_name = "elementMap")]
pub fn element_map() -> Traversal {
Traversal::anonymous_with_step(traversal::ElementMapStep::new())
}
pub fn id() -> Traversal {
Traversal::anonymous_with_step(traversal::IdStep)
}
pub fn label() -> Traversal {
Traversal::anonymous_with_step(traversal::LabelStep)
}
pub fn constant(value: JsValue) -> Result<Traversal, JsError> {
let v = js_to_value(value)?;
Ok(Traversal::anonymous_with_step(
traversal::ConstantStep::new(v),
))
}
pub fn unfold() -> Traversal {
Traversal::anonymous_with_step(traversal::UnfoldStep::new())
}
pub fn path() -> Traversal {
Traversal::anonymous_with_step(traversal::PathStep::new())
}
#[wasm_bindgen(js_name = "selectOne")]
pub fn select_one(label: &str) -> Traversal {
Traversal::anonymous_with_step(traversal::SelectStep::single(label))
}
pub fn select(labels: JsValue) -> Result<Traversal, JsError> {
let label_vec = js_array_to_strings(labels)?;
Ok(Traversal::anonymous_with_step(traversal::SelectStep::new(
label_vec,
)))
}
pub fn mean() -> Traversal {
Traversal::anonymous_with_step(traversal::MeanStep::new())
}
pub fn count() -> Traversal {
Traversal::anonymous_with_step(traversal::aggregate::CountStep::new())
}
pub fn min() -> Traversal {
Traversal::anonymous_with_step(traversal::MinStep::new())
}
pub fn max() -> Traversal {
Traversal::anonymous_with_step(traversal::MaxStep::new())
}
#[wasm_bindgen(js_name = "as")]
pub fn as_(label: &str) -> Traversal {
Traversal::anonymous_with_step(traversal::AsStep::new(label))
}
pub fn fold() -> Traversal {
Traversal::anonymous_with_step(super::traversal::FoldStep::new())
}
pub fn sum() -> Traversal {
Traversal::anonymous_with_step(super::traversal::SumStep::new())
}
#[wasm_bindgen(js_name = "addV")]
pub fn add_v(label: &str) -> Traversal {
Traversal::anonymous_with_step(traversal::AddVStep::new(label))
}
#[wasm_bindgen(js_name = "addE")]
pub fn add_e(label: &str) -> Traversal {
Traversal::anonymous_with_step(super::traversal::AddESpawnStep::new(label))
}
pub fn property(key: &str, value: JsValue) -> Result<Traversal, JsError> {
let v = js_to_value(value)?;
Ok(Traversal::anonymous_with_step(
traversal::PropertyStep::new(key, v),
))
}
pub fn drop() -> Traversal {
Traversal::anonymous_with_step(traversal::DropStep::new())
}
#[wasm_bindgen(js_name = "where_")]
pub fn where_(sub: Traversal) -> Traversal {
let t = Traversal::anonymous_with_step(traversal::IdentityStep);
let core_traversal = sub.into_core_traversal();
t.add_step_internal(traversal::WhereStep::new(core_traversal))
}
pub fn not(sub: Traversal) -> Traversal {
let t = Traversal::anonymous_with_step(traversal::IdentityStep);
let core_traversal = sub.into_core_traversal();
t.add_step_internal(traversal::NotStep::new(core_traversal))
}
pub fn union(traversals: Vec<Traversal>) -> Traversal {
let t = Traversal::anonymous_with_step(traversal::IdentityStep);
let core_traversals: Vec<_> = traversals
.into_iter()
.map(|tr| tr.into_core_traversal())
.collect();
t.add_step_internal(traversal::UnionStep::new(core_traversals))
}
pub fn coalesce(traversals: Vec<Traversal>) -> Traversal {
let t = Traversal::anonymous_with_step(traversal::IdentityStep);
let core_traversals: Vec<_> = traversals
.into_iter()
.map(|tr| tr.into_core_traversal())
.collect();
t.add_step_internal(traversal::CoalesceStep::new(core_traversals))
}
pub fn choose(condition: Traversal, if_true: Traversal, if_false: Traversal) -> Traversal {
let t = Traversal::anonymous_with_step(traversal::IdentityStep);
let cond = condition.into_core_traversal();
let t_branch = if_true.into_core_traversal();
let f_branch = if_false.into_core_traversal();
t.add_step_internal(traversal::ChooseStep::new(cond, t_branch, f_branch))
}
pub fn optional(sub: Traversal) -> Traversal {
let t = Traversal::anonymous_with_step(traversal::IdentityStep);
let core_traversal = sub.into_core_traversal();
t.add_step_internal(traversal::OptionalStep::new(core_traversal))
}
pub fn local(sub: Traversal) -> Traversal {
let t = Traversal::anonymous_with_step(traversal::IdentityStep);
let core_traversal = sub.into_core_traversal();
t.add_step_internal(traversal::LocalStep::new(core_traversal))
}
#[wasm_bindgen(js_name = "shortestPath")]
pub fn shortest_path(target_id: JsValue) -> Result<Traversal, JsError> {
let target = crate::wasm::types::js_to_vertex_id(target_id)?;
Ok(Traversal::anonymous_with_step(
traversal::ShortestPathStep::new(target),
))
}
#[wasm_bindgen(js_name = "shortestPathWeighted")]
pub fn shortest_path_weighted(
target_id: JsValue,
weight_property: &str,
) -> Result<Traversal, JsError> {
let target = crate::wasm::types::js_to_vertex_id(target_id)?;
Ok(Traversal::anonymous_with_step(
traversal::DijkstraStep::new(target, weight_property.to_string()),
))
}
pub fn bfs(max_depth: Option<u32>) -> Traversal {
Traversal::anonymous_with_step(
traversal::algorithm_steps::BfsTraversalStep::new(max_depth, None),
)
}
pub fn dfs(max_depth: Option<u32>) -> Traversal {
Traversal::anonymous_with_step(
traversal::algorithm_steps::DfsTraversalStep::new(max_depth, None),
)
}
}