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

use crate::ShouldEmit;
use crate::attributes::diagnostic::*;
use crate::attributes::prelude::*;
use crate::diagnostics::DiagnosticOnUnknownOnlyForImports;

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

impl OnUnknownParser {
    fn parse<'sess>(&mut self, cx: &mut AcceptContext<'_, 'sess>, args: &ArgParser, mode: Mode) {
        if let Some(features) = cx.features
            && !features.diagnostic_on_unknown()
        {
            // `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
            args.ignore_args();
            return;
        }
        let span = cx.attr_span;
        self.span = Some(span);

        // At early parsing we get passed `Target::Crate` regardless of the item we're on.
        // Therefore, only do target checking if we can emit.
        let early = matches!(cx.should_emit, ShouldEmit::Nothing);

        if !early && !matches!(cx.target, Target::Use) {
            let target_span = cx.target_span;
            cx.emit_lint(
                MISPLACED_DIAGNOSTIC_ATTRIBUTES,
                DiagnosticOnUnknownOnlyForImports { target_span },
                span,
            );
            return;
        }

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

        if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
            merge_directives(cx, &mut self.directive, (span, directive));
        };
    }
}

impl AttributeParser for OnUnknownParser {
    const ATTRIBUTES: AcceptMapping<Self> = &[(
        &[sym::diagnostic, sym::on_unknown],
        template!(List: &[r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#]),
        AttributeStability::Stable, // Unstable, stability checked manually in the parser
        |this, cx, args| {
            this.parse(cx, args, Mode::DiagnosticOnUnknown);
        },
    )];
    // "Allowed" for all targets, but noop for all but use statements.
    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);

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