use rustc_feature::AttributeStability;
use rustc_hir::attrs::diagnostic::Directive;
use rustc_session::lint::builtin::MISPLACED_DIAGNOSTIC_ATTRIBUTES;

use crate::attributes::diagnostic::*;
use crate::attributes::prelude::*;
use crate::diagnostics::DiagnosticOnUnmatchArgsOnlyForMacros;

#[derive(Default)]
pub(crate) struct OnUnmatchArgsParser {
    span: Option<Span>,
    directive: Option<(Span, Directive)>,
}

impl AttributeParser for OnUnmatchArgsParser {
    const ATTRIBUTES: AcceptMapping<Self> = &[(
        &[sym::diagnostic, sym::on_unmatch_args],
        template!(List: &[r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#]),
        AttributeStability::Stable, // Unstable, stability checked manually in the parser
        |this, cx, args| {
            if !cx.features().diagnostic_on_unmatch_args() {
                return;
            }

            let span = cx.attr_span;
            this.span = Some(span);

            if !matches!(cx.target, Target::MacroDef) {
                cx.emit_lint(
                    MISPLACED_DIAGNOSTIC_ATTRIBUTES,
                    DiagnosticOnUnmatchArgsOnlyForMacros,
                    span,
                );
                return;
            }

            let mode = Mode::DiagnosticOnUnmatchArgs;
            let Some(items) = parse_list(cx, args, mode) else { return };

            let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) else {
                return;
            };
            merge_directives(cx, &mut this.directive, (span, directive));
        },
    )];

    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);

    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
        if let Some(_span) = self.span {
            Some(AttributeKind::OnUnmatchArgs { directive: self.directive.map(|d| Box::new(d.1)) })
        } else {
            None
        }
    }
}
