/// The deep-scan surface: the full JS-exported shape of every `@ExpoModule`, `@SharedObject`,
/// `@Record`, `@Union`, and `@ExpoView` type and every `Enumerable` enum, with the per-member detail a
/// TypeScript type generator needs. Read syntactically (like the macros): each boundary type becomes a
/// structured `TypeNode` tree so the consumer walks a tagged tree rather than re-parsing Swift type
/// syntax.
///
/// Every declaration and member carries the `platforms` that build it, and the ones that can be
/// written with an access modifier carry the `accessLevel` they spell. A declaration no platform
/// builds is not reported.

/// One Apple OS that builds a declaration.
struct ExportedPlatform: Encodable, Equatable {
  /// The OS, spelled as `os(...)` spells it: `iOS`, `macOS`, `tvOS`, `watchOS`, or `visionOS`.
  let name: String

  /// The version spelled in `@available` (`17.0` in `@available(iOS 17.0, *)`), or `nil` when no
  /// attribute on the declaration or its enclosing types names one. The latest version wins when
  /// several do.
  let introduced: String?

  /// How the declaration's own `@available` attributes deprecate it on this OS, or `nil` when they
  /// don't. Enclosing types are not read: a member of a deprecated type is not itself reported as
  /// deprecated.
  var deprecated: ExportedDeprecation? = nil
}

/// A deprecation from `@available`: `@available(*, deprecated, ...)` on every OS, or
/// `@available(iOS, deprecated: 16, obsoleted: 18, ...)` on one. Each field is `nil` when not written.
struct ExportedDeprecation: Encodable, Equatable {
  /// The version from `deprecated: 16`; `nil` for a bare `deprecated`.
  var since: String? = nil

  /// The version from `obsoleted: 18`, from which the declaration is no longer available. A
  /// declaration that is only obsoleted is still reported as deprecated.
  var obsoleted: String? = nil

  /// The `message:` text.
  var message: String? = nil

  /// The `renamed:` text: the Swift name of the replacement, as written (`newName`,
  /// `other(value:)`). It is not resolved to a JS name.
  var renamed: String? = nil
}

/// One parameter of a `@JS` function or `@JS init`.
struct ExportedParameter: Encodable, Equatable {
  /// The argument label (the `first` name): `to` in `func move(to point: Point)`, or `_` if unlabeled.
  let label: String

  /// The internal parameter name (the `second` name, else the same as `label`): `point` above.
  let name: String

  var type: TypeNode

  /// True when the caller may omit it: a default value or an optional type. Distinct from the type
  /// being `.optional` (a defaulted non-optional is also omittable). Encoded as `optional`.
  let isOptional: Bool

  /// The default value expression as source text (`1`, `"x"`, `.fast`), or `nil` when none is
  /// written. Not evaluated: it may be any expression.
  let defaultValue: String?

  private enum CodingKeys: String, CodingKey {
    case label, name, type, defaultValue
    case isOptional = "optional"
  }
}

/// One `@JS func` on a module or shared object.
struct ExportedFunction: Encodable, Equatable {
  /// The Swift declaration name. A function with `.withAsync("x")` has two entries with this name.
  let name: String

  /// The JS name it binds under: the `@JS("x")` override, else `name`. The second entry of a function
  /// with `.withAsync("x")` binds under `x`. `nil` for a function bound under a symbol (see `symbol`).
  let jsName: String?

  /// The symbol it binds under with `@JS(symbol: ...)`, or `nil` when it binds under `jsName`.
  /// Exactly one of `jsName` and `symbol` is set.
  var symbol: ExportedSymbol? = nil

  var parameters: [ExportedParameter]

  /// The return type, or `nil` for `Void`. Named `returns` to pair with `parameters`.
  var returns: TypeNode?

  /// `async` (an async function is promise-returning in JS).
  let isAsync: Bool

  /// `throws`.
  let isThrowing: Bool

  /// `static`/`class` member.
  let isStatic: Bool

  /// The spelled access modifier, or `nil` when none is written.
  let accessLevel: String?

