"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.processAudio = processAudio;
exports.convertAudioToText = convertAudioToText;
exports.processVideo = processVideo;
exports.convertToText = convertToText;
const pdf_parse_1 = __importDefault(require("pdf-parse"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const utils_1 = require("./utils");
function getMediaProcessor() {
    const { services } = require("./services");
    return services().MediaProcessor;
}
async function processAudio(filePath, reusePreviousTranscript = true, chunkTime = 30) {
    const parsed = path.parse(filePath);
    const outputPath = `${parsed.dir}/${parsed.name}/transcript.json`;
    const exists = await (0, utils_1.fileExists)(outputPath);
    if (exists && reusePreviousTranscript) {
        console.log(`Transcription ${outputPath} already exists, skipping`);
        const fileContent = await (0, utils_1.readFile)(outputPath, "utf8");
        return outputPath.endsWith("txt")
            ? fileContent.split("\n")
            : JSON.parse(fileContent);
    }
    const mediaProcessor = getMediaProcessor();
    return mediaProcessor.processAudio(filePath, reusePreviousTranscript, chunkTime);
}
async function convertAudioToText(filePath, reusePreviousTranscript = true, chunkTime = 30) {
    const audios = await processAudio(filePath, reusePreviousTranscript, chunkTime);
    let fullString = "";
    for (let i = 0; i < audios.length; i++) {
        const audio = audios[i];
        fullString += `[${i * chunkTime}:${(i + 1) * chunkTime}s] ${audio}`;
    }
    return fullString;
}
async function processVideo(filePath, reusePreviousTranscript = true, chunkTime = 30) {
    const parsed = path.parse(filePath);
    const outputPath = `${parsed.dir}/${parsed.name}/video.json`;
    console.log("Processing audio...");
    const transcriptions = await processAudio(filePath, reusePreviousTranscript, chunkTime);
    return transcriptions;
}
async function convertVideoToText(filePath, reusePreviousTranscript = true, chunkTime = 30) {
    const transcriptions = await processVideo(filePath, reusePreviousTranscript, chunkTime);
    if (Array.isArray(transcriptions)) {
        return transcriptions.join("\n");
    }
    return String(transcriptions);
}
async function convertPdfToText(filePath) {
    const existingPdfBytes = fs.readFileSync(filePath);
    const data = await (0, pdf_parse_1.default)(existingPdfBytes);
    return data.text;
}
async function convertToText(filePath) {
    const extension = filePath.split(".").pop();
    switch (extension) {
        case "mp4":
        case "webm":
        case "mov":
        case "mpeg":
            return convertVideoToText(filePath);
        case "mp3":
        case "mpga":
        case "m4a":
        case "wav":
            return convertAudioToText(filePath);
        case "pdf":
            return convertPdfToText(filePath);
        default:
            return (await (0, utils_1.readFile)(filePath, "utf8")) || "";
    }
}
//# sourceMappingURL=conversion.js.map