"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "NotionReader", {
    enumerable: true,
    get: function() {
        return NotionReader;
    }
});
const _notionmdcrawler = require("notion-md-crawler");
const _Node = require("../Node.js");
class NotionReader {
    crawl;
    /**
   * Constructor for the NotionReader class
   * @param {NotionReaderOptions} options - Configuration options for the reader
   */ constructor({ client, serializers }){
        this.crawl = (0, _notionmdcrawler.crawler)({
            client,
            serializers
        });
    }
    /**
   * Converts Pages to an array of Document objects
   * @param {Page} pages - The Notion pages to convert (Return value of `loadPages`)
   * @returns {Document[]} An array of Document objects
   */ toDocuments(pages) {
        return Object.values(pages).map((page)=>{
            const text = (0, _notionmdcrawler.pageToString)(page);
            return new _Node.Document({
                id_: page.metadata.id,
                text,
                metadata: page.metadata
            });
        });
    }
    /**
   * Loads recursively the Notion page with the specified root page ID.
   * @param {string} rootPageId - The root Notion page ID
   * @returns {Promise<Page[]>} A Promise that resolves to a Pages object(Convertible with the `toDocuments` method)
   */ async loadPages(rootPageId) {
        const iter = this.crawl(rootPageId);
        const pages = [];
        for await (const result of iter){
            if (result.success) {
                pages.push(result.page);
            } else {
                console.error(`Failed to load page (${result.failure.parentId}): ${result.failure.reason}`);
            }
        }
        return pages;
    }
    /**
   * Loads recursively Notion pages and converts them to an array of Document objects
   * @param {string} rootPageId - The root Notion page ID
   * @returns {Promise<Document[]>} A Promise that resolves to an array of Document objects
   */ async loadData(rootPageId) {
        const pages = await this.loadPages(rootPageId);
        return this.toDocuments(pages);
    }
}
