/*
En la venta de red se ve la transferencia https://2upra.com/wp-json/1/v1/2?token=MjgxMDk4fDE5MjQ5MDU2MDB8Y2FjaGVkfGYwNzY1MjU1NjIzZDk0YzI2MmYwMTU2NDY4MDY3OGFlfDk5OTk5OXwyNmFkN2Y5ZmRiNGNmMGE0NzkxNTc0Nzk2MjIxMDVjYjRjOTU2Y2Y3MzRhOGQ2Mzc3M2ZhOTdhMmFlYjU5YmRh&_wpnonce=982f26a68e y es completamente accesible y reproducible, yo no quiero eso y no se hacer para evitarlo sin dañar el hecho que quiero lo usuarios puedan cachear los audios en el navegador

por cierto estoy usando esto

// sw.js
const CACHE_NAME = 'audio-cache-v1';

self.addEventListener('install', event => {
    self.skipWaiting();
});

self.addEventListener('activate', event => {
    event.waitUntil(clients.claim());
});

// sw.js
self.addEventListener('fetch', event => {
    if (event.request.url.includes('/wp-json/1/v1/2')) {
        event.respondWith(
            caches.open(CACHE_NAME).then(async cache => {
                try {
                    const response = await fetch(event.request.clone(), {
                        credentials: 'same-origin',
                        headers: {
                            'X-Requested-With': 'XMLHttpRequest',
                            //'Accept': 'audio/mpeg',
                            //'Range': 'bytes=0-',
                            'X-WP-Nonce': event.request.headers.get('X-WP-Nonce')
                        }
                    });

                    if (!response.ok && response.status !== 206) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }

                    // No cachear respuestas parciales
                    if (response.status === 206) {
                        return response;
                    }

                    const clonedResponse = response.clone();
                    cache.put(event.request, clonedResponse);
                    return response;
                } catch (error) {
                    console.error('Fetch error:', error);
                    const cachedResponse = await cache.match(event.request);
                    if (cachedResponse) {
                        return cachedResponse;
                    }
                    throw error;
                }
            })
        );
    }
});
*/

window.we = function (postId, audioUrl, container, playOnLoad = false) {
    if (!window.wavesurfers) {
        window.wavesurfers = {};
    }

    const MAX_RETRIES = 3;
    //console.log(`Intentando cargar audio para postId=${postId}, URL=${audioUrl}`);

    const loadAndPlayAudioStream = (retryCount = 0) => {
        if (retryCount >= MAX_RETRIES) {
            //console.error(`No se pudo cargar el audio para postId=${postId} después de varios intentos`);
            container.querySelector('.waveform-loading').style.display = 'none';
            container.querySelector('.waveform-message').style.display = 'block';
            container.querySelector('.waveform-message').textContent = 'Error al cargar el audio.';
            return;
        }

        window.audioLoading = true;

        fetch(audioUrl, {
            method: 'GET',
            credentials: 'same-origin',
            headers: {
                'X-WP-Nonce': audioSettings.nonce,
                'X-Requested-With': 'XMLHttpRequest',
                //Accept: 'audio/mpeg,audio/*;q=0.9,*/*;q=0.8'
            }
        })
        .then(response => {
            if (!response.ok) throw new Error('Respuesta de red no satisfactoria');
            return response;
        })
        .then(response => {
            const reader = response.body.getReader();
            return new ReadableStream({
                start(controller) {
                    return pump();
                    function pump() {
                        return reader.read().then(({ done, value }) => {
                            if (done) {
                                controller.close();
                                return;
                            }
                            controller.enqueue(value);
                            return pump();
                        });
                    }
                }
            });
        })
        .then(stream => new Response(stream))
        .then(response => response.blob())
        .then(blob => {
            const audioBlobUrl = URL.createObjectURL(blob);

            // Inicializar wavesurfer y guardarlo en el objeto global
            const wavesurfer = initWavesurfer(container);
            window.wavesurfers[postId] = wavesurfer;

            wavesurfer.load(audioBlobUrl);

            const waveformBackground = container.querySelector('.waveform-background');
            if (waveformBackground) {
                waveformBackground.style.display = 'none';
            }