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, useState, useEffect } from "react";
import ArrowUpwardIcon from "@mui/icons-material/ArrowUpward";
import { makeStyles } from "../theme";
import { useConstCallback } from "powerhooks/useConstCallback";
import { Evt } from "evt";
import { useEvt } from "evt/hooks";
import { getScrollableParent } from "powerhooks/getScrollableParent";
import { useStateRef } from "powerhooks/useStateRef";
export var GlLinkToTop = memo(function (props) {
    var className = props.className;
    var _a = __read(useState(false), 2), isShown = _a[0], setIsShown = _a[1];
    var _b = __read(useState(undefined), 2), scrollableParent = _b[0], setScrollableParent = _b[1];
    var ref = useStateRef(null);
    useEffect(function () {
        var element = ref.current;
        if (!element) {
            return;
        }
        setScrollableParent(getScrollableParent({
            element: element,
            "doReturnElementIfScrollable": true,
        }));
    }, []);
    var onClick = useConstCallback(function () {
        if (scrollableParent === undefined) {
            return;
        }
        scrollableParent.scrollTo({
            "top": 0,
            "behavior": "smooth",
        });
    });
    useEvt(function (ctx) {
        if (scrollableParent === undefined) {
            return;
        }
        Evt.from(ctx, scrollableParent, "scroll").attach(function () {
            var scrollTop = scrollableParent.scrollTop;
            if (scrollTop / (window.innerHeight / 100) >= 70) {
                setIsShown(true);
                return;
            }
            setIsShown(false);
        });
    }, [scrollableParent]);
    var _c = useStyles({ isShown: isShown }, { props: props }), classes = _c.classes, cx = _c.cx;
    return (_jsx("div", __assign({ ref: ref, onClick: onClick, className: cx(classes.root, className) }, { children: _jsx(ArrowUpwardIcon, { className: classes.arrowIcon }) })));
});
var useStyles = makeStyles({ "name": { GlLinkToTop: GlLinkToTop } })(function (theme, _a) {
    var isShown = _a.isShown;
    return ({
        "root": {
            "transition": "opacity, 500ms",
            "zIndex": 1,
            "display": "flex",
            "backgroundColor": theme.colors.useCases.surfaces.background,
            "alignItems": "center",
            "borderRadius": 5,
            "justifyContent": "center",
            "padding": theme.spacing(1),
            "opacity": isShown ? 0.6 : 0,
            "pointerEvents": isShown ? undefined : "none",
            "position": "fixed",
            "border": "solid ".concat(theme.colors.useCases.typography.textPrimary, " 3px"),
            "top": "90%",
            "right": theme.paddingRightLeft,
            "cursor": "pointer",
        },
        "arrowIcon": {},
    });
});
//# sourceMappingURL=GlLinkToTop.js.map