  var platforms: [ExportedPlatform]

  /// The `name` of the `@SharedObject` superclass that declares the member, or `nil` for a member of the
  /// type itself. Set on the members that a shared object inherits (see
  /// `ExportedSurface.mergingInheritedMembers(through:)`).
  var inheritedFrom: String? = nil

  /// The flags are encoded under their TS-keyword spellings.
  private enum CodingKeys: String, CodingKey {
    case name, jsName, symbol, parameters, returns, accessLevel, platforms, inheritedFrom
    case isAsync = "async"
    case isThrowing = "throws"
    case isStatic = "static"
  }
}

/// The symbol that a `@JS(symbol: ...)` member binds under.
struct ExportedSymbol: Encodable, Hashable {
  enum Kind: String, Encodable, Hashable {
    /// A well-known symbol, such as `Symbol.iterator`, from `@JS(symbol: .iterator)`.
    case wellKnown
    /// The symbol that the global registry holds for a key, `Symbol.for("app.id")`, from
    /// `@JS(symbol: "app.id")`.
    case registered
  }

  let kind: Kind

  /// The name of the well-known symbol, such as `iterator`, or the registry key.
  let name: String
}

/// One `@JS var` on a module or shared object.
struct ExportedProperty: Encodable, Equatable {
  /// The Swift declaration name.
  let name: String

  /// The JS name it binds under: the `@JS("x")` override, else `name`. `nil` for a property bound
  /// under a symbol (see `symbol`).
  let jsName: String?

  /// The symbol it binds under with `@JS(symbol: ...)`, or `nil` when it binds under `jsName`.
  /// Exactly one of `jsName` and `symbol` is set.
  var symbol: ExportedSymbol? = nil

  /// The value type, or `nil` when undeterminable syntactically (no annotation and no literal
  /// default), the case the macro binds getter-only.
  var type: TypeNode?

  /// True when JS can assign to it: a stored `var` or a computed `var` with a `set`. Encoded as its
  /// inverse, `readonly`.
  let isSettable: Bool

  /// `static`/`class` member. Encoded as `static`.
  let isStatic: Bool

  /// The initial value of a stored property as source text, or `nil` when none is written.
  let defaultValue: String?

  /// The spelled access modifier, or `nil` when none is written. A setter-only modifier like
  /// `private(set)` is not the property's access level.
  let accessLevel: String?

  var platforms: [ExportedPlatform]

  /// The `name` of the `@SharedObject` superclass that declares the member, or `nil` for a member of the
  /// type itself. Set on the members that a shared object inherits (see
  /// `ExportedSurface.mergingInheritedMembers(through:)`).
  var inheritedFrom: String? = nil

  private enum CodingKeys: String, CodingKey {
    case name, jsName, symbol, type, defaultValue, accessLevel, platforms, inheritedFrom
    case isReadonly = "readonly"
    case isStatic = "static"
  }

  func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(name, forKey: .name)
    try container.encodeIfPresent(jsName, forKey: .jsName)
    try container.encodeIfPresent(symbol, forKey: .symbol)
    try container.encodeIfPresent(type, forKey: .type)
    try container.encode(!isSettable, forKey: .isReadonly)
    try container.encode(isStatic, forKey: .isStatic)
    try container.encodeIfPresent(defaultValue, forKey: .defaultValue)
    try container.encodeIfPresent(accessLevel, forKey: .accessLevel)
    try container.encode(platforms, forKey: .platforms)
    try container.encodeIfPresent(inheritedFrom, forKey: .inheritedFrom)
  }
}

/// One `@Record` property: a plain data slot exposing `optional`/`required` (vs. `ExportedProperty`,
/// a JS accessor exposing `readonly`/`static`). A record crosses the boundary by value, so the whole
/// type is read-only in JS; that's a record-level fact and isn't stamped per property.
struct ExportedRecordProperty: Encodable, Equatable {
  let name: String

  /// `@Record` requires a determinable type on every property, so this is never `nil`.
  var type: TypeNode

