import { l as d, R as g, W as p, U as u, x as m } from "./useVidstackSrc-CNo2sIR4.js";
import { l as C } from "./wt-vidstack-player-DL-84dHQ.js";
function h() {
  return "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1";
}
function f() {
  return !!window.cast?.framework;
}
function w() {
  return !!window.chrome?.cast?.isAvailable;
}
function c() {
  return s().getCastState() === cast.framework.CastState.CONNECTED;
}
function s() {
  return window.cast.framework.CastContext.getInstance();
}
function l() {
  return s().getCurrentSession();
}
function y() {
  return l()?.getSessionObj().media[0];
}
function _(r) {
  return y()?.media.contentId === r?.src;
}
function v() {
  return {
    language: "en-US",
    autoJoinPolicy: chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED,
    receiverApplicationId: chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID,
    resumeSavedSession: !0,
    androidReceiverCompatible: !0
  };
}
function E(r) {
  return `Google Cast Error Code: ${r}`;
}
function P(r, e) {
  return d(s(), r, e);
}
class S {
  name = "google-cast";
  target;
  #e;
  /**
   * @see {@link https://developers.google.com/cast/docs/reference/web_sender/cast.framework.CastContext}
   */
  get cast() {
    return s();
  }
  mediaType() {
    return "video";
  }
  canPlay(e) {
    return g && !u && p(e);
  }
  async prompt(e) {
    let t, a, o;
    try {
      t = await this.#r(e), this.#e || (this.#e = new cast.framework.RemotePlayer(), new cast.framework.RemotePlayerController(this.#e)), a = e.player.createEvent("google-cast-prompt-open", {
        trigger: t
      }), e.player.dispatchEvent(a), this.#t(e, "connecting", a), await this.#o(m(e.$props.googleCast)), e.$state.remotePlaybackInfo.set({
        deviceName: l()?.getCastDevice().friendlyName
      }), c() && this.#t(e, "connected", a);
    } catch (n) {
      const i = n instanceof Error ? n : this.#a(
        (n + "").toUpperCase(),
        "Prompt failed."
      );
      throw o = e.player.createEvent("google-cast-prompt-error", {
        detail: i,
        trigger: a ?? t,
        cancelable: !0
      }), e.player.dispatch(o), this.#t(
        e,
        c() ? "connected" : "disconnected",
        o
      ), i;
    } finally {
      e.player.dispatch("google-cast-prompt-close", {
        trigger: o ?? a ?? t
      });
    }
  }
  async load(e) {
    if (!this.#e)
      throw Error("[vidstack] google cast player was not initialized");
    return new (await import("./vidstack-google-cast-1ZEtw8UJ.js")).GoogleCastProvider(this.#e, e);
  }
  async #r(e) {
    if (f()) return;
    const t = e.player.createEvent("google-cast-load-start");
    e.player.dispatch(t), await C(h()), await customElements.whenDefined("google-cast-launcher");
    const a = e.player.createEvent("google-cast-loaded", { trigger: t });
    if (e.player.dispatch(a), !w())
      throw this.#a("CAST_NOT_AVAILABLE", "Google Cast not available on this platform.");
    return a;
  }
  async #o(e) {
    this.#s(e);
    const t = await this.cast.requestSession();
    if (t)
      throw this.#a(
        t.toUpperCase(),
        E(t)
      );
  }
  #s(e) {
    this.cast?.setOptions({
      ...v(),
      ...e
    });
  }
  #t(e, t, a) {
    const o = { type: "google-cast", state: t };
    e.notify("remote-playback-change", o, a);
  }
  #a(e, t) {
    const a = Error(t);
    return a.code = e, a;
  }
}
const b = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  GoogleCastLoader: S
}, Symbol.toStringTag, { value: "Module" }));
export {
  l as a,
  y as b,
  E as c,
  s as g,
  _ as h,
  P as l,
  b as v
};
