import { defineComponent as m, watch as p, createElementBlock as M, openBlock as _, unref as R, ref as r, onMounted as D } from "vue";
import d from "hls.js";
import { cloneDeep as l, isEqual as c } from "lodash";
import { nanoid as T } from "nanoid";
import { SHJDatasourceV2 as z } from "../../commons/core/datasource/index.mjs";
const A = ["poster", "autoplay", "controls", "loop", "muted"], B = m({ name: "zv-media-m3u8-dynamic" }), N = /* @__PURE__ */ m({
  ...B,
  props: {
    basicOption: {},
    sources: {},
    useEvents: {},
    uuid: {}
  },
  emits: [
    "on-video-progress",
    "on-video-play",
    "on-video-pause",
    "on-video-ended",
    "on-video-playing",
    "on-video-timeupdate",
    "on-video-error",
    "on-video-volumechange"
  ],
  setup(E, { expose: f, emit: y }) {
    const e = E, w = () => {
      const o = r(), n = r(""), O = r(T()), u = () => {
        z.parse({
          tId: e.uuid,
          sources: e.sources,
          callback: ({ data: v }) => {
            try {
              if (window.SHJParseEvent(e.useEvents, "dataListener", v), n.value = v[0].data[0].src, n.value)
                if (d.isSupported()) {
                  const a = new d();
                  a.loadSource(n.value), a.attachMedia(o.value), a.on(d.Events.MANIFEST_PARSED, () => {
                    e.basicOption.autoplay && o.value.play();
                  });
                } else o.value.canPlayType("application/vnd.apple.mpegurl") && (o.value.src = n.value);
            } catch {
              window.SHJParseEvent(e.useEvents, "dataListener", null);
            }
          }
        });
      };
      return D(() => u()), { key: O, videoRef: o, initM3u8Video: u };
    }, { key: P, videoRef: i, initM3u8Video: t } = w();
    p(() => l(e.basicOption), (o, n) => {
      c(o, n) || t();
    }, { deep: !0 }), p(() => l(e.sources), (o, n) => {
      c(o, n) || t();
    }, { deep: !0 });
    const s = y, g = () => {
      s("on-video-progress"), window.SHJParseEvent(e.useEvents, "on-video-progress");
    }, V = () => {
      s("on-video-play"), window.SHJParseEvent(e.useEvents, "on-video-play");
    }, S = () => {
      s("on-video-pause"), window.SHJParseEvent(e.useEvents, "on-video-pause");
    }, h = () => {
      s("on-video-ended"), window.SHJParseEvent(e.useEvents, "on-video-ended");
    }, H = () => {
      s("on-video-playing"), window.SHJParseEvent(e.useEvents, "on-video-playing");
    }, J = () => {
      s("on-video-timeupdate"), window.SHJParseEvent(e.useEvents, "on-video-timeupdate");
    }, b = () => {
      s("on-video-error"), window.SHJParseEvent(e.useEvents, "on-video-error");
    }, k = () => {
      s("on-video-volumechange"), window.SHJParseEvent(e.useEvents, "on-video-volumechange");
    };
    return f({
      /** 开始播放 */
      play: () => {
        i.value && i.value.play();
      },
      /** 暂停播放 */
      pause: () => {
        i.value && i.value.pause();
      },
      refresh: () => t(),
      refreshView: () => t(),
      refreshData: () => t()
    }), (o, n) => (_(), M("video", {
      key: R(P),
      ref_key: "videoRef",
      ref: i,
      class: "zerov-widget",
      poster: o.basicOption.poster,
      autoplay: o.basicOption.autoplay,
      controls: o.basicOption.controls,
      loop: o.basicOption.loop,
      muted: o.basicOption.muted,
      onProgress: g,
      onPlay: V,
      onEnded: h,
      onPlaying: H,
      onPause: S,
      onTimeupdate: J,
      onError: b,
      onVolumechange: k
    }, null, 40, A));
  }
});
export {
  N as default
};
