import WaveSurfer from 'wavesurfer.js';

export const getAudioMetadata = (audioUrl: string) => new Promise<Record<string, string | number>>(async (resolve, reject) => {
  try {
    const dummyContainer = window.document.createElement('div');
    const waveSurferInstance = WaveSurfer.create({
      container: dummyContainer,
    });

    waveSurferInstance.on('ready', async () => {
      const metadata = {
        duration: waveSurferInstance.getDuration(),
      };

      waveSurferInstance.destroy();
      dummyContainer.remove();
      resolve(metadata);
    });

    await waveSurferInstance.load(audioUrl);
  } catch (err) {
    reject(err);
  }
});
