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, jsxs as _jsxs } from "react/jsx-runtime";
import { memo, useMemo, useReducer } from "react";
import { Text, breakpointsValues, makeStyles, Button } from "./theme";
import { useDomRect } from "powerhooks/useDomRect";
import { motion } from "framer-motion";
import { useIntersectionObserver } from "./tools/useIntersectionObserver";
export var GlYoutubeVideoSection = memo(function (props) {
    var className = props.className, link = props.link, src = props.src, title = props.title, width = props.width, height = props.height, buttonLabel = props.buttonLabel, hasAnimation = props.hasAnimation;
    var _a = __read(useReducer(function (x) { return x + 1; }, 0), 2), forceUpdate = _a[1];
    var _b = useDomRect(), iframeWidth = _b.domRect.width, iframeRef = _b.ref;
    var animationProps = useMemo(function () {
        if (hasAnimation === undefined ||
            !hasAnimation ||
            src === undefined) {
            return undefined;
        }
        return {
            "initial": {
                "opacity": 0,
            },
            "animate": {},
            "transition": {
                "duration": 1,
                "ease": "easeOut",
            },
        };
    }, []);
    var ref = useIntersectionObserver({
        "callback": function (_a) {
            var entry = _a.entry, observer = _a.observer;
            if (hasAnimation === undefined ||
                !hasAnimation ||
                animationProps === undefined) {
                observer.unobserve(entry.target);
                return;
            }
            if (entry.isIntersecting) {
                animationProps.animate = {
                    "opacity": 1,
                };
                observer.unobserve(entry.target);
                forceUpdate();
            }
        },
        "threshold": 0.5,
    }, []).ref;
    var _c = useStyles({
        width: width,
        height: height,
        "currentWidth": iframeWidth,
    }, { props: props }), classes = _c.classes, cx = _c.cx;
    return (_jsxs("section", __assign({ ref: ref, className: cx(classes.root, className) }, { children: [title !== undefined && (_jsx(Text, __assign({ className: classes.title, typo: "page heading" }, { children: title }))), src !== undefined && (_jsx(motion.iframe, __assign({}, animationProps, { ref: iframeRef, className: classes.iframe, src: src, title: "YouTube video player", allow: "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture", allowFullScreen: true }))), buttonLabel !== undefined && (_jsx(Button, __assign({ onClick: link === null || link === void 0 ? void 0 : link.onClick, className: classes.button, href: link === null || link === void 0 ? void 0 : link.href, type: "submit", variant: "secondary" }, { children: buttonLabel })))] })));
});
var useStyles = makeStyles({ "name": { GlYoutubeVideoSection: GlYoutubeVideoSection } })(function (theme, _a) {
    var height = _a.height, width = _a.width, currentWidth = _a.currentWidth;
    return ({
        "root": __assign({ "display": "grid", "gridTemplateColumns": "1fr", "justifyItems": "center" }, theme.spacing.rightLeft("padding", "".concat(theme.paddingRightLeft, "px"))),
        "title": {
            "textAlign": "center",
        },
        "iframe": __assign(__assign({ "border": "none" }, theme.spacing.topBottom("margin", "".concat(theme.spacing(7), "px"))), { "boxShadow": theme.customShadow, "width": width !== null && width !== void 0 ? width : (function () {
                if (theme.windowInnerWidth >= breakpointsValues.md) {
                    return 700;
                }
                if (theme.windowInnerWidth >= breakpointsValues.sm) {
                    return "70%";
                }
                return "100%";
            })(), "height": height !== null && height !== void 0 ? height : (function () {
                if (theme.windowInnerWidth >= breakpointsValues.md) {
                    return 7 * 60;
                }
                return (currentWidth / 100) * 60;
            })() }),
        "button": {},
    });
});
//# sourceMappingURL=GlYoutubeVideoSection.js.map