"use strict";

exports.__esModule = true;
exports.default = void 0;
var _keys2 = _interopRequireDefault(require("lodash/fp/keys"));
var _isFunction2 = _interopRequireDefault(require("lodash/fp/isFunction"));
var _includes2 = _interopRequireDefault(require("lodash/fp/includes"));
var _get2 = _interopRequireDefault(require("lodash/fp/get"));
var _react = _interopRequireDefault(require("react"));
var _jwplayerReact = _interopRequireDefault(require("@jwplayer/jwplayer-react"));
var _jwplayer = _interopRequireDefault(require("./jwplayer.css"));
var _propTypes = require("./prop-types");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const fileAndTracks = ({
  file,
  customProps,
  playlist
} = {}) => {
  if (file) return {
    file,
    tracks: customProps ? customProps.tracks : null
  };
  if (playlist && playlist[0]) return playlist[0];
  return {
    file: null,
    tracks: null
  };
};
class JWPlayer extends _react.default.Component {
  constructor(props, context) {
    super(props, context);
    this.state = {
      scriptFailedLoading: false
    };
    this.handlePlay = this.handlePlay.bind(this);
    this.handleResume = this.handleResume.bind(this);
    this.handlePause = this.handlePause.bind(this);
    this.handleEnded = this.handleEnded.bind(this);
    this.handleError = this.handleError.bind(this);
    this.handleSetupError = this.handleSetupError.bind(this);
    this.handleScriptError = this.handleScriptError.bind(this);
    this.handlePlayAttemptFailed = this.handlePlayAttemptFailed.bind(this);
    this.handleAutostartNotAllowed = this.handleAutostartNotAllowed.bind(this);
    this.handleWarning = this.handleWarning.bind(this);
  }
  static getDerivedStateFromProps(props) {
    return fileAndTracks(props.jwpOptions);
  }
  componentDidUpdate(prevProps) {
    const changes = (0, _keys2.default)(this.props).filter(key => {
      const {
        [key]: current
      } = this.props;
      const {
        [key]: prev
      } = prevProps;
      return current !== prev;
    });
    const shouldStart = (0, _includes2.default)('autoplay', changes);
    if (shouldStart) {
      if ((0, _isFunction2.default)(window.jwplayer)) {
        window.jwplayer(prevProps.jwpOptions.playerId).play();
      }
    }
  }
  handlePlay(e) {
    const {
      onPlay
    } = this.props;
    if (onPlay) onPlay(e);
  }
  handleResume(e) {
    const {
      onResume
    } = this.props;
    if (onResume) onResume(e);
  }
  handlePause(e) {
    const {
      onPause
    } = this.props;
    if (onPause) onPause(e);
  }
  handleEnded(e) {
    const {
      onEnded
    } = this.props;
    if (onEnded) onEnded(e);
  }
  handleScriptError(script) {
    this.setState({
      scriptFailedLoading: true
    });
    if (script) {
      script.parentNode.removeChild();
    }
  }
  handleSetupError(error) {
    const {
      onSetupError
    } = this.props;
    if (onSetupError) onSetupError(error);
  }
  handlePlayAttemptFailed(error) {
    const {
      onPlayAttemptFailed
    } = this.props;
    if (onPlayAttemptFailed) onPlayAttemptFailed(error);
  }
  handleAutostartNotAllowed(error) {
    const {
      onAutostartNotAllowed
    } = this.props;
    if (onAutostartNotAllowed) onAutostartNotAllowed(error);
  }
  handleWarning(error) {
    const {
      onWarning
    } = this.props;
    if (onWarning) onWarning(error);
  }
  handleError(error) {
    const {
      onError,
      mimeType,
      jwpOptions
    } = this.props;
    const {
      code
    } = error;
    onError && onError(error);
    if (mimeType === 'application/kontiki') {
      return;
    }

    // This error mostly appears on IE11 on W7
    // Since IE11 dont kinda support M3U8 sometimes,
    // We've decided to switch from M3U8 to mp4 whenever it appears
    if (code === 214000) {
      const {
        file: videoUrl
      } = jwpOptions;
      const regex = /^https:\/\/content.jwplatform\.com\/manifests\/(\w+).m3u8/;
      const matched = videoUrl.match(regex);
      if (matched) {
        const videoId = matched[1];
        return this.setState({
          file: `https://content.jwplatform.com/videos/${videoId}-1080.mp4`
        });
      }
    }
  }
  render() {
    const {
      jwpOptions,
      disableAutostart,
      scriptErrorMessage
    } = this.props;
    const {
      scriptFailedLoading,
      file,
      tracks
    } = this.state;
    const _jwpOptions = {
      ...jwpOptions,
      customProps: {
        ...(0, _get2.default)('customProps', jwpOptions),
        key: jwpOptions.licenseKey,
        autostart: !disableAutostart && (0, _get2.default)('customProps.autostart', jwpOptions)
      }
    };
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, scriptFailedLoading ? /*#__PURE__*/_react.default.createElement("p", {
      className: _jwplayer.default.errorMessage
    }, scriptErrorMessage, " ") : null, /*#__PURE__*/_react.default.createElement(_jwplayerReact.default, {
      key: file,
      className: _jwplayer.default.wrapper,
      id: _jwpOptions.playerId,
      file: file,
      tracks: tracks,
      library: _jwpOptions.playerScript,
      config: _jwpOptions.customProps,
      onAutoStart: this.handlePlay,
      onPlay: this.handlePlay,
      onResume: this.handleResume,
      onPause: this.handlePause,
      onOneHundredPercent: this.handleEnded,
      onError: this.handleError,
      onSetupError: this.handleSetupError,
      onPlayAttemptFailed: this.handlePlayAttemptFailed,
      onWarning: this.handleWarning,
      onAutostartNotAllowed: this.handleAutostartNotAllowed
    }));
  }
}
JWPlayer.propTypes = process.env.NODE_ENV !== "production" ? _propTypes.JWPlayerPropTypes : {};
var _default = exports.default = JWPlayer;
//# sourceMappingURL=jwplayer.js.map