{"version":3,"file":"exporter.cjs","names":["nodes: ExportNode[]","exportNode: ExportNode"],"sources":["../../src/export/exporter.ts"],"sourcesContent":["import type {\n  ImmutableTree,\n} from \"../immutable-tree.js\";\nimport type {\n  Node,\n} from \"../node.js\";\nimport type {\n  ExportNode,\n} from \"./types.js\";\n\n/**\n * Exporter exports nodes from an ImmutableTree in depth-first post-order (LRN).\n * This order must be preserved when importing to recreate the same tree structure.\n *\n * Based on Go IAVL export.go lines 28-99.\n *\n * Usage:\n * ```typescript\n * const exporter = tree.export();\n * for await (const node of exporter) {\n *   // Process exported node\n * }\n * ```\n */\nexport class Exporter {\n  private tree: ImmutableTree;\n\n  constructor(tree: ImmutableTree) {\n    if (!tree) {\n      throw new Error(\"tree is nil: iavl/export newExporter failed to create\");\n    }\n    // Empty tree is allowed - it just exports no nodes\n    this.tree = tree;\n  }\n\n  /**\n   * Returns an async iterator that yields exported nodes in post-order (LRN).\n   * Nodes are exported depth-first post-order, which means:\n   * - Left child nodes are exported before right child nodes\n   * - Child nodes are exported before parent nodes\n   *\n   * This order is required for import to work correctly.\n   *\n   * Based on Go export.go lines 62-80.\n   */\n  async* [Symbol.asyncIterator](): AsyncIterableIterator<ExportNode> {\n    const root = this.tree.getRoot();\n    if (!root) {\n      return;\n    }\n\n    // Collect nodes in post-order (LRN) during traversal\n    const nodes: ExportNode[] = [];\n\n    await root.traversePost(this.tree.ndb, true, (node: Node) => {\n      // Export this node\n      const exportNode: ExportNode = {\n        key: node.key || new Uint8Array(0),\n        value: node.value || new Uint8Array(0),\n        version: node.nodeKey?.version || 0n,\n        height: node.subtreeHeight,\n      };\n\n      nodes.push(exportNode);\n      return false; // continue traversal\n    });\n\n    // Yield all collected nodes\n    for (const node of nodes) {\n      yield node;\n    }\n  }\n\n  /**\n   * Alternative API compatible with Go's Next() pattern.\n   * Returns the next exported node, or null when done.\n   *\n   * Note: Prefer using `for await (const node of exporter)` instead.\n   */\n  private iterator: AsyncIterator<ExportNode> | null = null;\n\n  async next(): Promise<ExportNode | null> {\n    if (!this.iterator) {\n      this.iterator = this[Symbol.asyncIterator]();\n    }\n\n    const result = await this.iterator.next();\n    if (result.done) {\n      return null;\n    }\n    return result.value;\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAwBA,IAAa,WAAb,MAAsB;CACpB,AAAQ;CAER,YAAY,MAAqB;AAC/B,MAAI,CAAC,KACH,OAAM,IAAI,MAAM,wDAAwD;AAG1E,OAAK,OAAO;;;;;;;;;;;;CAad,QAAQ,OAAO,iBAAoD;EACjE,MAAM,OAAO,KAAK,KAAK,SAAS;AAChC,MAAI,CAAC,KACH;EAIF,MAAMA,QAAsB,EAAE;AAE9B,QAAM,KAAK,aAAa,KAAK,KAAK,KAAK,OAAO,SAAe;GAE3D,MAAMC,aAAyB;IAC7B,KAAK,KAAK,OAAO,IAAI,WAAW,EAAE;IAClC,OAAO,KAAK,SAAS,IAAI,WAAW,EAAE;IACtC,SAAS,KAAK,SAAS,WAAW;IAClC,QAAQ,KAAK;IACd;AAED,SAAM,KAAK,WAAW;AACtB,UAAO;IACP;AAGF,OAAK,MAAM,QAAQ,MACjB,OAAM;;;;;;;;CAUV,AAAQ,WAA6C;CAErD,MAAM,OAAmC;AACvC,MAAI,CAAC,KAAK,SACR,MAAK,WAAW,KAAK,OAAO,gBAAgB;EAG9C,MAAM,SAAS,MAAM,KAAK,SAAS,MAAM;AACzC,MAAI,OAAO,KACT,QAAO;AAET,SAAO,OAAO"}