// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// Generates the file `api.txt`, which describes the analyzer public API.
library;

import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer_testing/package_root.dart' as pkg_root;
import 'package:analyzer_utilities/src/api_summary/src/api_summary_customizer.dart';
import 'package:analyzer_utilities/tool/api.dart';
import 'package:analyzer_utilities/tools.dart';

Future<void> main() async {
  await GeneratedContent.generateAll(pkg_root.packageRoot, allTargets);
}

/// A list of all targets generated by this code generator.
final List<GeneratedContent> allTargets = allTargetsForPackage(
  'analyzer',
  createCustomizer: _ApiSummaryCustomizer.new,
);

/// Customization of the API summary tool that recognizes the
/// `@AnalyzerPublicApi` annotation.
base class _ApiSummaryCustomizer extends ApiSummaryCustomizer {
  @override
  bool shouldShowDetails(Element element) =>
      super.shouldShowDetails(element) ||
      element.metadata.annotations.any(_isPublicApiAnnotation);

  bool _isPublicApiAnnotation(ElementAnnotation annotation) {
    if (annotation.computeConstantValue() case DartObject(
      type: InterfaceType(element: InterfaceElement(name: 'AnalyzerPublicApi')),
    )) {
      return true;
    } else {
      return false;
    }
  }
}
