"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GlLinkToTop = void 0;
var jsx_runtime_1 = require("react/jsx-runtime");
var react_1 = require("react");
var ArrowUpward_1 = __importDefault(require("@mui/icons-material/ArrowUpward"));
var theme_1 = require("../theme");
var useConstCallback_1 = require("powerhooks/useConstCallback");
var evt_1 = require("evt");
var hooks_1 = require("evt/hooks");
var useGetScrollableParent_1 = require("../tools/useGetScrollableParent");
exports.GlLinkToTop = (0, react_1.memo)(function (props) {
    var className = props.className;
    var _a = __read((0, react_1.useState)(false), 2), isShown = _a[0], setIsShown = _a[1];
    var _b = (0, useGetScrollableParent_1.useGetScrollableParent)(), ref = _b.ref, scrollableParent = _b.scrollableParent;
    var onClick = (0, useConstCallback_1.useConstCallback)(function () {
        if (scrollableParent === undefined) {
            return;
        }
        scrollableParent.scrollTo({
            "top": 0,
            "behavior": "smooth",
        });
    });
    (0, hooks_1.useEvt)(function (ctx) {
        if (scrollableParent === undefined) {
            return;
        }
        evt_1.Evt.from(ctx, scrollableParent, "scroll").attach(function () {
            var scrollTop = (function () {
                if (scrollableParent === window) {
                    return scrollableParent
                        .scrollY;
                }
                return 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 ((0, jsx_runtime_1.jsx)("div", __assign({ ref: ref, onClick: onClick, className: cx(classes.root, className) }, { children: (0, jsx_runtime_1.jsx)(ArrowUpward_1.default, { className: classes.arrowIcon }) })));
});
var useStyles = (0, theme_1.makeStyles)({ "name": { GlLinkToTop: exports.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