import _get from "lodash/fp/get";
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { NovaCompositionCoorpacademyMicrophone as PodcastIcon } from '@coorpacademy/nova-icons';
import Provider from '../../atom/provider';
import style from './style.css';
class Audio extends React.Component {
  static contextTypes = {
    skin: Provider.childContextTypes.skin
  };
  constructor(props, context) {
    super(props, context);
    this.handlePlay = this.handlePlay.bind(this);
    this.audioElement = null;
    this.setAudio = this.setAudio.bind(this);
  }
  shouldComponentUpdate(nextProps) {
    const {
      mediaUrl
    } = this.props;
    return nextProps.mediaUrl !== mediaUrl;
  }
  componentDidUpdate() {
    this.audioElement.load();
  }
  setAudio = element => {
    this.audioElement = element;
  };
  handlePlay(e) {
    const {
      onPlay
    } = this.props;
    if (onPlay) onPlay(e);
  }
  render() {
    const {
      description,
      mediaUrl,
      poster
    } = this.props;
    const {
      skin
    } = this.context;
    const white = _get('common.white', skin);
    return /*#__PURE__*/React.createElement("div", {
      className: style.frame,
      style: {
        backgroundImage: `url(${poster})`
      }
    }, /*#__PURE__*/React.createElement(PodcastIcon, {
      style: {
        color: white
      },
      className: style.icon
    }), description ? /*#__PURE__*/React.createElement("div", {
      className: classnames(style.description, style.innerHTML),
      "data-name": "audioDescription"
      // eslint-disable-next-line react/no-danger
      ,
      dangerouslySetInnerHTML: {
        __html: description
      }
    }) : null, /*#__PURE__*/React.createElement("audio", {
      className: style.audio,
      controls: true,
      controlsList: "nodownload",
      ref: this.setAudio,
      autoPlay: "",
      onPlay: this.handlePlay,
      name: "media",
      "data-testid": "audio",
      "data-name": "audio",
      preload: "auto"
    }, /*#__PURE__*/React.createElement("source", {
      src: mediaUrl
    })));
  }
}
Audio.propTypes = process.env.NODE_ENV !== "production" ? {
  description: PropTypes.string,
  mediaUrl: PropTypes.string,
  poster: PropTypes.string,
  onPlay: PropTypes.func
} : {};
export default Audio;
//# sourceMappingURL=index.js.map