import { jsxs as S, jsx as e, Fragment as re } from "react/jsx-runtime";
import { s as o, c as X } from "./sl-types.js";
import { useState as I, useEffect as b, useCallback as w, forwardRef as ce, useRef as L } from "react";
import { B as f } from "./index6.js";
import { G as ie, d as ue, O as de } from "./index14.js";
import { C as pe } from "./useStreamLayerApp2.js";
const me = /* @__PURE__ */ o("div")({
  name: "CloseIconWrap",
  class: "c3v6bv6",
  propsAsIs: !1
}), ye = /* @__PURE__ */ o("div")({
  name: "TimerWrap",
  class: "teuhmro",
  propsAsIs: !1
}), he = () => f, fe = /* @__PURE__ */ o(he())({
  name: "Icon",
  class: "i17rfxv5",
  propsAsIs: !0
}), Ye = ({
  close: n,
  className: a,
  timerClassName: s,
  skipIcon: l,
  isPlaying: u = !0,
  duration: i = 0,
  autoClose: d
}) => {
  const [p, m] = I(i === 0);
  b(() => {
    m(!i);
  }, [i]);
  const P = w(() => {
    d === pe.ENABLED ? n == null || n() : m(!0);
  }, [d, n]);
  return p ? /* @__PURE__ */ S(me, {
    "data-a": "intr",
    onClick: n,
    className: a,
    style: l ? {
      width: "auto"
    } : {},
    children: [l && /* @__PURE__ */ e("span", {
      "data-a": "intr",
      children: "Skip"
    }), /* @__PURE__ */ e(fe, {
      "data-a": "intr",
      name: l ? "iconSkip" : "icon-cross"
    })]
  }) : /* @__PURE__ */ e(ye, {
    children: /* @__PURE__ */ e(ie, {
      className: s,
      isPlaying: u,
      duration: i,
      onTimerExpired: P
    })
  });
}, ge = /* @__PURE__ */ o("div")({
  name: "Container",
  class: "c1bgb86j",
  propsAsIs: !1
}), Ie = /* @__PURE__ */ o("video")({
  name: "Player",
  class: "p6703cq",
  propsAsIs: !1
}), be = /* @__PURE__ */ o("img")({
  name: "Poster",
  class: "p12ow7zo",
  propsAsIs: !1
}), R = /* @__PURE__ */ o("div")({
  name: "Control",
  class: "cdpal9o",
  propsAsIs: !1
}), Pe = "t1xke4ml", ve = "h1yzxv57", Ae = /* @__PURE__ */ o("div")({
  name: "VideoControls",
  class: "vxo8xch",
  propsAsIs: !1
}), $ = /* @__PURE__ */ o("button")({
  name: "ControlBtn",
  class: "coyuzu0",
  propsAsIs: !1
}), Ce = () => $, we = /* @__PURE__ */ o(Ce())({
  name: "MuteIcon",
  class: "musmi9a",
  propsAsIs: !0
}), Me = () => $, ke = /* @__PURE__ */ o(Me())({
  name: "PauseIcon",
  class: "p1qe7rum",
  propsAsIs: !0
}), Ee = () => $, xe = /* @__PURE__ */ o(Ee())({
  name: "PlayIcon",
  class: "psevery",
  propsAsIs: !0
}), Y = /* @__PURE__ */ o("button")({
  name: "WebOSIconButton",
  class: "w1q3u7py",
  propsAsIs: !1
}), Be = () => Y, T = /* @__PURE__ */ o(Be())({
  name: "WebOSIconButtonAnimation",
  class: "wdlqvum",
  propsAsIs: !0
}), Oe = () => T, We = /* @__PURE__ */ o(Oe())({
  name: "WebOSIconButtonPlay",
  class: "w1ylz9ea",
  propsAsIs: !0
}), Ne = () => T, Ve = /* @__PURE__ */ o(Ne())({
  name: "WebOSIconButtonPause",
  class: "w1iylt55",
  propsAsIs: !0
}), Le = () => T, Re = /* @__PURE__ */ o(Le())({
  name: "WebOSIconButtonMute",
  class: "w8h4h7",
  propsAsIs: !0
}), Se = () => T, Te = /* @__PURE__ */ o(Se())({
  name: "WebOSIconButtonUnmute",
  class: "w12l12kt",
  propsAsIs: !0
}), Ue = (n) => {
  for (const a of n)
    a.isIntersecting ? a.target instanceof HTMLVideoElement && a.target.autoplay && a.target.play() : a.target instanceof HTMLVideoElement && !a.target.paused && a.target.pause();
}, Z = new IntersectionObserver(Ue, { threshold: 0.5 }), D = /* @__PURE__ */ new Set(), je = (n) => {
  Z.observe(n), D.add(n);
}, qe = (n) => {
  Z.unobserve(n), D.delete(n);
}, ze = () => {
  for (const n of D)
    n instanceof HTMLVideoElement && !n.paused && n.pause();
};
function He(...n) {
  return w(
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (a) => {
      for (const s of n)
        s !== null && (typeof s == "function" ? s(a) : s && (s.current = a));
    },
    // Re-run the callback if the array of refs changes
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [...n]
  );
}
const $e = de(ge), De = ({ loading: n, autoPlay: a, played: s, playing: l, isMuted: u, unmute: i, mute: d, togglePlaying: p }) => !n && !a && !s ? /* @__PURE__ */ e(R, { children: l ? /* @__PURE__ */ e(f, { name: "icon-pause" }) : /* @__PURE__ */ e(f, { name: "icon-play" }) }) : s ? /* @__PURE__ */ S(Ae, { children: [
  l ? /* @__PURE__ */ e(ke, { onClick: p }) : /* @__PURE__ */ e(xe, { onClick: p }),
  /* @__PURE__ */ e(we, { className: X(!u && "muted"), onClick: u ? i : d })
] }) : null, Fe = ({
  loading: n,
  autoPlay: a,
  playMuted: s,
  playing: l,
  played: u,
  unmute: i,
  mute: d,
  isMuted: p,
  hasNotPausedVideo: m,
  hasInteractedWithAudio: P
}) => /* @__PURE__ */ S(re, { children: [
  !n && !(a && s) && /* @__PURE__ */ e(R, { "data-control-btn": "true", children: l ? m ? null : /* @__PURE__ */ e(We, { children: /* @__PURE__ */ e(f, { name: "icon-play" }) }) : u ? /* @__PURE__ */ e(Ve, { children: /* @__PURE__ */ e(f, { name: "icon-pause" }) }) : /* @__PURE__ */ e(Y, { "data-control-icon": "true", children: /* @__PURE__ */ e(f, { name: "icon-play" }) }) }),
  !n && a && s && P && /* @__PURE__ */ e(R, { children: p ? /* @__PURE__ */ e(Re, { onClick: i, children: /* @__PURE__ */ e(f, { name: "iconMute" }) }) : /* @__PURE__ */ e(Te, { onClick: d, children: /* @__PURE__ */ e(f, { name: "iconUnMute" }) }) })
] }), Ze = ce(
  ({
    poster: n,
    source: a,
    aspectRatio: s,
    controlVideo: l,
    onReplay: u,
    onPlay: i,
    onPause: d,
    onEnd: p,
    onRender: m,
    onMute: P,
    onQuartile: v,
    autoPlay: M,
    playMuted: k,
    className: ee,
    muted: E,
    webos: B,
    hasNotPausedVideo: ne
  }, te) => {
    const c = L(null), ae = He(c, te), U = L(!1), j = L(!1), y = L(/* @__PURE__ */ new Set()), [F, G] = I(!1), [O, W] = I(!1), [q, Q] = I(!1), [_, N] = I(!0), [z, V] = I(k), [H, oe] = I(!1);
    b(() => {
      a && (V(k), G(!1));
    }, [a]);
    const A = w(() => {
      l == null || l({ muted: !1 });
    }, [l]), J = w(() => {
      l == null || l({ muted: !0 });
    }, [l]), C = w((t) => {
      t == null || t.stopPropagation(), c.current && (c.current.muted = !0), V(!0);
    }, []), x = w(
      (t) => {
        t == null || t.stopPropagation(), c.current && (c.current.muted = !1), V(!1), H || oe(!0);
      },
      [H]
    );
    b(() => {
      E !== void 0 && (E ? C() : x());
    }, [C, E, x]);
    const K = (t) => {
      var r;
      (r = t.stopPropagation) == null || r.call(t);
      const g = c == null ? void 0 : c.current;
      if (g)
        if (O)
          g.pause();
        else {
          const h = c == null ? void 0 : c.current;
          if (!h)
            return;
          ze(), h.play().catch((le) => console.log(le)), N(!1);
        }
    }, se = () => {
      var t;
      (t = c == null ? void 0 : c.current) == null || t.load(), N(!0);
    };
    return b(() => () => {
      A();
    }, [A]), b(() => {
      const t = c.current;
      return t ? (j.current = t.muted, V(t.muted), je(t), () => {
        qe(t);
      }) : () => {
      };
    }, []), b(() => {
      m == null || m();
    }, [m]), b(() => {
      y.current.clear(), U.current = !1;
    }, [a]), /* @__PURE__ */ S(
      $e,
      {
        webos: B,
        onClick: M && k ? z ? x : C : K,
        "data-a": "intr",
        autoFocus: !0,
        options: { focusKey: "video-play-btn" },
        className: X(O && !B && Pe, M && ve, ee),
        style: s ? { aspectRatio: s } : {},
        children: [
          /* @__PURE__ */ e(
            Ie,
            {
              ref: ae,
              src: a,
              autoPlay: M,
              onPlay: () => {
                G(!0), U.current && (u == null || u(), y.current.clear()), W(!0), N(!1), i == null || i(), k ? C() : J();
              },
              onTimeUpdate: (t) => {
                const r = t.target.duration, g = t.target.currentTime;
                if (r && g) {
                  const h = Math.floor(g / r * 100);
                  h >= 25 && h < 50 && !y.current.has(25) ? (v == null || v(25), y.current.add(25)) : h >= 50 && h < 75 && !y.current.has(50) ? (v == null || v(50), y.current.add(50)) : h >= 75 && !y.current.has(75) && (v == null || v(75), y.current.add(75));
                }
              },
              onVolumeChange: (t) => {
                const r = t.target.muted;
                j.current !== r && (P == null || P(r), j.current = r, (r !== E || E === void 0) && (r ? A() : J()));
              },
              onError: async (t) => {
                try {
                  if (t.target instanceof HTMLVideoElement) {
                    Q(!0);
                    const r = await (await fetch(t.target.src)).blob(), g = URL.createObjectURL(r);
                    t.target.src = g;
                  }
                } catch (r) {
                  console.error("video fallback fetch failed", r), W(!0), N(!0);
                } finally {
                  Q(!1), A();
                }
              },
              onPause: () => {
                W(!1), d == null || d(), A();
              },
              onEnded: () => {
                W(!1), U.current = !0, se(), p == null || p(), A();
              },
              style: { visibility: _ ? "hidden" : "visible" },
              controls: !1,
              playsInline: !0
            }
          ),
          /* @__PURE__ */ e(be, { src: n, style: { visibility: _ ? "visible" : "hidden" } }),
          !B && /* @__PURE__ */ e(
            De,
            {
              loading: q,
              autoPlay: M,
              played: F,
              playing: O,
              isMuted: z,
              unmute: x,
              mute: C,
              togglePlaying: K
            }
          ),
          B && /* @__PURE__ */ e(
            Fe,
            {
              loading: q,
              autoPlay: M,
              playMuted: k,
              playing: O,
              played: F,
              unmute: x,
              mute: C,
              isMuted: z,
              hasNotPausedVideo: ne,
              hasInteractedWithAudio: H
            }
          ),
          q && /* @__PURE__ */ e(R, { children: /* @__PURE__ */ e(ue, {}) })
        ]
      }
    );
  }
);
export {
  Ze as E,
  ze as w,
  Ye as y
};
