/**
 * AudioPlayerElement Component
 *
 * Audio player element with controls.
 * Renders with unified wrapper styling + content.
 */

import React from 'react';
import type { CSSProperties } from 'react';
import type { CanvasElement } from '../../../types/constructor';
import { resolveAssetPlaybackUrl } from '../../../lib/assetUrl';

interface AudioPlayerElementProps {
  element: CanvasElement;
  resolveUrl?: (url: string | undefined) => string;
  className: string;
  style: CSSProperties;
}

const AudioPlayerElement: React.FC<AudioPlayerElementProps> = ({
  element,
  resolveUrl,
  className,
  style,
}) => {
  const resolve = resolveUrl ?? resolveAssetPlaybackUrl;

  if (!element.mediaUrl) {
    return (
      <div className={className} style={style}>
        <span className='px-4 py-2 text-sm'>Audio Player</span>
      </div>
    );
  }

  return (
    <div className={className} style={style}>
      <audio
        className='w-full'
        src={resolve(element.mediaUrl)}
        controls
        autoPlay={Boolean(element.mediaAutoplay)}
        loop={Boolean(element.mediaLoop)}
      />
    </div>
  );
};

export default AudioPlayerElement;
