"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 tsafe_1 = require("tsafe");
var Markdown_1 = require("./tools/Markdown");
var tss_react_1 = require("tss-react");
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 textTransitionParameters = (0, react_1.useMemo)(function () {
        if (!hasAnimation || hasAnimation === undefined) {
            return;
        }
        return {
            "ease": "easeOut",
            "duration": 0.5,
        };
    }, []);
    var illustrationAnimationProps = (0, react_1.useMemo)(function () {
        if (!hasAnimation || hasAnimation === undefined) {
            return;
        }
        return {
            "initial": (function () {
                return {
                    "opacity": 0,
                    "x": illustrationPosition === "left" ? -100 : 100,
                };
            })(),
            "animate": {},
            "transition": {
                "delay": 0.3,
                "duration": 0.5,
                "ease": "easeOut",
            },
        };
    }, []);
    var titleAnimationProps = (0, react_1.useMemo)(function () {
        if (!hasAnimation || hasAnimation === undefined) {
            return;
        }
        return {
            "initial": {
                "opacity": 0,
                "x": (function () {
                    var value = 100;
                    switch (illustrationPosition) {
                        case "left":
                            return value;
                        default:
                            return -value;
                    }
                })(),
            },
            "animate": {},
            "transition": textTransitionParameters,
        };
    }, []);
    var bodyAnimationProps = (0, react_1.useMemo)(function () {
        if (!hasAnimation || hasAnimation === undefined) {
            return;
        }
        return {
            "initial": {
                "opacity": 0,
            },
            "animate": {},
            "transition": textTransitionParameters,
        };
    }, []);
    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;
            }
            (0, tsafe_1.assert)(illustrationAnimationProps &&
                titleAnimationProps &&
                bodyAnimationProps);
            if (entry.isIntersecting) {
                illustrationAnimationProps.animate = {
                    "opacity": 1,
                    "x": 0,
                };
                titleAnimationProps.animate = {
                    "opacity": 1,
                    "x": 0,
                };
                bodyAnimationProps.animate = {
                    "opacity": 1,
                };
                observer.unobserve(entry.target);
                forceUpdate();
            }
        },
        "threshold": 0.2,
    }).ref;
    var _b = useStyles({
        "illustrationPosition": illustrationPosition !== null && illustrationPosition !== void 0 ? illustrationPosition : "right",
        "hasIllustration": illustration !== undefined,
        "hasArticle": title !== undefined ||
            body !== undefined ||
            buttonLabel !== undefined,
    }), classes = _b.classes, cx = _b.cx;
    classes = (0, tss_react_1.useMergedClasses)(classes, props.classes);
    return ((0, jsx_runtime_1.jsx)("section", __assign({ ref: ref, id: id, className: cx(classes.root, className) }, { children: (0, jsx_runtime_1.jsxs)("div", __assign({ className: classes.contentWrapper }, { children: [(title !== undefined ||
                    body !== undefined ||
                    buttonLabel !== undefined) && ((0, jsx_runtime_1.jsxs)("article", __assign({ className: classes.article }, { children: [title && ((0, jsx_runtime_1.jsx)(framer_motion_1.motion.div, __assign({}, titleAnimationProps, { children: (0, jsx_runtime_1.jsx)(Markdown_1.Markdown, __assign({ className: classes.title }, { children: title })) }))), body && ((0, jsx_runtime_1.jsx)(framer_motion_1.motion.div, __assign({}, bodyAnimationProps, { children: (0, jsx_runtime_1.jsx)(Markdown_1.Markdown, __assign({ className: classes.body }, { children: body })) }))), buttonLabel && ((0, jsx_runtime_1.jsx)("div", __assign({ className: classes.buttonWrapper }, { children: (0, jsx_runtime_1.jsx)(GlButton_1.GlButton, __assign({ className: classes.button, type: "submit", href: buttonLink === null || buttonLink === void 0 ? void 0 : buttonLink.href, onClick: buttonLink === null || buttonLink === void 0 ? void 0 : buttonLink.onClick, variant: "secondary" }, { children: buttonLabel })) })))] }))), (0, jsx_runtime_1.jsx)(framer_motion_1.motion.div, __assign({}, illustrationAnimationProps, { children: (0, jsx_runtime_1.jsx)("aside", __assign({ className: classes.aside }, { children: illustration })) }))] })) })));
});
var useStyles = (0, theme_1.makeStyles)({ "name": { GlArticle: exports.GlArticle } })(function (theme, _a) {
    var illustrationPosition = _a.illustrationPosition, hasIllustration = _a.hasIllustration, hasArticle = _a.hasArticle;
    return ({
        "root": {
            "overflowX": "hidden",
        },
        "contentWrapper": __assign(__assign({ "display": "flex", "flexDirection": (function () {
                if (illustrationPosition === "left" &&
                    theme.windowInnerWidth >= theme_2.breakpointsValues.md) {
                    return "row-reverse";
                }
                if (theme.windowInnerWidth < theme_2.breakpointsValues.md) {
                    return "column";
                }
                return undefined;
            })(), "alignItems": theme.windowInnerWidth < theme_2.breakpointsValues.md
                ? "left"
                : "center", "justifyContent": "center" }, (function () {
            var value = theme.spacing(7);
            return {
                "marginTop": value,
                "marginBottom": value,
            };
        })()), (function () {
            var value = theme.spacing(8);
            if (theme.windowInnerWidth < theme_2.breakpointsValues.lg ||
                !hasArticle) {
                return undefined;
            }
            if (illustrationPosition === "left") {
                return {
                    "paddingRight": value,
                };
            }
            return {
                "paddingLeft": value,
            };
        })()),
        "title": __assign({}, theme.typography.variants["page heading"].style),
        "article": __assign({ "display": "flex", "flexDirection": "column", "textAlign": "left", "marginBottom": (function () {
                if (theme.windowInnerWidth >= theme_2.breakpointsValues.md ||
                    !hasIllustration) {
                    return undefined;
                }
                return theme.spacing(8);
            })(), "width": (function () {
                if (!hasIllustration) {
                    return undefined;
                }
                if (theme.windowInnerWidth >= theme_2.breakpointsValues.xl) {
                    return 412;
                }
                if (theme.windowInnerWidth >= theme_2.breakpointsValues["lg+"]) {
                    return 311;
                }
                if (theme.windowInnerWidth >= theme_2.breakpointsValues.md) {
                    return 270;
                }
                return undefined;
            })() }, (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;
            }
            if (illustrationPosition === "left") {
                return {
                    "marginLeft": value,
                };
            }
            return {
                "marginRight": value,
            };
        })()),
        "body": __assign(__assign({}, theme.typography.variants["body 1"].style), { "margin": theme.spacing({
                "topBottom": 4,
                "rightLeft": 0,
            }), "color": theme.colors.useCases.typography.textSecondary }),
        "buttonWrapper": {
            "display": "flex",
            "justifyContent": "flex-end",
        },
        "button": {
            "alignSelf": "right",
        },
        "aside": __assign({}, (theme.windowInnerWidth >= theme_2.breakpointsValues.md
            ? __assign(__assign({}, (function () {
                if (!hasArticle) {
                    return undefined;
                }
                var value = theme.spacing(8);
                switch (illustrationPosition) {
                    case "left":
                        return {
                            "marginRight": value,
                        };
                    case "right":
                        return {
                            "marginLeft": value,
                        };
                }
            })()), { "maxWidth": 800 }) : {})),
    });
});
//# sourceMappingURL=GlArticle.js.map