import React, { Component } from "react";
import { callPlayer, getSDK, randomString } from "../utils.js";
const SDK_URL = "https://connect.facebook.net/en_US/sdk.js";
const SDK_GLOBAL = "FB";
const SDK_GLOBAL_READY = "fbAsyncInit";
const MATCH_URL = /^https?:\/\/(www\.)?facebook\.com.*\/(video(s)?|watch|story)(\.php?|\/).+$/;
const PLAYER_ID_PREFIX = "facebook-player-";
class Facebook extends Component {
  constructor() {
    super(...arguments);
    this.callPlayer = callPlayer;
    this.playerID = this.props.config.facebook.playerId || `${PLAYER_ID_PREFIX}${randomString()}`;
    this.mute = () => {
      this.callPlayer("mute");
    };
    this.unmute = () => {
      this.callPlayer("unmute");
    };
  }
  load(url, isReady) {
    if (isReady) {
      getSDK(SDK_URL, SDK_GLOBAL, SDK_GLOBAL_READY).then(
        (FB) => FB.XFBML.parse()
      );
      return;
    }
    getSDK(SDK_URL, SDK_GLOBAL, SDK_GLOBAL_READY).then((FB) => {
      FB.init({
        appId: this.props.config.facebook.appId,
        xfbml: true,
        version: this.props.config.facebook.version
      });
      FB.Event.subscribe("xfbml.render", (_msg) => {
        this.props.onLoaded();
      });
      FB.Event.subscribe("xfbml.ready", (msg) => {
        if (msg.type === "video" && msg.id === this.playerID) {
          this.player = msg.instance;
          this.player.subscribe("startedPlaying", this.props.onPlay);
          this.player.subscribe("paused", this.props.onPause);
          this.player.subscribe("finishedPlaying", this.props.onEnded);
          this.player.subscribe("startedBuffering", this.props.onBuffer);
          this.player.subscribe("finishedBuffering", this.props.onBufferEnd);
          this.player.subscribe("error", this.props.onError);
          if (!this.props.muted) {
            this.callPlayer("unmute");
          }
          this.props.onReady();
          document.getElementById(this.playerID).querySelector("iframe").style.visibility = "visible";
        }
      });
    });
  }
  play() {
    this.callPlayer("play");
  }
  pause() {
    this.callPlayer("pause");
  }
  stop() {
  }
  seekTo(seconds) {
    this.callPlayer("seek", seconds);
  }
  setVolume(fraction) {
    this.callPlayer("setVolume", fraction);
  }
  getDuration() {
    return this.callPlayer("getDuration");
  }
  getCurrentTime() {
    return this.callPlayer("getCurrentPosition");
  }
  getSecondsLoaded() {
    return null;
  }
  render() {
    const style = {
      width: "100%",
      height: "100%"
    };
    return /* @__PURE__ */ React.createElement(
      "div",
      {
        style,
        id: this.playerID,
        className: "fb-video",
        "data-href": this.props.url,
        "data-autoplay": this.props.playing ? "true" : "false",
        "data-allowfullscreen": "true",
        "data-controls": this.props.controls ? "true" : "false",
        "data-width": "16000px",
        "data-height": "9000px"
      }
    );
  }
}
Facebook.displayName = "Facebook";
Facebook.canPlay = (url) => MATCH_URL.test(url);
Facebook.loopOnEnded = true;
export {
  Facebook
};
//# sourceMappingURL=facebook.js.map
