use oxc_ast::ast::{BindingPattern, ObjectPattern};
use oxc_span::GetSpan;
use vize_carton::FxHashMap;
use crate::types::BindingType;
use super::{PropsDestructureBinding, PropsDestructuredBindings};
use vize_carton::{String, ToCompactString};
pub fn process_props_destructure(
pattern: &ObjectPattern<'_>,
source: &str,
) -> (
PropsDestructuredBindings,
FxHashMap<String, BindingType>,
FxHashMap<String, String>,
) {
let mut result = PropsDestructuredBindings::default();
let mut binding_metadata: FxHashMap<String, BindingType> = FxHashMap::default();
let mut props_aliases: FxHashMap<String, String> = FxHashMap::default();
for prop in pattern.properties.iter() {
let key = resolve_object_key(&prop.key, source);
if let Some(key) = key {
match &prop.value {
BindingPattern::AssignmentPattern(assign) => {
if let BindingPattern::BindingIdentifier(id) = &assign.left {
let local = id.name.to_compact_string();
let default_expr = &source
[assign.right.span().start as usize..assign.right.span().end as usize];
result.bindings.insert(
key.clone(),
PropsDestructureBinding {
local: local.clone(),
default: Some(default_expr.to_compact_string()),
},
);
if local != key {
binding_metadata.insert(local.clone(), BindingType::PropsAliased);
props_aliases.insert(local, key);
} else {
binding_metadata.insert(local.clone(), BindingType::Props);
}
}
}
BindingPattern::BindingIdentifier(id) => {
let local = id.name.to_compact_string();
result.bindings.insert(
key.clone(),
PropsDestructureBinding {
local: local.clone(),
default: None,
},
);
if local != key {
binding_metadata.insert(local.clone(), BindingType::PropsAliased);
props_aliases.insert(local, key);
} else {
binding_metadata.insert(local.clone(), BindingType::Props);
}
}
_ => {
}
}
}
}
if let Some(rest) = &pattern.rest {
if let BindingPattern::BindingIdentifier(id) = &rest.argument {
let rest_name = id.name.to_compact_string();
result.rest_id = Some(rest_name.clone());
binding_metadata.insert(rest_name, BindingType::SetupReactiveConst);
}
}
(result, binding_metadata, props_aliases)
}
fn resolve_object_key(key: &oxc_ast::ast::PropertyKey<'_>, _source: &str) -> Option<String> {
match key {
oxc_ast::ast::PropertyKey::StaticIdentifier(id) => Some(id.name.to_compact_string()),
oxc_ast::ast::PropertyKey::StringLiteral(lit) => Some(lit.value.to_compact_string()),
oxc_ast::ast::PropertyKey::NumericLiteral(lit) => Some(lit.value.to_compact_string()),
_ => None, }
}