  /// Optional-typed. Encoded as `optional`.
  let isOptional: Bool

  /// The default value as source text, or `nil` when none is written. A field JS leaves out takes it.
  let defaultValue: String?

  /// The spelled access modifier, or `nil` when none is written. Never `private` or `fileprivate`,
  /// since those exclude a property from the record.
  let accessLevel: String?

  let platforms: [ExportedPlatform]

  /// Whether JS must supply this property, matching the macro's `RecordProperty.isRequired`. Encoded
  /// as `required`.
  var isRequired: Bool {
    return defaultValue == nil && !isOptional
  }

  private enum CodingKeys: String, CodingKey {
    case name, type, defaultValue, accessLevel, platforms
    case isOptional = "optional"
    case isRequired = "required"
  }

  func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(name, forKey: .name)
    try container.encode(type, forKey: .type)
    try container.encode(isOptional, forKey: .isOptional)
    try container.encode(isRequired, forKey: .isRequired)
    try container.encodeIfPresent(defaultValue, forKey: .defaultValue)
    try container.encodeIfPresent(accessLevel, forKey: .accessLevel)
    try container.encode(platforms, forKey: .platforms)
  }
}

/// One `@Event var` on a module or shared object: a typed event JS listens for with
/// `addListener(jsName, …)` rather than calls.
struct ExportedEvent: Encodable, Equatable {
  /// The Swift property name, e.g. `onStatusChange`.
  let name: String

  /// The name JS listens under: the `@Event("x")` override, else `name` with a conventional `on`
  /// prefix stripped and decapitalized (`onStatusChange` -> `statusChange`).
  let jsName: String

  /// The single payload parameter's type, or `nil` for a no-payload `() -> Void` event.
  var payload: TypeNode?

  /// How the event is dispatched: the default `scheduled`, or the `.inline` or `.blocking` option.
  let dispatch: ExportedEventDispatch

  /// The spelled access modifier, or `nil` when none is written.
  let accessLevel: String?

  var platforms: [ExportedPlatform]

  /// The `name` of the `@SharedObject` superclass that declares the event, or `nil` for an event of the
  /// type itself. Set on the events that a shared object inherits (see
  /// `ExportedSurface.mergingInheritedMembers(through:)`).
  var inheritedFrom: String? = nil

  private enum CodingKeys: String, CodingKey {
    case name, jsName, payload, dispatch, accessLevel, platforms, inheritedFrom
  }

  func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(name, forKey: .name)
    try container.encode(jsName, forKey: .jsName)
    try container.encodeIfPresent(payload, forKey: .payload)
    try container.encode(dispatch, forKey: .dispatch)
    try container.encodeIfPresent(accessLevel, forKey: .accessLevel)
    try container.encode(platforms, forKey: .platforms)
    try container.encodeIfPresent(inheritedFrom, forKey: .inheritedFrom)
  }
}

/// A `@ExpoModule` type and its `@JS` surface.
struct ExportedModule: Encodable, Equatable {
  /// The Swift class name. A module is always declared at file scope, since autolinking registers no
  /// other.
  let name: String

  /// The JS module name: `@ExpoModule("Foo")` override, else the class name.
  let jsName: String

  var functions: [ExportedFunction]
  var properties: [ExportedProperty]
  var events: [ExportedEvent]

  /// The classes listed in `@ExpoModule(classes: [...])`, in source order. Each is reachable from JS
  /// as a member of the module.
  var classes: [ExportedClass]

  /// The spelled access modifier, or `nil` when none is written.
  let accessLevel: String?

  let platforms: [ExportedPlatform]

  /// Absolute source path, matching `scan-packages`.
  let file: String
}

/// One entry of `@ExpoModule(classes: [...])`.
struct ExportedClass: Encodable, Equatable {
  /// The `name` of the matching `@SharedObject`, which is qualified when the class is nested
  /// (`Outer.Statement`). The Swift name as spelled in the argument when the scan found no
  /// `@SharedObject` with this name.
  let name: String

