import type {} from "@atcute/lexicons";
import * as v from "@atcute/lexicons/validations";
import type {} from "@atcute/lexicons/ambient";

const _linkDescriptorSchema = /*#__PURE__*/ v.object({
  $type: /*#__PURE__*/ v.optional(
    /*#__PURE__*/ v.literal("sh.tangled.query.enrichResponse#linkDescriptor"),
  ),
  /**
   * Link source: the collection whose records are counted, a colon, then a RecordPath naming the reference field (e.g. sh.tangled.feed.star:subject, sh.tangled.feed.star:subject.uri), or an envelope field: .repo (authoring repo), .collection, .rkey, or bare . (the record's own at-uri).
   */
  source: /*#__PURE__*/ v.string(),
  /**
   * What to compute over matching records: how many records (count), how many distinct authors (distinctAuthors), or the viewer's own record among the matches (viewer, requires the top-level viewer param). Results land in the stats sidecar at stats[ref][source][type].
   */
  type: /*#__PURE__*/ v.string<
    "count" | "distinctAuthors" | "viewer" | (string & {})
  >(),
});
const _mainSchema = /*#__PURE__*/ v.procedure(
  "sh.tangled.query.enrichResponse",
  {
    params: null,
    input: {
      type: "lex",
      schema: /*#__PURE__*/ v.object({
        /**
         * Counts to compute for each found reference. A descriptor only applies to references it can meaningfully point at: a did can carry author-side counts (.repo), an at-uri can carry subject-side counts.
         */
        get enrich() {
          return /*#__PURE__*/ v.array(linkDescriptorSchema);
        },
        /**
         * Parameters for the inner query, exactly as it declares them.
         */
        params: /*#__PURE__*/ v.optional(/*#__PURE__*/ v.unknown()),
        /**
         * RecordPaths into the inner response restricting which references get counted. When omitted, every did / at-uri / strong-ref in the response is counted. Paths that match nothing yield empty stats rather than an error.
         */
        sources: /*#__PURE__*/ v.optional(
          /*#__PURE__*/ v.array(/*#__PURE__*/ v.string()),
        ),
        /**
         * DID whose own relation to each reference is looked up for viewer-type descriptors, e.g. whether this did starred the found repo and with which record. Required when any enrich descriptor uses type viewer.
         */
        viewer: /*#__PURE__*/ v.optional(/*#__PURE__*/ v.didString()),
        /**
         * NSID of the inner query to run, e.g. sh.tangled.repo.listRepos.
         */
        xrpc: /*#__PURE__*/ v.nsidString(),
      }),
    },
    output: {
      type: "lex",
      schema: /*#__PURE__*/ v.object({
        /**
         * The inner query's response, unmodified.
         */
        output: /*#__PURE__*/ v.unknown(),
        /**
         * Reference value (did or at-uri) -> link source (echoed verbatim from the request) -> aggregation results. Example: stats["did:plc:..."]["sh.tangled.graph.follow:subject"] = {"count": 12, "distinctAuthors": 11}.
         */
        stats: /*#__PURE__*/ v.unknown(),
      }),
    },
  },
);

type linkDescriptor$schematype = typeof _linkDescriptorSchema;
type main$schematype = typeof _mainSchema;

export interface linkDescriptorSchema extends linkDescriptor$schematype {}
export interface mainSchema extends main$schematype {}

export const linkDescriptorSchema =
  _linkDescriptorSchema as linkDescriptorSchema;
export const mainSchema = _mainSchema as mainSchema;

export interface LinkDescriptor extends v.InferInput<
  typeof linkDescriptorSchema
> {}

export interface $params {}
export interface $input extends v.InferXRPCBodyInput<mainSchema["input"]> {}
export interface $output extends v.InferXRPCBodyInput<mainSchema["output"]> {}

declare module "@atcute/lexicons/ambient" {
  interface XRPCProcedures {
    "sh.tangled.query.enrichResponse": mainSchema;
  }
}
