import _get from "lodash/fp/get";
import _omit from "lodash/fp/omit";
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
import React from 'react';
import PropTypes from 'prop-types';
import { NovaSolidVideosVideoControlPlay as Play } from '@coorpacademy/nova-icons';
import Pdf from '../pdf';
import Audio from '../audio';
import VideoPlayer from '../video-player';
import Life from '../../atom/life';
import Provider from '../../atom/provider';
import style from './style.css';
export const TYPE_IMAGE = 'img';
export const TYPE_PDF = 'pdf';
export const TYPE_VIDEO = 'video';
export const TYPE_AUDIO = 'audio';
const isType = process.env.NODE_ENV !== "production" ? name => PropTypes.oneOf([name]) : {};
const videoPropType = process.env.NODE_ENV !== "production" ? PropTypes.shape({
  type: isType(TYPE_VIDEO).isRequired,
  description: PropTypes.string.isRequired,
  ...VideoPlayer.propTypes
}) : {};
const pdfPropType = process.env.NODE_ENV !== "production" ? PropTypes.shape({
  type: isType(TYPE_PDF).isRequired,
  description: PropTypes.string.isRequired,
  mediaUrl: PropTypes.string.isRequired,
  ...Pdf.propTypes
}) : {};
const audioPropType = process.env.NODE_ENV !== "production" ? PropTypes.shape({
  type: isType(TYPE_AUDIO).isRequired,
  description: PropTypes.string.isRequired,
  mediaUrl: PropTypes.string.isRequired,
  ...Audio.propTypes
}) : {};
const imgPropType = process.env.NODE_ENV !== "production" ? PropTypes.shape({
  type: isType(TYPE_IMAGE).isRequired,
  url: PropTypes.string.isRequired
}) : {};
const ResourceElement = props => {
  const {
    resource,
    autoplay = false,
    disableAutostart
  } = props;
  const {
    type,
    videoId,
    ...childProps
  } = _omit('id', resource);
  const {
    url
  } = childProps;
  switch (type) {
    case TYPE_IMAGE:
      return /*#__PURE__*/React.createElement("div", {
        className: style.img,
        style: {
          backgroundImage: `url(${url})`
        }
      });
    case TYPE_PDF:
      return /*#__PURE__*/React.createElement(Pdf, childProps);
    case TYPE_AUDIO:
      return /*#__PURE__*/React.createElement(Audio, childProps);
    case TYPE_VIDEO:
      return /*#__PURE__*/React.createElement(VideoPlayer, _extends({}, _omit('id', childProps), {
        autoplay: autoplay,
        disableAutostart: disableAutostart,
        id: videoId
      }));
  }
};
ResourceElement.propTypes = process.env.NODE_ENV !== "production" ? {
  resource: PropTypes.oneOfType([videoPropType, pdfPropType, imgPropType, audioPropType]),
  autoplay: PropTypes.bool,
  disableAutostart: PropTypes.bool
} : {};
const OverlayElement = (props = {}, context = undefined) => {
  const {
    title,
    text,
    lifeAmount,
    onClick
  } = props;
  const {
    skin
  } = context;
  const dark = _get('common.dark', skin);
  return /*#__PURE__*/React.createElement("div", {
    className: style.overlay,
    onClick: onClick,
    "data-name": "overlay"
  }, /*#__PURE__*/React.createElement("div", {
    className: style.icons
  }, /*#__PURE__*/React.createElement(Play, {
    className: style.playButton,
    style: {
      color: dark
    }
  }), /*#__PURE__*/React.createElement(Life, {
    count: lifeAmount,
    operator: "+",
    bounce: {
      type: 'bounceTwice',
      delay: '2.5s'
    },
    heartOnRight: true
  })), /*#__PURE__*/React.createElement("p", {
    className: style.overlayTitle
  }, title), /*#__PURE__*/React.createElement("p", {
    className: style.overlayText
  }, text));
};
OverlayElement.contextTypes = {
  skin: Provider.childContextTypes.skin
};
OverlayElement.propTypes = process.env.NODE_ENV !== "production" ? {
  lifeAmount: PropTypes.number,
  onClick: PropTypes.func,
  text: PropTypes.string,
  title: PropTypes.string
} : {};
class ResourcePlayer extends React.Component {
  constructor(props, context) {
    super(props);
    this.state = {
      autoplay: false,
      overlay: props.overlay
    };
    this.handleOverlay = this.handleOverlay.bind(this);
  }
  handleOverlay() {
    this.setState({
      autoplay: true,
      overlay: null
    });
  }
  render() {
    const {
      className: customClassName,
      resource,
      overlay: propsOverlay
    } = this.props;
    const {
      overlay: stateOverlay,
      autoplay
    } = this.state;
    const {
      type
    } = resource;
    const overlayView = stateOverlay ? /*#__PURE__*/React.createElement(OverlayElement, _extends({}, stateOverlay, {
      onClick: this.handleOverlay
    })) : null;
    return /*#__PURE__*/React.createElement("div", {
      "data-name": type,
      className: customClassName
    }, /*#__PURE__*/React.createElement("div", {
      className: {
        [TYPE_IMAGE]: style.image,
        [TYPE_PDF]: style.pdf,
        [TYPE_AUDIO]: style.audio,
        [TYPE_VIDEO]: style.video
      }[type]
    }, overlayView, /*#__PURE__*/React.createElement(ResourceElement, _extends({}, this.props, {
      disableAutostart: !!propsOverlay,
      autoplay: autoplay
    }))));
  }
}
ResourcePlayer.propTypes = process.env.NODE_ENV !== "production" ? {
  className: PropTypes.string,
  resource: PropTypes.oneOfType([videoPropType, pdfPropType, imgPropType, audioPropType]),
  overlay: PropTypes.shape({
    title: PropTypes.string,
    text: PropTypes.string,
    lifeAmount: PropTypes.number
  })
} : {};
export default ResourcePlayer;
//# sourceMappingURL=index.js.map