"use strict";

exports.__esModule = true;
exports.default = exports.TYPE_VIDEO = exports.TYPE_PDF = exports.TYPE_IMAGE = exports.TYPE_AUDIO = void 0;
var _get2 = _interopRequireDefault(require("lodash/fp/get"));
var _omit2 = _interopRequireDefault(require("lodash/fp/omit"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _novaIcons = require("@coorpacademy/nova-icons");
var _pdf = _interopRequireDefault(require("../pdf"));
var _audio = _interopRequireDefault(require("../audio"));
var _videoPlayer = _interopRequireDefault(require("../video-player"));
var _life = _interopRequireDefault(require("../../atom/life"));
var _provider = _interopRequireDefault(require("../../atom/provider"));
var _style = _interopRequireDefault(require("./style.css"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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); }
const TYPE_IMAGE = exports.TYPE_IMAGE = 'img';
const TYPE_PDF = exports.TYPE_PDF = 'pdf';
const TYPE_VIDEO = exports.TYPE_VIDEO = 'video';
const TYPE_AUDIO = exports.TYPE_AUDIO = 'audio';
const isType = process.env.NODE_ENV !== "production" ? name => _propTypes.default.oneOf([name]) : {};
const videoPropType = process.env.NODE_ENV !== "production" ? _propTypes.default.shape({
  type: isType(TYPE_VIDEO).isRequired,
  description: _propTypes.default.string.isRequired,
  ..._videoPlayer.default.propTypes
}) : {};
const pdfPropType = process.env.NODE_ENV !== "production" ? _propTypes.default.shape({
  type: isType(TYPE_PDF).isRequired,
  description: _propTypes.default.string.isRequired,
  mediaUrl: _propTypes.default.string.isRequired,
  ..._pdf.default.propTypes
}) : {};
const audioPropType = process.env.NODE_ENV !== "production" ? _propTypes.default.shape({
  type: isType(TYPE_AUDIO).isRequired,
  description: _propTypes.default.string.isRequired,
  mediaUrl: _propTypes.default.string.isRequired,
  ..._audio.default.propTypes
}) : {};
const imgPropType = process.env.NODE_ENV !== "production" ? _propTypes.default.shape({
  type: isType(TYPE_IMAGE).isRequired,
  url: _propTypes.default.string.isRequired
}) : {};
const ResourceElement = props => {
  const {
    resource,
    autoplay = false,
    disableAutostart
  } = props;
  const {
    type,
    videoId,
    ...childProps
  } = (0, _omit2.default)('id', resource);
  const {
    url
  } = childProps;
  switch (type) {
    case TYPE_IMAGE:
      return /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.img,
        style: {
          backgroundImage: `url(${url})`
        }
      });
    case TYPE_PDF:
      return /*#__PURE__*/_react.default.createElement(_pdf.default, childProps);
    case TYPE_AUDIO:
      return /*#__PURE__*/_react.default.createElement(_audio.default, childProps);
    case TYPE_VIDEO:
      return /*#__PURE__*/_react.default.createElement(_videoPlayer.default, _extends({}, (0, _omit2.default)('id', childProps), {
        autoplay: autoplay,
        disableAutostart: disableAutostart,
        id: videoId
      }));
  }
};
ResourceElement.propTypes = process.env.NODE_ENV !== "production" ? {
  resource: _propTypes.default.oneOfType([videoPropType, pdfPropType, imgPropType, audioPropType]),
  autoplay: _propTypes.default.bool,
  disableAutostart: _propTypes.default.bool
} : {};
const OverlayElement = (props = {}, context = undefined) => {
  const {
    title,
    text,
    lifeAmount,
    onClick
  } = props;
  const {
    skin
  } = context;
  const dark = (0, _get2.default)('common.dark', skin);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.overlay,
    onClick: onClick,
    "data-name": "overlay"
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.icons
  }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaSolidVideosVideoControlPlay, {
    className: _style.default.playButton,
    style: {
      color: dark
    }
  }), /*#__PURE__*/_react.default.createElement(_life.default, {
    count: lifeAmount,
    operator: "+",
    bounce: {
      type: 'bounceTwice',
      delay: '2.5s'
    },
    heartOnRight: true
  })), /*#__PURE__*/_react.default.createElement("p", {
    className: _style.default.overlayTitle
  }, title), /*#__PURE__*/_react.default.createElement("p", {
    className: _style.default.overlayText
  }, text));
};
OverlayElement.contextTypes = {
  skin: _provider.default.childContextTypes.skin
};
OverlayElement.propTypes = process.env.NODE_ENV !== "production" ? {
  lifeAmount: _propTypes.default.number,
  onClick: _propTypes.default.func,
  text: _propTypes.default.string,
  title: _propTypes.default.string
} : {};
class ResourcePlayer extends _react.default.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.default.createElement(OverlayElement, _extends({}, stateOverlay, {
      onClick: this.handleOverlay
    })) : null;
    return /*#__PURE__*/_react.default.createElement("div", {
      "data-name": type,
      className: customClassName
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: {
        [TYPE_IMAGE]: _style.default.image,
        [TYPE_PDF]: _style.default.pdf,
        [TYPE_AUDIO]: _style.default.audio,
        [TYPE_VIDEO]: _style.default.video
      }[type]
    }, overlayView, /*#__PURE__*/_react.default.createElement(ResourceElement, _extends({}, this.props, {
      disableAutostart: !!propsOverlay,
      autoplay: autoplay
    }))));
  }
}
ResourcePlayer.propTypes = process.env.NODE_ENV !== "production" ? {
  className: _propTypes.default.string,
  resource: _propTypes.default.oneOfType([videoPropType, pdfPropType, imgPropType, audioPropType]),
  overlay: _propTypes.default.shape({
    title: _propTypes.default.string,
    text: _propTypes.default.string,
    lifeAmount: _propTypes.default.number
  })
} : {};
var _default = exports.default = ResourcePlayer;
//# sourceMappingURL=index.js.map