var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
import { jsx as _jsx } from "react/jsx-runtime";
import { memo, forwardRef, useEffect, useState, useId } from "react";
import { tss } from "../theme";
import { useIntersectionObserver } from "../tools/useIntersectionObserver";
import { useMergeRefs } from "powerhooks/useMergeRefs";
export var GlVideo = memo(forwardRef(function (props, ref_forwarded) {
    var sources = props.sources, className = props.className, _a = props.hasShadow, hasShadow = _a === void 0 ? true : _a, _b = props.hasBorderRadius, hasBorderRadius = _b === void 0 ? true : _b, height = props.height, id_props = props.id, width = props.width, _c = props.autoPlay, autoPlay = _c === void 0 ? true : _c, _d = props.delayBeforeAutoPlay, delayBeforeAutoPlay = _d === void 0 ? 0 : _d, loop = props.loop, muted = props.muted, controls = props.controls, onLoad = props.onLoad;
    var _e = useStyles({
        hasShadow: hasShadow,
        hasBorderRadius: hasBorderRadius,
    }), classes = _e.classes, cx = _e.cx;
    var _f = __read(useState(false), 2), isDataLoaded = _f[0], setIsDataLoaded = _f[1];
    var _g = __read(useState(false), 2), isIntersected = _g[0], setIntersected = _g[1];
    var id = (function useClosure() {
        var id = useId();
        return id_props !== null && id_props !== void 0 ? id_props : id;
    })();
    var internalRef = useIntersectionObserver({
        "callback": function (_a) {
            var observer = _a.observer, entry = _a.entry;
            if (entry.isIntersecting) {
                observer.unobserve(entry.target);
                setIntersected(true);
            }
        },
        "threshold": 0.2,
    }, []).ref;
    var ref = useMergeRefs([internalRef, ref_forwarded]);
    useEffect(function () {
        if (delayBeforeAutoPlay === 0) {
            return;
        }
        if (!isDataLoaded || !isIntersected) {
            return;
        }
        var timer = setTimeout(function () {
            //@ts-expect-error: we know is will work
            document.getElementById(id).play();
        }, delayBeforeAutoPlay);
        return function () {
            clearTimeout(timer);
        };
    }, [isDataLoaded]);
    return (_jsx("video", __assign({ ref: ref, className: cx(classes.root, className), onLoadedData: function () {
            setIsDataLoaded(true);
            onLoad === null || onLoad === void 0 ? void 0 : onLoad();
        }, loop: loop !== null && loop !== void 0 ? loop : true, muted: muted !== null && muted !== void 0 ? muted : true, autoPlay: delayBeforeAutoPlay === 0 && autoPlay, id: id, width: width, height: height, playsInline: true, controls: controls !== null && controls !== void 0 ? controls : false }, { children: sources.map(function (source) { return (_jsx("source", __assign({}, source))); }) })));
}));
var useStyles = tss
    .withParams()
    .withName({ GlVideo: GlVideo })
    .create(function (_a) {
    var theme = _a.theme, hasShadow = _a.hasShadow, hasBorderRadius = _a.hasBorderRadius;
    return ({
        "root": {
            "boxShadow": !hasShadow ? undefined : theme.customShadow,
            "borderRadius": !hasBorderRadius ? undefined : theme.borderRadius,
        },
    });
});
//# sourceMappingURL=GlVideo.js.map