use proc_macro2::{Span, TokenStream};
use quote::quote_spanned;
use syn::{parse::Parser, punctuated::Punctuated, token::Comma, Ident, ItemFn, Result};

pub fn alias_fn_impl(item: ItemFn, attr: TokenStream) -> Result<TokenStream> {
    let parser = Punctuated::<Ident, Comma>::parse_terminated;
    let names = parser.parse2(attr)?;

    let span = Span::call_site();
    let alias_fns: Vec<ItemFn> = names
        .iter()
        .map(|name| {
            let mut alias_fn = item.clone();
            alias_fn.sig.ident = Ident::new(&name.to_string(), span);
            alias_fn
        })
        .collect();

    let expanded = quote_spanned! { span =>
        #item
        #( #alias_fns )*
    };

    Ok(expanded)
}