  /// The name of the module member JS reads the class from: the matching `@SharedObject`'s `jsName`.
  /// `nil` when the scan found no `@SharedObject` with this name, for example a class defined
  /// outside the scanned paths.
  let jsName: String?
}

/// The `@JS init` of a shared object: the constructor JS calls with `new`.
struct ExportedInitializer: Encodable, Equatable {
  var parameters: [ExportedParameter]

  /// The spelled access modifier, or `nil` when none is written.
  let accessLevel: String?

  let platforms: [ExportedPlatform]
}

/// A `@SharedObject` type: a JS class with an optional `@JS init` constructor plus its `@JS` members.
struct ExportedSharedObject: Encodable, Equatable {
  /// The Swift class name, qualified with the types it is nested in (`Outer.Player`).
  let name: String

  /// The JS class name: `@SharedObject("Foo")` override, else the class name without the enclosing
  /// types.
  let jsName: String

  /// The `@JS init`, or `nil` when there's none. A shared object has at most one; the first is kept
  /// if a malformed source has more. Not named `constructor`, which every JS object inherits.
  var initializer: ExportedInitializer?

  /// What the class inherits from `SharedRef<T>`, directly or through its scanned superclasses, or
  /// `nil` when it doesn't inherit `SharedRef`.
  var sharedRef: ExportedSharedRef?

  /// The nearest superclass with `@SharedObject`, whose JavaScript class core makes the parent of this
  /// one, or `nil` when the class has none and its JavaScript parent is the base class (see
  /// `ExportedSurface.mergingInheritedMembers(through:)`).
  var superclass: ExportedSuperclass? = nil

  /// The members that the class inherits from its `@SharedObject` superclasses, which have
  /// `inheritedFrom`, followed by its own `@JS` members (see
  /// `ExportedSurface.mergingInheritedMembers(through:)`).
  var functions: [ExportedFunction]
  var properties: [ExportedProperty]
  var events: [ExportedEvent]

  /// The spelled access modifier, or `nil` when none is written.
  let accessLevel: String?

  let platforms: [ExportedPlatform]

  let file: String

  /// The superclass as written, without generic arguments, or `nil` when there is none or it is
  /// `SharedObject`, `SharedRef` or `AnySharedObject`. Not encoded: `superclass` reports what it
  /// resolves to.
  var declaredSuperclass: String? = nil

  /// The value of the class's own `nativeRefType` override (see `ExportedSharedRef.nativeRefType`), or
  /// `.none` when it declares none. Not encoded: a subclass of a `SharedRef` subclass reports its own
  /// override in the inherited `sharedRef`.
  var ownNativeRefType: String?? = .none

  private enum CodingKeys: String, CodingKey {
    case name, jsName, initializer, sharedRef, superclass, functions, properties, events, accessLevel, platforms, file
  }
}

/// The superclass of a shared object, the nearest one with `@SharedObject`.
struct ExportedSuperclass: Encodable, Equatable {
  /// The `name` of the superclass, which is qualified when the class is nested (`Media.Player`). The
  /// name as written when the scan found no class with this name, for example a class in another
  /// package. The first entry of an inheritance clause can also be a protocol, which the scan can't
  /// tell from a class in another package. Also the name as written when the superclass depends on the
  /// platform: a class without the macro between them is declared in `#if` branches that lead to
  /// different superclasses.
  let name: String

  /// The `jsName` of the superclass, or `nil` when the scan found no class with this name, or when the
  /// superclass depends on the platform.
  let jsName: String?
}

/// The `SharedRef<T>` superclass of a shared object. A class that inherits `SharedRef` through its
/// superclasses reports the `T` of the nearest one that names it, also a class without the macro. A
/// class that inherits it through a class the scan doesn't find has none.
struct ExportedSharedRef: Encodable, Equatable {
  /// The `T` in `SharedRef<T>`: the native type the object holds, which JS never sees.
  let native: TypeNode

  /// The `nativeRefType` JS reads to tell refs apart, for example `image`. `unknown` (core's default)
  /// when the class doesn't override it, and `nil` when the override isn't a plain string literal.
  let nativeRefType: String?
}

