use std::collections::{BTreeMap, HashSet};
use tsurara_loader::Loc;
use tsurara_naming::{Context, Names, NamingPolicy};
use tsurara_sem::Model;
use tsurara_sem::ir::{Inline, Object, Type};
use tsurara_sem::operation::Operation;
pub fn candidates(model: &Model) -> Vec<&Inline> {
let mut out = Vec::new();
for d in &model.schemas {
collect(&d.ty, &mut out);
}
for op in model.operations.iter().chain(&model.webhooks) {
collect_operation(op, &mut out);
}
let mut seen = HashSet::new();
out.retain(|c| seen.insert(c.loc.clone()));
out
}
fn collect_operation<'a>(op: &'a Operation, out: &mut Vec<&'a Inline>) {
for p in &op.params {
collect(&p.ty, out);
}
if let Some(b) = &op.body {
for c in &b.contents {
collect(&c.ty, out);
}
}
for r in &op.responses {
for c in &r.contents {
collect(&c.ty, out);
}
for h in &r.headers {
collect(&h.ty, out);
}
}
for cb in &op.callbacks {
for o in &cb.operations {
collect_operation(o, out);
}
}
}
fn collect<'a>(ty: &'a Type, out: &mut Vec<&'a Inline>) {
match ty {
Type::Inline(i) => {
out.push(i);
collect(&i.ty, out);
}
Type::Array(t) => collect(t, out),
Type::Tuple(t) => {
t.items.iter().for_each(|i| collect(i, out));
if let Some(r) = &t.rest {
collect(r, out);
}
}
Type::Union(ts) | Type::Intersection(ts) => ts.iter().for_each(|t| collect(t, out)),
Type::Choice(c) => c.members.iter().for_each(|t| collect(t, out)),
Type::Object(Object { properties, index }) => {
properties.iter().for_each(|p| collect(&p.ty, out));
if let Some(i) = index {
collect(i, out);
}
}
Type::Mapped(m) => collect(&m.schema, out),
_ => {}
}
}
pub fn claim_candidates(
model: &Model,
names: &mut Names,
policy: &dyn NamingPolicy,
scope: &str,
overrides: &BTreeMap<String, String>,
) -> Vec<(Loc, String)> {
candidates(model)
.into_iter()
.map(|c| {
let pointer = format!("#{}", c.loc.pointer);
let name = match overrides.get(&pointer) {
Some(name) => {
names.reserve(scope, name, &pointer);
name.clone()
}
None => {
let ident = policy.convert(Context::Type, &c.preferred_name, false);
names
.claim(policy, Context::Type, scope, &ident, &pointer)
.name
}
};
(c.loc.clone(), name)
})
.collect()
}