"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;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MediaManager = void 0;
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
// fluent-ffmpeg is an optionalDependency — loaded lazily so missing it only
// fails at call-time, not at module load.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function loadFfmpeg() {
    try {
        return require('fluent-ffmpeg');
    }
    catch {
        throw new Error('fluent-ffmpeg is not installed. ' +
            'Install it with: npm install fluent-ffmpeg');
    }
}
class MediaManager {
    outputDir;
    constructor() {
        this.outputDir = path.join(process.cwd(), '.media_output');
        if (!fs.existsSync(this.outputDir)) {
            fs.mkdirSync(this.outputDir, { recursive: true });
        }
    }
    async extractAudio(inputPath, outputFilename = `audio_${Date.now()}.mp3`) {
        const ffmpeg = loadFfmpeg();
        const outputPath = path.join(this.outputDir, outputFilename);
        return new Promise((resolve, reject) => {
            console.log(`[MEDIA] Extraindo áudio de ${inputPath}...`);
            ffmpeg(inputPath)
                .output(outputPath)
                .noVideo()
                .audioCodec('libmp3lame')
                .on('end', () => {
                console.log(`[MEDIA] Sucesso! Salvo em: ${outputPath}`);
                resolve(outputPath);
            })
                .on('error', (err) => {
                console.error('[MEDIA] Falha ao extrair áudio:', err);
                reject(err);
            })
                .run();
        });
    }
    async cutVideo(inputPath, startTime, duration, outputFilename = `cut_${Date.now()}.mp4`) {
        const ffmpeg = loadFfmpeg();
        const outputPath = path.join(this.outputDir, outputFilename);
        return new Promise((resolve, reject) => {
            console.log(`[MEDIA] Cortando vídeo ${inputPath} (Start: ${startTime}, Dur: ${duration})...`);
            ffmpeg(inputPath)
                .setStartTime(startTime)
                .setDuration(duration)
                .output(outputPath)
                .videoCodec('copy')
                .audioCodec('copy')
                .on('end', () => {
                console.log(`[MEDIA] Corte finalizado! Salvo em: ${outputPath}`);
                resolve(outputPath);
            })
                .on('error', (err) => {
                console.error('[MEDIA] Erro ao cortar vídeo:', err);
                reject(err);
            })
                .run();
        });
    }
}
exports.MediaManager = MediaManager;
