"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBaseFileName = exports.hasValidExtension = exports.getFileExtension = exports.getFileType = exports.PULSE_CRON_EXTENSION_TYPES = void 0;
exports.parseAwsFolderLocationUrl = parseAwsFolderLocationUrl;
/**
 * Parse S3 folder URL for monitoring (virtual-hosted https or s3://).
 * Returns the full key prefix path, not only the last segment (so nested folders work).
 * Prefix has a trailing slash when non-empty, suitable for ListObjectsV2 Prefix.
 */
function parseAwsFolderLocationUrl(locationUrl) {
    const trimmed = locationUrl.trim().replace(/^["']|["']$/g, '');
    let bucketName;
    let pathSegments;
    if (trimmed.startsWith('s3://')) {
        const withoutScheme = trimmed.slice('s3://'.length);
        const parts = withoutScheme.split('/').filter((p) => p.length > 0);
        bucketName = parts[0] ?? '';
        pathSegments = parts.slice(1).join('/');
    }
    else if (trimmed.startsWith('https://') || trimmed.startsWith('http://')) {
        const url = new URL(trimmed);
        const hostParts = url.hostname.split('.');
        bucketName = hostParts[0] ?? '';
        pathSegments = url.pathname.replace(/^\//, '');
    }
    else {
        bucketName = trimmed.replace(/[/\\]/g, '').replace(/\s/g, '') || trimmed;
        pathSegments = '';
    }
    let keyPrefix = pathSegments.replace(/\/+/g, '/').replace(/\/$/, '');
    if (keyPrefix.length > 0) {
        keyPrefix += '/';
    }
    return { bucketName, prefix: keyPrefix };
}
exports.PULSE_CRON_EXTENSION_TYPES = {
    chat: ['.log', '.json', '.txt'],
    audio: ['.wav', '.mp3', '.ogg', '.flac', '.pcm16'],
    video: ['.mp4', '.mov', '.avi', '.mkv', '.webm', '.m4v', '.mpg', '.mpeg'],
    image: ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.svg', '.ico', '.webp', '.avif', '.heic', '.heif'],
};
// Get file type based on extension
const getFileType = (key) => {
    const lowerKey = key.toLowerCase();
    // Chat type files
    if (exports.PULSE_CRON_EXTENSION_TYPES.chat.some(ext => lowerKey.endsWith(ext))) {
        return 'chat';
    }
    // Audio type files
    if (exports.PULSE_CRON_EXTENSION_TYPES.audio.some(ext => lowerKey.endsWith(ext))) {
        return 'audio';
    }
    if (exports.PULSE_CRON_EXTENSION_TYPES.video.some(ext => lowerKey.endsWith(ext))) {
        return 'video';
    }
    if (exports.PULSE_CRON_EXTENSION_TYPES.image.some(ext => lowerKey.endsWith(ext))) {
        return 'image';
    }
    return 'unknown';
};
exports.getFileType = getFileType;
// Get file extension based on key
const getFileExtension = (key) => {
    const lastDotIndex = key.lastIndexOf('.');
    if (lastDotIndex === -1) {
        return '';
    }
    return key.substring(lastDotIndex).toLowerCase().replace('.', '');
};
exports.getFileExtension = getFileExtension;
// Check if file has valid extension
const hasValidExtension = (key, validExtensions) => {
    if (validExtensions && validExtensions.length > 0) {
        const lowerKey = key.toLowerCase();
        return validExtensions.some(ext => lowerKey.endsWith(ext.toLowerCase()));
    }
    return true; // If no extensions specified, accept all files
};
exports.hasValidExtension = hasValidExtension;
// Get base file name without extension
const getBaseFileName = (key) => {
    const lastDotIndex = key.lastIndexOf('.');
    if (lastDotIndex === -1) {
        return key;
    }
    return key.substring(0, lastDotIndex);
};
exports.getBaseFileName = getBaseFileName;
//# sourceMappingURL=pulseCronCommon.js.map