"use strict";

exports.__esModule = true;
exports.default = void 0;
var _keys2 = _interopRequireDefault(require("lodash/fp/keys"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _provider = _interopRequireDefault(require("../../atom/provider"));
var _loader = _interopRequireDefault(require("../../atom/loader"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*
  from https://github.com/u-wave/react-vimeo
  adapted to use Vimeo.js from context and support width: 100%
*/

const eventNames = {
  ready: 'onReady',
  play: 'onPlay',
  pause: 'onPause',
  ended: 'onEnded',
  timeupdate: 'onTimeUpdate',
  progress: 'onProgress',
  seeked: 'onSeeked',
  texttrackchange: 'onTextTrackChange',
  cuechange: 'onCueChange',
  cuepoint: 'onCuePoint',
  volumechange: 'onVolumeChange',
  error: 'onError',
  loaded: 'onLoaded'
};
class Vimeo extends _react.default.Component {
  static contextTypes = {
    Vimeo: _provider.default.childContextTypes.Vimeo
  };
  static defaultProps = {
    autopause: true,
    autoplay: false,
    loop: false,
    showByline: true,
    showPortrait: true,
    showTitle: true
  };
  constructor(props, context) {
    super(props, context);
    this.refContainer = this.refContainer.bind(this);
    this.state = {
      isLoading: true
    };
  }
  componentDidMount() {
    this.createPlayer();
  }
  componentDidUpdate(prevProps) {
    const changes = (0, _keys2.default)(this.props).filter(key => {
      const {
        [key]: current
      } = this.props;
      const {
        [key]: prev
      } = prevProps;
      return current !== prev;
    });
    this.updateProps(changes);
  }
  getInitialOptions() {
    const {
      id,
      width,
      height,
      autopause,
      autoplay,
      showByline,
      color,
      loop,
      showPortrait,
      showTitle
    } = this.props;
    return {
      id,
      width,
      height,
      autopause,
      autoplay,
      byline: showByline,
      color,
      loop,
      portrait: showPortrait,
      title: showTitle
    };
  }
  updateProps(propNames) {
    const player = this.player;
    propNames.forEach(key => {
      const {
        [key]: value
      } = this.props;
      switch (key) {
        case 'width':
        case 'height':
          player.element[key] = value;
          break;
        case 'id':
          if (value) {
            player.loadVideo(value);
          } else {
            player.unload();
          }
          break;
        case 'autoplay':
          player.play();
          break;
      }
    });
  }
  createPlayer() {
    const options = this.getInitialOptions();
    const {
      Vimeo: V
    } = this.context;
    if (!V) return null;
    const {
      Player
    } = V;
    this.player = new Player(this.container, {
      ...options,
      width: '1',
      height: '1'
    });
    this.player.on('loaded', () => {
      this.updateProps(['width', 'height']);
      this.setState(state => ({
        isLoading: false
      }));
    });
    (0, _keys2.default)(eventNames).forEach(dmName => {
      const {
        [dmName]: reactName
      } = eventNames;
      this.player.on(dmName, e => {
        const {
          [reactName]: handler
        } = this.props;
        if (handler) {
          handler(e);
        }
      });
    });
  }
  refContainer(container) {
    this.container = container;
  }
  render() {
    const {
      className,
      height,
      width
    } = this.props;
    const {
      isLoading
    } = this.state;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: className,
      ref: this.refContainer,
      style: {
        height,
        width
      }
    }, isLoading ? /*#__PURE__*/_react.default.createElement(_loader.default, null) : null);
  }
}
Vimeo.propTypes = process.env.NODE_ENV !== "production" ? {
  /**
   * A Vimeo ID or URL.
   */
  id: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
  /**
   * CSS className for the player element.
   */
  className: _propTypes.default.string,
  /**
   * Width of the player element.
   */
  width: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
  /**
   * Height of the player element.
   */
  height: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
  /**
   * Pause the video.
   */
  paused: _propTypes.default.bool,
  // Player parameters
  /**
   * Pause this video automatically when another one plays.
   */
  autopause: _propTypes.default.bool,
  /**
   * Automatically start playback of the video. Note that this won’t work on
   * some devices.
   */
  autoplay: _propTypes.default.bool,
  /**
   * Show the byline on the video.
   */
  showByline: _propTypes.default.bool,
  /**
   * Specify the color of the video controls. Colors may be overridden by the
   * embed settings of the video.
   */
  color: _propTypes.default.string,
  /**
   * Play the video again when it reaches the end.
   */
  loop: _propTypes.default.bool,
  /**
   * Show the portrait on the video.
   */
  showPortrait: _propTypes.default.bool,
  /**
   * Show the title on the video.
   */
  showTitle: _propTypes.default.bool,
  // Events

  /**
   * Sent when the Vimeo player API has loaded.
   */
  onReady: _propTypes.default.func,
  /**
   * Sent when the player triggers an error.
   */
  onError: _propTypes.default.func,
  /**
   * Triggered when the video plays.
   */
  onPlay: _propTypes.default.func,
  /**
   * Triggered when the video pauses.
   */
  onPause: _propTypes.default.func,
  /**
   * Triggered any time the video playback reaches the end.
   * Note: when `loop` is turned on, the ended event will not fire.
   */
  onEnd: _propTypes.default.func,
  /**
   * Triggered as the `currentTime` of the video updates. It generally fires
   * every 250ms, but it may vary depending on the browser.
   */
  onTimeUpdate: _propTypes.default.func,
  /**
   * Triggered as the video is loaded. Reports back the amount of the video
   * that has been buffered.
   */
  onProgress: _propTypes.default.func,
  /**
   * Triggered when the player seeks to a specific time. An `onTimeUpdate`
   * event will also be fired at the same time.
   */
  onSeeked: _propTypes.default.func,
  /**
   * Triggered when the active text track (captions/subtitles) changes. The
   * values will be `null` if text tracks are turned off.
   */
  onTextTrackChange: _propTypes.default.func,
  /**
   * Triggered when the active cue for the current text track changes. It also
   * fires when the active text track changes. There may be multiple cues
   * active.
   */
  onCueChange: _propTypes.default.func,
  /**
   * Triggered when the current time hits a registered cue point.
   */
  onCuePoint: _propTypes.default.func,
  /**
   * Triggered when the volume in the player changes. Some devices do not
   * support setting the volume of the video independently from the system
   * volume, so this event will never fire on those devices.
   */
  onVolumeChange: _propTypes.default.func,
  /**
   * Triggered when a new video is loaded in the player.
   */
  onLoaded: _propTypes.default.func
} : {};
var _default = exports.default = Vimeo;
//# sourceMappingURL=vimeo.js.map