/**
 * WorkbookWriter - Node.js Streaming Excel Writer
 *
 * Extends base with file path support and file system image loading.
 */
import fs from "fs";
import { WorksheetWriter } from "./worksheet-writer.js";
import { WorkbookWriterBase } from "./workbook-writer.browser.js";
import { mediaPath } from "../utils/ooxml-paths.js";
class WorkbookWriter extends WorkbookWriterBase {
    constructor(options = {}) {
        super(options, WorksheetWriter);
    }
    /**
     * Create output stream - supports filename option in Node.js
     */
    _createOutputStream(options) {
        if (options.filename) {
            return fs.createWriteStream(options.filename);
        }
        return super._createOutputStream(options);
    }
    /**
     * Add media files - supports loading from file system
     */
    addMedia() {
        return Promise.all(this.media.map(async (medium) => {
            if (medium.type === "image") {
                const filename = mediaPath(medium.name);
                // Node.js: support loading from file
                if (medium.filename) {
                    // `fs.readFile` returns a `Buffer` (which is already a `Uint8Array`).
                    // Avoid copying into a new `Uint8Array` for performance.
                    const data = await fs.promises.readFile(medium.filename);
                    this._addFile(data, filename);
                    return;
                }
                if (medium.buffer) {
                    this._addFile(medium.buffer, filename);
                    return;
                }
                if (medium.base64) {
                    const content = medium.base64.substring(medium.base64.indexOf(",") + 1);
                    this._addFile(content, filename, true);
                    return;
                }
            }
            throw new Error("Unsupported media");
        }));
    }
}
export { WorkbookWriter };
