import { ab as y, v as u, m as c, x as l, d as b, f as d, l as v, i as k } from "./useVidstackSrc-CNo2sIR4.js";
import { e as o, p as m, L as r, T as w, Q as f } from "./wt-vidstack-player-DL-84dHQ.js";
import { R as P } from "./vidstack-DqAw8m9J-kmvNQesV.js";
import { E as T } from "./vidstack-DDXt6fpN-cezVOaCC.js";
import { resolveVimeoVideoId as C, getVimeoVideoInfo as g } from "./vidstack-krOAtKMi-C50BTxmn.js";
const $ = [
  "bufferend",
  "bufferstart",
  // 'cuechange',
  "durationchange",
  "ended",
  "enterpictureinpicture",
  "error",
  "fullscreenchange",
  "leavepictureinpicture",
  "loaded",
  // 'loadeddata',
  // 'loadedmetadata',
  // 'loadstart',
  "playProgress",
  "loadProgress",
  "pause",
  "play",
  "playbackratechange",
  // 'progress',
  "qualitychange",
  "seeked",
  "seeking",
  // 'texttrackchange',
  "timeupdate",
  "volumechange",
  "waiting"
  // 'adstarted',
  // 'adcompleted',
  // 'aderror',
  // 'adskipped',
  // 'adallcompleted',
  // 'adclicked',
  // 'chapterchange',
  // 'chromecastconnected',
  // 'remoteplaybackavailabilitychange',
  // 'remoteplaybackconnecting',
  // 'remoteplaybackconnect',
  // 'remoteplaybackdisconnect',
  // 'liveeventended',
  // 'liveeventstarted',
  // 'livestreamoffline',
  // 'livestreamonline',
];
class M extends T {
  $$PROVIDER_TYPE = "VIMEO";
  scope = y();
  fullscreen;
  #e;
  #s = u("");
  #i = u(!1);
  #o = null;
  #c = null;
  #d = !1;
  #n = new o(0, 0);
  #l = new P(this.#g.bind(this));
  #w = null;
  #r = null;
  #a = /* @__PURE__ */ new Map();
  #u = null;
  constructor(e, t) {
    super(e), this.#e = t;
    const s = this;
    this.fullscreen = {
      get active() {
        return s.#d;
      },
      supported: !0,
      enter: () => this.#t("requestFullscreen"),
      exit: () => this.#t("exitFullscreen")
    };
  }
  /**
   * Whether tracking session data should be enabled on the embed, including cookies and analytics.
   * This is turned off by default to be GDPR-compliant.
   *
   * @defaultValue `false`
   */
  cookies = !1;
  title = !0;
  byline = !0;
  portrait = !0;
  color = "00ADEF";
  get type() {
    return "vimeo";
  }
  get currentSrc() {
    return this.#c;
  }
  get videoId() {
    return this.#s();
  }
  get hash() {
    return this.#o;
  }
  get isPro() {
    return this.#i();
  }
  preconnect() {
    m(this.getOrigin());
  }
  setup() {
    super.setup(), c(this.#P.bind(this)), c(this.#T.bind(this)), c(this.#C.bind(this)), this.#e.notify("provider-setup", this);
  }
  destroy() {
    this.#k(), this.fullscreen = void 0;
    const e = "provider destroyed";
    for (const t of this.#a.values())
      for (const { reject: s } of t) s(e);
    this.#a.clear(), this.#t("destroy");
  }
  async play() {
    return this.#t("play");
  }
  async pause() {
    return this.#t("pause");
  }
  setMuted(e) {
    this.#t("setMuted", e);
  }
  setCurrentTime(e) {
    this.#t("seekTo", e), this.#e.notify("seeking", e);
  }
  setVolume(e) {
    this.#t("setVolume", e), this.#t("setMuted", l(this.#e.$state.muted));
  }
  setPlaybackRate(e) {
    this.#t("setPlaybackRate", e);
  }
  async loadSource(e) {
    if (!b(e.src)) {
      this.#c = null, this.#o = null, this.#s.set("");
      return;
    }
    const { videoId: t, hash: s } = C(e.src);
    this.#s.set(t ?? ""), this.#o = s ?? null, this.#c = e;
  }
  #P() {
    this.#k();
    const e = this.#s();
    if (!e) {
      this.src.set("");
      return;
    }
    this.src.set(`${this.getOrigin()}/video/${e}`), this.#e.notify("load-start");
  }
  #T() {
    const e = this.#s();
    if (!e) return;
    const t = d(), s = new AbortController();
    return this.#u = t, g(e, s, this.#o).then((i) => {
      t.resolve(i);
    }).catch((i) => {
      t.reject();
    }), () => {
      t.reject(), s.abort();
    };
  }
  #C() {
    const e = this.#i(), { $state: t, qualities: s } = this.#e;
    if (t.canSetPlaybackRate.set(e), s[r.setReadonly](!e), e)
      return v(s, "change", () => {
        if (s.auto) return;
        const i = s.selected?.id;
        i && this.#t("setQuality", i);
      });
  }
  getOrigin() {
    return "https://player.vimeo.com";
  }
  buildParams() {
    const { keyDisabled: e } = this.#e.$props, { playsInline: t, nativeControls: s } = this.#e.$state, i = s();
    return {
      title: this.title,
      byline: this.byline,
      color: this.color,
      portrait: this.portrait,
      controls: i,
      h: this.hash,
      keyboard: i && !e(),
      transparent: !0,
      playsinline: t(),
      dnt: !this.cookies
    };
  }
  #g() {
    this.#t("getCurrentTime");
  }
  // Embed will sometimes dispatch 0 at end of playback.
  #h = !1;
  #$(e, t) {
    if (this.#h && e === 0) return;
    const { realCurrentTime: s, paused: i, bufferedEnd: n, seekableEnd: a, live: h } = this.#e.$state;
    if (s() === e) return;
    const p = s();
    this.#e.notify("time-change", e, t), Math.abs(p - e) > 1.5 && (this.#e.notify("seeking", e, t), !i() && n() < e && this.#e.notify("waiting", void 0, t)), !h() && a() - e < 0.01 && (this.#e.notify("end", void 0, t), this.#h = !0, setTimeout(() => {
      this.#h = !1;
    }, 500));
  }
  #E(e, t) {
    this.#e.notify("seeked", e, t);
  }
  #V(e) {
    const t = this.#s();
    this.#u?.promise.then((s) => {
      if (!s) return;
      const { title: i, poster: n, duration: a, pro: h } = s;
      this.#i.set(h), this.#e.notify("title-change", i, e), this.#e.notify("poster-change", n, e), this.#e.notify("duration-change", a, e), this.#f(a, e);
    }).catch(() => {
      t === this.#s() && (this.#t("getVideoTitle"), this.#t("getDuration"));
    });
  }
  #f(e, t) {
    const { nativeControls: s } = this.#e.$state, i = s();
    this.#n = new o(0, e);
    const n = {
      buffered: new o(0, 0),
      seekable: this.#n,
      duration: e
    };
    this.#e.delegate.ready(n, t), i || this.#t("_hideOverlay"), this.#t("getQualities"), this.#t("getChapters");
  }
  #I(e, t, s) {
    switch (e) {
      case "getVideoTitle":
        const i = t;
        this.#e.notify("title-change", i, s);
        break;
      case "getDuration":
        const n = t;
        this.#e.$state.canPlay() ? this.#e.notify("duration-change", n, s) : this.#f(n, s);
        break;
      case "getCurrentTime":
        this.#$(t, s);
        break;
      case "getBuffered":
        k(t) && t.length && this.#p(t[t.length - 1][1], s);
        break;
      case "setMuted":
        this.#y(l(this.#e.$state.volume), t, s);
        break;
      // case 'getTextTracks':
      //   this.#onTextTracksChange(data as VimeoTextTrack[], trigger);
      //   break;
      case "getChapters":
        this.#L(t);
        break;
      case "getQualities":
        this.#F(t, s);
        break;
    }
    this.#m(e)?.resolve();
  }
  #R() {
    for (const e of $)
      this.#t("addEventListener", e);
  }
  #x(e) {
    this.#l.stop(), this.#e.notify("pause", void 0, e);
  }
  #A(e) {
    this.#l.start(), this.#e.notify("play", void 0, e);
  }
  #M(e) {
    const { paused: t } = this.#e.$state;
    !t() && !this.#h && this.#e.notify("playing", void 0, e);
  }
  #p(e, t) {
    const s = {
      buffered: new o(0, e),
      seekable: this.#n
    };
    this.#e.notify("progress", s, t);
  }
  #S(e) {
    this.#e.notify("waiting", void 0, e);
  }
  #q(e) {
    const { paused: t } = this.#e.$state;
    t() || this.#e.notify("playing", void 0, e);
  }
  #Q(e) {
    const { paused: t } = this.#e.$state;
    t() && this.#e.notify("play", void 0, e), this.#e.notify("waiting", void 0, e);
  }
  #y(e, t, s) {
    const i = { volume: e, muted: t };
    this.#e.notify("volume-change", i, s);
  }
  // #onTextTrackChange(track: VimeoTextTrack, trigger: Event) {
  //   const textTrack = this.#ctx.textTracks.toArray().find((t) => t.language === track.language);
  //   if (textTrack) textTrack.mode = track.mode;
  // }
  // #onTextTracksChange(tracks: VimeoTextTrack[], trigger: Event) {
  //   for (const init of tracks) {
  //     const textTrack = new TextTrack({
  //       ...init,
  //       label: init.label.replace('auto-generated', 'auto'),
  //     });
  //     textTrack[TextTrackSymbol.readyState] = 2;
  //     this.#ctx.textTracks.add(textTrack, trigger);
  //     textTrack.setMode(init.mode, trigger);
  //   }
  // }
  // #onCueChange(cue: VimeoTextCue, trigger: Event) {
  //   const { textTracks, $state } = this.#ctx,
  //     { currentTime } = $state,
  //     track = textTracks.selected;
  //   if (this.#currentCue) track?.removeCue(this.#currentCue, trigger);
  //   this.#currentCue = new window.VTTCue(currentTime(), Number.MAX_SAFE_INTEGER, cue.text);
  //   track?.addCue(this.#currentCue, trigger);
  // }
  #L(e) {
    if (this.#b(), !e.length) return;
    const t = new w({
      kind: "chapters",
      default: !0
    }), { seekableEnd: s } = this.#e.$state;
    for (let i = 0; i < e.length; i++) {
      const n = e[i], a = e[i + 1];
      t.addCue(
        new window.VTTCue(
          n.startTime,
          a?.startTime ?? s(),
          n.title
        )
      );
    }
    this.#r = t, this.#e.textTracks.add(t);
  }
  #b() {
    this.#r && (this.#e.textTracks.remove(this.#r), this.#r = null);
  }
  #F(e, t) {
    this.#e.qualities[f.enableAuto] = e.some((s) => s.id === "auto") ? () => this.#t("setQuality", "auto") : void 0;
    for (const s of e) {
      if (s.id === "auto") continue;
      const i = +s.id.slice(0, -1);
      isNaN(i) || this.#e.qualities[r.add](
        {
          id: s.id,
          width: i * (16 / 9),
          height: i,
          codec: "avc1,h.264",
          bitrate: -1
        },
        t
      );
    }
    this.#v(
      e.find((s) => s.active),
      t
    );
  }
  #v({ id: e } = {}, t) {
    if (!e) return;
    const s = e === "auto", i = this.#e.qualities.getById(e);
    s ? (this.#e.qualities[f.setAuto](s, t), this.#e.qualities[r.select](void 0, !0, t)) : this.#e.qualities[r.select](i ?? void 0, !0, t);
  }
  #O(e, t, s) {
    switch (e) {
      case "ready":
        this.#R();
        break;
      case "loaded":
        this.#V(s);
        break;
      case "play":
        this.#A(s);
        break;
      case "playProgress":
        this.#M(s);
        break;
      case "pause":
        this.#x(s);
        break;
      case "loadProgress":
        this.#p(t.seconds, s);
        break;
      case "waiting":
        this.#Q(s);
        break;
      case "bufferstart":
        this.#S(s);
        break;
      case "bufferend":
        this.#q(s);
        break;
      case "volumechange":
        this.#y(t.volume, l(this.#e.$state.muted), s);
        break;
      case "durationchange":
        this.#n = new o(0, t.duration), this.#e.notify("duration-change", t.duration, s);
        break;
      case "playbackratechange":
        this.#e.notify("rate-change", t.playbackRate, s);
        break;
      case "qualitychange":
        this.#v(t, s);
        break;
      case "fullscreenchange":
        this.#d = t.fullscreen, this.#e.notify("fullscreen-change", t.fullscreen, s);
        break;
      case "enterpictureinpicture":
        this.#e.notify("picture-in-picture-change", !0, s);
        break;
      case "leavepictureinpicture":
        this.#e.notify("picture-in-picture-change", !1, s);
        break;
      case "ended":
        this.#e.notify("end", void 0, s);
        break;
      case "error":
        this.#D(t, s);
        break;
      case "seek":
      case "seeked":
        this.#E(t.seconds, s);
        break;
    }
  }
  #D(e, t) {
    const { message: s, method: i } = e;
    i === "setPlaybackRate" && this.#i.set(!1), i && this.#m(i)?.reject(s);
  }
  onMessage(e, t) {
    e.event ? this.#O(e.event, e.data, t) : e.method && this.#I(e.method, e.value, t);
  }
  onLoad() {
  }
  async #t(e, t) {
    let s = d(), i = this.#a.get(e);
    return i || this.#a.set(e, i = []), i.push(s), this.postMessage({
      method: e,
      value: t
    }), s.promise;
  }
  #k() {
    this.#l.stop(), this.#n = new o(0, 0), this.#u = null, this.#w = null, this.#i.set(!1), this.#b();
  }
  #m(e) {
    return this.#a.get(e)?.shift();
  }
}
export {
  M as VimeoProvider
};
