"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    AudioSubtitlesReader: function() {
        return AudioSubtitlesReader;
    },
    AudioTranscriptParagraphsReader: function() {
        return AudioTranscriptParagraphsReader;
    },
    AudioTranscriptReader: function() {
        return AudioTranscriptReader;
    },
    AudioTranscriptSentencesReader: function() {
        return AudioTranscriptSentencesReader;
    }
});
const _env = require("@llamaindex/env");
const _assemblyai = require("assemblyai");
const _Node = require("../Node.js");
/**
 * Base class for AssemblyAI Readers.
 */ class AssemblyAIReader {
    client;
    /**
   * Creates a new AssemblyAI Reader.
   * @param assemblyAIOptions The options to configure the AssemblyAI Reader.
   * Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
   */ constructor(assemblyAIOptions){
        let options = assemblyAIOptions;
        if (!options) {
            options = {};
        }
        if (!options.apiKey) {
            options.apiKey = (0, _env.getEnv)("ASSEMBLYAI_API_KEY");
        }
        if (!options.apiKey) {
            throw new Error("No AssemblyAI API key provided. Pass an `apiKey` option, or configure the `ASSEMBLYAI_API_KEY` environment variable.");
        }
        this.client = new _assemblyai.AssemblyAI(options);
    }
    async transcribeOrGetTranscript(params) {
        if (typeof params === "string") {
            return await this.client.transcripts.get(params);
        } else {
            return await this.client.transcripts.transcribe(params);
        }
    }
    async getTranscriptId(params) {
        if (typeof params === "string") {
            return params;
        } else {
            return (await this.client.transcripts.transcribe(params)).id;
        }
    }
}
/**
 * Transcribe audio and read the transcript as a document using AssemblyAI.
 */ class AudioTranscriptReader extends AssemblyAIReader {
    /**
   * Transcribe audio or get a transcript and load the transcript as a document using AssemblyAI.
   * @param params Parameters to transcribe an audio file or get an existing transcript.
   * @returns A promise that resolves to a single document containing the transcript text.
   */ async loadData(params) {
        const transcript = await this.transcribeOrGetTranscript(params);
        return [
            new _Node.Document({
                text: transcript.text || undefined
            })
        ];
    }
}
/**
 * Transcribe audio and return a document for each paragraph.
 */ class AudioTranscriptParagraphsReader extends AssemblyAIReader {
    /**
   * Transcribe audio or get a transcript, and returns a document for each paragraph.
   * @param params The parameters to transcribe audio or get an existing transcript.
   * @returns A promise that resolves to an array of documents, each containing a paragraph of the transcript.
   */ async loadData(params) {
        const transcriptId = await this.getTranscriptId(params);
        const paragraphsResponse = await this.client.transcripts.paragraphs(transcriptId);
        return paragraphsResponse.paragraphs.map((p)=>new _Node.Document({
                text: p.text
            }));
    }
}
/**
 * Transcribe audio and return a document for each sentence.
 */ class AudioTranscriptSentencesReader extends AssemblyAIReader {
    /**
   * Transcribe audio or get a transcript, and returns a document for each sentence.
   * @param params The parameters to transcribe audio or get an existing transcript.
   * @returns A promise that resolves to an array of documents, each containing a sentence of the transcript.
   */ async loadData(params) {
        const transcriptId = await this.getTranscriptId(params);
        const sentencesResponse = await this.client.transcripts.sentences(transcriptId);
        return sentencesResponse.sentences.map((p)=>new _Node.Document({
                text: p.text
            }));
    }
}
/**
 * Transcribe audio a transcript and read subtitles for the transcript as `srt` or `vtt` format.
 */ class AudioSubtitlesReader extends AssemblyAIReader {
    /**
   * Transcribe audio or get a transcript and reads subtitles for the transcript as `srt` or `vtt` format.
   * @param params The parameters to transcribe audio or get an existing transcript.
   * @param subtitleFormat The format of the subtitles, either `srt` or `vtt`.
   * @returns A promise that resolves a document containing the subtitles as the page content.
   */ async loadData(params, subtitleFormat = "srt") {
        const transcriptId = await this.getTranscriptId(params);
        const subtitles = await this.client.transcripts.subtitles(transcriptId, subtitleFormat);
        return [
            new _Node.Document({
                text: subtitles
            })
        ];
    }
}
