"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var twitch_exports = {};
__export(twitch_exports, {
  Twitch: () => Twitch
});
module.exports = __toCommonJS(twitch_exports);
var import_react = __toESM(require("react"));
var import_utils = require("../utils.js");
const SDK_URL = "https://player.twitch.tv/js/embed/v1.js";
const SDK_GLOBAL = "Twitch";
const MATCH_VIDEO_URL = /(?:www\.|go\.)?twitch\.tv\/videos\/(\d+)($|\?)/;
const MATCH_CHANNEL_URL = /(?:www\.|go\.)?twitch\.tv\/([a-z0-9_]+)($|\?)/;
const PLAYER_ID_PREFIX = "twitch-player-";
class Twitch extends import_react.Component {
  constructor() {
    super(...arguments);
    this.callPlayer = import_utils.callPlayer;
    this.playerID = this.props.config.twitch.playerId || `${PLAYER_ID_PREFIX}${(0, import_utils.randomString)()}`;
    this.mute = () => {
      this.callPlayer("setMuted", true);
    };
    this.unmute = () => {
      this.callPlayer("setMuted", false);
    };
  }
  load(url, isReady) {
    const { playsinline, onError, config, controls } = this.props;
    const isChannel = MATCH_CHANNEL_URL.test(url);
    const id = isChannel ? url.match(MATCH_CHANNEL_URL)[1] : url.match(MATCH_VIDEO_URL)[1];
    if (isReady) {
      if (isChannel) {
        this.player.setChannel(id);
      } else {
        this.player.setVideo("v" + id);
      }
      return;
    }
    (0, import_utils.getSDK)(SDK_URL, SDK_GLOBAL).then((Twitch2) => {
      this.player = new Twitch2.Player(this.playerID, {
        video: isChannel ? "" : id,
        channel: isChannel ? id : "",
        height: "100%",
        width: "100%",
        playsinline,
        autoplay: this.props.playing,
        muted: this.props.muted,
        // https://github.com/CookPete/react-player/issues/733#issuecomment-549085859
        controls: isChannel ? true : controls,
        ...config.twitch.options
      });
      const { READY, PLAYING, PAUSE, ENDED, ONLINE, OFFLINE } = Twitch2.Player;
      this.player.addEventListener(READY, this.props.onReady);
      this.player.addEventListener(PLAYING, this.props.onPlay);
      this.player.addEventListener(PAUSE, this.props.onPause);
      this.player.addEventListener(ENDED, this.props.onEnded);
      this.player.addEventListener(ONLINE, this.props.onLoaded);
      this.player.addEventListener(OFFLINE, this.props.onLoaded);
    }, onError);
  }
  play() {
    this.callPlayer("play");
  }
  pause() {
    this.callPlayer("pause");
  }
  stop() {
    this.callPlayer("pause");
  }
  seekTo(seconds) {
    this.callPlayer("seek", seconds);
  }
  setVolume(fraction) {
    this.callPlayer("setVolume", fraction);
  }
  getDuration() {
    return this.callPlayer("getDuration");
  }
  getCurrentTime() {
    return this.callPlayer("getCurrentTime");
  }
  getSecondsLoaded() {
    return null;
  }
  render() {
    const style = {
      width: "100%",
      height: "100%"
    };
    return /* @__PURE__ */ import_react.default.createElement("div", { style, id: this.playerID, loading: "lazy" });
  }
}
Twitch.displayName = "Twitch";
Twitch.canPlay = (url) => MATCH_VIDEO_URL.test(url) || MATCH_CHANNEL_URL.test(url);
Twitch.loopOnEnded = true;
//# sourceMappingURL=twitch.js.map