/// A `@Record` type and its properties (data only: no functions, accessors, or constructor).
struct ExportedRecord: Encodable, Equatable {
  /// The Swift type name (struct or class), qualified with the types it is nested in
  /// (`Outer.Options`).
  let name: String

  var properties: [ExportedRecordProperty]

  /// The spelled access modifier, or `nil` when none is written.
  let accessLevel: String?

  let platforms: [ExportedPlatform]

  let file: String
}

/// One case of a reported enum.
///
/// What `rawValue` carries depends on the enum's raw type, and a consumer reads it that way:
/// - `String`: always present, and **decoded** (`case active = "act"` reports `act`, unquoted). A case
///   writing none takes its own name, so nothing is left to derive.
/// - an integer type: present only where written, as **source text** (`1`, `1 << 3`), since the value
///   may be an expression a syntactic scan can't evaluate. Swift also continues from the preceding
///   case's value (`case a = 1; case b` makes `b` 2), and that carry is the consumer's to apply.
/// - no raw type: always absent, since the enum has no raw values.
struct ExportedEnumCase: Encodable, Equatable {
  /// The case name as declared.
  let name: String

  /// The raw value, decoded for a string and verbatim source text otherwise, or `nil` per the rule
  /// above.
  let rawValue: String?

  let platforms: [ExportedPlatform]

  private enum CodingKeys: String, CodingKey {
    case name, rawValue, platforms
  }

  func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(name, forKey: .name)
    try container.encodeIfPresent(rawValue, forKey: .rawValue)
    try container.encode(platforms, forKey: .platforms)
  }
}

/// An `Enumerable` enum: a type that crosses the boundary as its raw value rather than as an object.
/// Detected by conformance, not by a macro attribute, because core converts it through its
/// `RawRepresentable`/`Enumerable` conformance (`Coding/…+Enumerable`) with no macro involved.
struct ExportedEnum: Encodable, Equatable {
  /// The Swift enum name, qualified with the types it is nested in (`Options.Encoding`). No `jsName`:
  /// an enum carries no macro to spell an override on, and it reaches JS as its raw values, not under
  /// a bound name.
  let name: String

  /// The raw value type as written (`String`, `Int`, …), or `nil` for a bare `Enumerable` conformance
  /// with no raw type. A `nil` raw type is reported as-is rather than dropped: the enum is still
  /// declared convertible, and the consumer decides how to treat it.
  let rawType: TypeNode?

  let cases: [ExportedEnumCase]

  /// The spelled access modifier, or `nil` when none is written.
  let accessLevel: String?

  let platforms: [ExportedPlatform]

  let file: String

  private enum CodingKeys: String, CodingKey {
    case name, rawType, cases, accessLevel, platforms, file
  }

  func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(name, forKey: .name)
    try container.encodeIfPresent(rawType, forKey: .rawType)
    try container.encode(cases, forKey: .cases)
    try container.encodeIfPresent(accessLevel, forKey: .accessLevel)
    try container.encode(platforms, forKey: .platforms)
    try container.encode(file, forKey: .file)
  }
}

/// One alternative a `@Union` may hold: the Swift case name plus its payload type. Not a member in the
/// sense a module or shared object has members (functions, properties, events).
struct ExportedUnionMember: Encodable, Equatable {
  /// The case name as declared. Swift-side only: discrimination is structural, so it never reaches JS,
  /// but it ties an alternative back to the declaration.
  let name: String

  /// The associated value's type: what this alternative decodes from. Named `type` like every other
  /// type-valued field here; `payload` already means an event's argument type on `ExportedEvent`.
  var type: TypeNode

  let platforms: [ExportedPlatform]

  private enum CodingKeys: String, CodingKey {
    case name, type, platforms
  }
}

