"use strict";
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GlArticle = void 0;
var jsx_runtime_1 = require("react/jsx-runtime");
var react_1 = require("react");
var theme_1 = require("./theme");
var theme_2 = require("./theme");
var GlButton_1 = require("./utils/GlButton");
var framer_motion_1 = require("framer-motion");
var useIntersectionObserver_1 = require("./tools/useIntersectionObserver");
var Markdown_1 = require("./tools/Markdown");
var theme_3 = require("./theme");
var GlImage_1 = require("./utils/GlImage");
var GlVideo_1 = require("./utils/GlVideo");
var useConstCallback_1 = require("powerhooks/useConstCallback");
var textTransitionParameters = {
    "ease": "easeOut",
    "duration": 0.5,
};
function getIllustrationAnimationProps(params) {
    var illustrationPosition = params.illustrationPosition;
    return {
        "initial": (function () {
            return {
                "opacity": 0,
                "x": illustrationPosition === "left" ? -100 : 100,
            };
        })(),
        "animate": {},
        "transition": {
            "delay": 0.3,
            "duration": 0.5,
            "ease": "easeOut",
        },
    };
}
function getTitleAnimationProps(params) {
    var illustrationPosition = params.illustrationPosition;
    return {
        "initial": {
            "opacity": 0,
            "x": (function () {
                var value = 100;
                switch (illustrationPosition) {
                    case "left":
                        return value;
                    default:
                        return -value;
                }
            })(),
        },
        "animate": {},
        "transition": textTransitionParameters,
    };
}
var bodyAnimationProps = {
    "initial": {
        "opacity": 0,
    },
    "animate": {},
    "transition": textTransitionParameters,
};
exports.GlArticle = (0, react_1.memo)(function (props) {
    var illustration = props.illustration, body = props.body, buttonLabel = props.buttonLabel, illustrationPosition = props.illustrationPosition, title = props.title, className = props.className, id = props.id, buttonLink = props.buttonLink, hasAnimation = props.hasAnimation;
    var _a = __read((0, react_1.useReducer)(function (x) { return x + 1; }, 0), 2), forceUpdate = _a[1];
    var _b = __read((0, react_1.useState)(function () {
        if (illustration === undefined ||
            illustration.type === "custom component") {
            return true;
        }
        return false;
    }), 2), isIllustrationLoaded = _b[0], setIsIllustrationLoaded = _b[1];
    var illustrationAnimationProps = (0, react_1.useMemo)(function () { return getIllustrationAnimationProps({ illustrationPosition: illustrationPosition }); }, [illustrationPosition]);
    var titleAnimationProps = (0, react_1.useMemo)(function () { return getTitleAnimationProps({ illustrationPosition: illustrationPosition }); }, [illustrationAnimationProps]);
    var ref = (0, useIntersectionObserver_1.useIntersectionObserver)({
        "callback": function (_a) {
            var observer = _a.observer, entry = _a.entry;
            if (hasAnimation === undefined || !hasAnimation) {
                observer.unobserve(entry.target);
                return;
            }
            if (entry.isIntersecting && isIllustrationLoaded) {
                console.log("pipi");
                illustrationAnimationProps.animate = {
                    "opacity": 1,
                    "x": 0,
                };
                titleAnimationProps.animate = {
                    "opacity": 1,
                    "x": 0,
                };
                bodyAnimationProps.animate = {
                    "opacity": 1,
                };
                observer.unobserve(entry.target);
                forceUpdate();
            }
        },
        "threshold": 0.2,
    }, [isIllustrationLoaded]).ref;
    var onIllustrationLoaded = (0, useConstCallback_1.useConstCallback)(function () {
        setIsIllustrationLoaded(true);
    });
    var hasArticle = title !== undefined || body !== undefined || buttonLabel !== undefined;
    var hasIllustration = illustration !== undefined;
    var _c = useStyles({
        "illustrationPosition": illustrationPosition !== null && illustrationPosition !== void 0 ? illustrationPosition : "right",
        hasIllustration: hasIllustration,
        hasArticle: hasArticle,
        isIllustrationLoaded: isIllustrationLoaded,
    }, { props: props }), classes = _c.classes, cx = _c.cx;
    return ((0, jsx_runtime_1.jsxs)("section", __assign({ ref: ref, id: id, className: cx(classes.root, className) }, { children: [hasArticle && ((0, jsx_runtime_1.jsxs)("article", __assign({ className: classes.article }, { children: [title && ((0, jsx_runtime_1.jsx)(framer_motion_1.motion.div, __assign({}, (function () {
                        if (!hasAnimation) {
                            return undefined;
                        }
                        return titleAnimationProps;
                    })(), { children: typeof title === "string" ? ((0, jsx_runtime_1.jsx)(theme_3.Text, __assign({ typo: "page heading" }, { children: title }))) : (title) }))), body && ((0, jsx_runtime_1.jsx)(framer_motion_1.motion.div, __assign({}, (function () {
                        if (!hasAnimation) {
                            return undefined;
                        }
                        return bodyAnimationProps;
                    })(), { children: typeof body === "string" ? ((0, jsx_runtime_1.jsx)(Markdown_1.Markdown, __assign({ className: classes.body }, { children: body }))) : (body) }))), buttonLabel && ((0, jsx_runtime_1.jsx)(GlButton_1.GlButton, __assign({ type: "submit", href: buttonLink === null || buttonLink === void 0 ? void 0 : buttonLink.href, onClick: buttonLink === null || buttonLink === void 0 ? void 0 : buttonLink.onClick, variant: "secondary", className: classes.button }, { children: buttonLabel })))] }))), hasIllustration && ((0, jsx_runtime_1.jsx)(framer_motion_1.motion.aside, __assign({ className: classes.aside }, (function () {
                if (!hasAnimation) {
                    return undefined;
                }
                return illustrationAnimationProps;
            })(), { children: (function () {
                    switch (illustration.type) {
                        case "custom component":
                            return ((0, jsx_runtime_1.jsx)(illustration.Component, { onLoad: onIllustrationLoaded, id: "customComponentId", className: classes.customComponent }));
                        case "image":
                            return ((0, jsx_runtime_1.jsx)(GlImage_1.GlImage, __assign({ className: classes.image, onLoad: onIllustrationLoaded }, illustration)));
                        case "video":
                            return ((0, jsx_runtime_1.jsx)(GlVideo_1.GlVideo, __assign({ className: classes.video, onLoad: onIllustrationLoaded }, illustration)));
                    }
                })() })))] })));
});
var useStyles = (0, theme_1.makeStyles)({ "name": { GlArticle: exports.GlArticle } })(function (theme, _a) {
    var illustrationPosition = _a.illustrationPosition, hasIllustration = _a.hasIllustration, hasArticle = _a.hasArticle, isIllustrationLoaded = _a.isIllustrationLoaded;
    return ({
        "root": __assign(__assign(__assign({}, theme.spacing.rightLeft("padding", "".concat(theme.paddingRightLeft, "px"))), { "display": "flex", "alignItems": "center", "justifyContent": "center", "flexDirection": (function () {
                if (theme.windowInnerWidth < theme_2.breakpointsValues.md) {
                    return "column";
                }
                switch (illustrationPosition) {
                    case "left":
                        return "row-reverse";
                    case "right":
                        return "row";
                }
            })() }), theme.spacing.topBottom("margin", "".concat(theme.spacing(7), "px"))),
        "article": __assign({ "display": "flex", "flexDirection": "column", "minWidth": theme.windowInnerWidth < theme_2.breakpointsValues.md ? undefined : 300, "flex": hasIllustration ? 0.7 : undefined, "marginBottom": theme.windowInnerWidth >= theme_2.breakpointsValues.md ||
                !hasIllustration
                ? undefined
                : theme.spacing(8) }, (function () {
            var value = theme.windowInnerWidth >= theme_2.breakpointsValues.lg
                ? theme.spacing(9)
                : theme.spacing(5);
            if (theme.windowInnerWidth < theme_2.breakpointsValues.md ||
                !hasIllustration) {
                return undefined;
            }
            switch (illustrationPosition) {
                case "left":
                    return { "marginRight": value };
                case "right":
                    return { "marginLeft": value };
            }
        })()),
        "aside": __assign({}, (function () {
            if (!hasArticle ||
                theme.windowInnerWidth < theme_2.breakpointsValues.md) {
                return undefined;
            }
            var value = theme.spacing(10);
            switch (illustrationPosition) {
                case "left":
                    return {
                        "marginRight": value,
                    };
                case "right":
                    return {
                        "marginLeft": value,
                    };
            }
        })()),
        "body": {
            "color": theme.colors.useCases.typography.textSecondary,
        },
        "button": {
            "alignSelf": "end",
            "opacity": isIllustrationLoaded ? 1 : 0,
        },
        "image": {
            "width": "100%",
            "height": "auto",
            "objectFit": "cover",
            "verticalAlign": "middle",
        },
        "video": {
            "width": "100%",
        },
        "customComponent": {},
    });
});
//# sourceMappingURL=GlArticle.js.map