/**
 * WorkbookReader - Node.js Streaming Workbook Reader
 *
 * Extends base with file path support and temp file storage for large files.
 */
import fs from "fs";
import os from "os";
import { join } from "path";
import { iterateStream } from "../utils/iterate-stream.js";
import { WorksheetReader } from "./worksheet-reader.js";
import { HyperlinkReader } from "./hyperlink-reader.js";
import { WorkbookReaderBase, WorkbookReaderOptionsSchema } from "./workbook-reader.browser.js";
class WorkbookReader extends WorkbookReaderBase {
    constructor(input, options = {}) {
        super(input, options, WorksheetReader, HyperlinkReader);
        this.input = input;
    }
    _getStream(input) {
        if (typeof input === "string") {
            return fs.createReadStream(input);
        }
        return super._getStream(input);
    }
    async _storeWaitingWorksheet(sheetNo, entry) {
        const tmpDir = fs.mkdtempSync(join(os.tmpdir(), "excelts-"));
        const path = join(tmpDir, `sheet${sheetNo}.xml`);
        const cleanup = () => fs.rm(tmpDir, { recursive: true, force: true }, () => { });
        const writePromise = new Promise((resolve, reject) => {
            const tempStream = fs.createWriteStream(path);
            tempStream.on("error", reject);
            tempStream.on("finish", resolve);
            entry.pipe(tempStream);
        });
        return { sheetNo, path, cleanup, writePromise };
    }
    async *_processWaitingWorksheets(waitingWorksheets) {
        for (const ws of waitingWorksheets) {
            await ws.writePromise;
            const fileStream = fs.createReadStream(ws.path);
            try {
                yield* this._parseWorksheet(iterateStream(fileStream), ws.sheetNo);
            }
            finally {
                fileStream.close();
                ws.cleanup();
            }
        }
    }
}
export { WorkbookReader, WorkbookReaderOptionsSchema };