/// A `@Union` enum: a typed union of its cases' payload types (`A | B | C` in TypeScript).
///
/// **`members` is ordered, and the order is part of the contract.** Decode tries each payload in
/// declaration order and takes the first that succeeds, so where two shapes overlap (`Int` and
/// `Double`, two compatible records) the earlier case wins. Reordering them describes a different
/// union than the one the module runs.
struct ExportedUnion: Encodable, Equatable {
  /// The Swift enum name, qualified with the types it is nested in (`Options.Value`). No `jsName`:
  /// `@Union` takes no arguments, and a union reaches JS as its payload types rather than under a bound
  /// name.
  let name: String

  var members: [ExportedUnionMember]

  /// The spelled access modifier, or `nil` when none is written.
  let accessLevel: String?

  let platforms: [ExportedPlatform]

  let file: String
}

/// An `@ExpoView` class or SwiftUI view struct with the props and events of the `@ViewProps` struct in its
/// generic argument. It is reported on its own rather than under a module, because core reaches it through its
/// `AnyNativeView` conformance.
struct ExportedView: Encodable, Equatable {
  /// The Swift type name. Only a view declared at file scope is reported, as in `scan-packages`.
  let name: String

  /// The name the view registers under: the `@ExpoView<CardProps>("Foo")` override when present,
  /// otherwise the class name.
  let jsName: String

  /// The props type: the generic argument of `@ExpoView<CardProps>`, as written without backticks, or
  /// the qualified name of the scanned `@ViewProps` struct it names (`CardView.Props`). A class without
  /// the generic argument is not reported, because the macro rejects it.
  var propsType: String

  /// The name `propsType` is looked up under: its spelling without generic arguments, or `nil` when it
  /// isn't a plain or qualified type name. Not encoded.
  var propsTypeLookupName: String? = nil

  /// The value props of the `@ViewProps` struct, or `nil` when the scan declares no struct named
  /// `propsType` on a platform of the view. A struct shared by several views is copied into each of
  /// them, and a struct declared in several `#if` branches contributes the props of each declaration
  /// whose platforms overlap the view's. The props of the struct that `@ViewProps(extending:)` names
  /// follow, when the scan declares it.
  var props: [ExportedViewProp]? = nil

  /// The events of the `@ViewProps` struct, then those of the struct that it extends, or `nil` when
  /// `props` is `nil`.
  var events: [ExportedViewEvent]? = nil

  /// The spelled access modifier, or `nil` when none is written.
  let accessLevel: String?

  let platforms: [ExportedPlatform]

  let file: String

  private enum CodingKeys: String, CodingKey {
    case name, jsName, propsType, props, events, accessLevel, platforms, file
  }
}

/// One value prop of a `@ViewProps` struct: a stored property that isn't function-typed. Like
/// `ExportedRecordProperty`, but with no `required`: core resets a prop that React leaves out to its
/// default, so JS can always omit one.
struct ExportedViewProp: Encodable, Equatable {
  let name: String

  /// The annotated type, or the type of a literal initial value. A property with neither is not
  /// reported, because the macro rejects it.
  var type: TypeNode

  /// The initial value as source text, or `nil` when none is written.
  let defaultValue: String?

  /// The spelled access modifier, or `nil` when none is written. Never `private` or `fileprivate`,
  /// since those exclude a property from the props.
  let accessLevel: String?

  let platforms: [ExportedPlatform]

  /// The qualified name of the `@ViewProps` struct that declares the prop, which its refs are looked up
  /// from: the view's props struct, or a struct that it extends. Set when the prop is copied into a view.
  /// Not encoded.
  var scope: String? = nil

  /// Optional-typed, read from `type`, so every spelling of an optional counts (`T?`, `T!`,
  /// `Optional<T>`, `(T?)`). Encoded as `optional`.
  var isOptional: Bool {
    if case .optional = type {
      return true
    }
    return false
  }

  private enum CodingKeys: String, CodingKey {
    case name, type, defaultValue, accessLevel, platforms
    case isOptional = "optional"
  }

  func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(name, forKey: .name)
    try container.encode(type, forKey: .type)
    try container.encode(isOptional, forKey: .isOptional)
    try container.encodeIfPresent(defaultValue, forKey: .defaultValue)
    try container.encodeIfPresent(accessLevel, forKey: .accessLevel)
    try container.encode(platforms, forKey: .platforms)
  }
}

/// One event of a `@ViewProps` struct: a function-typed stored property. Its name is the React prop
/// name as written, with no `on` prefix stripped, so it carries no `jsName`.
struct ExportedViewEvent: Encodable, Equatable {
  /// The Swift property name, e.g. `onTap`.
  let name: String

  /// The type of the function's single parameter, or `nil` when it takes none (`() -> Void`) or more
  /// than one.
  var payload: TypeNode?

  /// The spelled access modifier, or `nil` when none is written.
  let accessLevel: String?

  let platforms: [ExportedPlatform]

  /// The qualified name of the `@ViewProps` struct that declares the event, which the refs of its
  /// payload are looked up from, as for `ExportedViewProp.scope`. Not encoded.
  var scope: String? = nil

  private enum CodingKeys: String, CodingKey {
    case name, payload, accessLevel, platforms
  }
}

/// A `@ViewProps` struct the scan found. Not encoded: `ExportedSurface.joiningViewProps(_:)` copies its
/// props and events into each view that names it, and a struct that no view names is not reported.
struct ViewPropsDeclaration: Equatable {
  /// The Swift struct name, qualified with the types it is nested in (`CardView.Props`).
  let name: String

  /// The value props, in declaration order.
  let props: [ExportedViewProp]

  /// The events, in declaration order.
  let events: [ExportedViewEvent]

  /// The platforms that build the struct, to match it with the views that build on them.
  let platforms: [ExportedPlatform]

  /// The props type of `@ViewProps(extending: BaseProps.self)` as written, without backticks, which the
  /// struct is looked up under from this one. `nil` when the struct extends no props.
  var baseLookupName: String? = nil
}

/// The exported types grouped by kind, nested under `exports` in the result so the surface is one
/// self-contained object separate from `stats`.
struct ExportedSurface: Encodable, Equatable {
  let modules: [ExportedModule]
  let sharedObjects: [ExportedSharedObject]
  let records: [ExportedRecord]
  let enums: [ExportedEnum]
  let unions: [ExportedUnion]
  let views: [ExportedView]
}

/// How an `@Event` is dispatched to JavaScript, from its option. Mirrors `EventDispatch` in the macros.
enum ExportedEventDispatch: String, Encodable, Equatable {
  case scheduled
  case inline
  case blocking
}

/// Version of the `scan-exports` output shape. Bumped on any breaking change to the envelope or to
/// anything under `exports`, so a consumer can verify it understands the output before trusting it.
/// Versioned independently of `scanPackagesSchemaVersion`: the two commands serve different consumers
/// and change for different reasons. Version 2 added `events` to modules and shared objects; version 3
/// added `enums`; version 4 added `unions`; version 5 resolves refs, adding `refKind` and correcting
/// an enum ref's `typeof`; version 6 added `classes` to modules; version 7 reports nested types under
/// qualified names and gives a resolved ref the name of its declaration; version 8 reports
/// declarations in `#if` blocks, adds `platforms` (with deprecations), `accessLevel`, and
/// `defaultValue`, and replaces `constructorParameters` with `initializer`; version 9 adds the
/// `sharedRef` type kind and the `sharedRef` of a shared object; version 10 adds the `tuple` and `object` type kinds (named after a tuple alias)
/// and resolves the tuple type aliases declared in `@ExpoModule` and `@SharedObject` classes; version 11
/// replaces the `sync` flag of an event with `dispatch`; version 12 adds the `symbol` of a function or
/// property bound with `@JS(symbol: ...)`, which then has no `jsName`. A new field that older consumers
/// can ignore, such as `views`, doesn't change the version.
let scanExportsSchemaVersion = 12

/// The `scan-exports` result: the surface plus the run's stats. A distinct envelope from
/// `ScanPackagesResult` (different consumer: TS generation vs. autolinking).
struct ScanExportsResult: Encodable, Equatable {
  let schemaVersion: Int
  let exports: ExportedSurface
  let stats: ScanStats
}
