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, useState } from "react";
import { makeStyles } from "../theme";
import { symToStr } from "tsafe/symToStr";
import { useCallbackFactory } from "powerhooks/useCallbackFactory";
import { useDomRect } from "powerhooks/useDomRect";
export var GlHeaderLink = memo(function (props) {
    var href = props.href, label = props.label, className = props.className, onClick = props.onClick;
    var _a = __read(useState(false), 2), isUnderlined = _a[0], setIsUnderlined = _a[1];
    var _b = __read(useState(false), 2), isActive = _b[0], setIsActive = _b[1];
    var handleHoverFactory = useCallbackFactory(function (_a) {
        var _b = __read(_a, 1), state = _b[0];
        switch (state) {
            case "enter":
                setIsUnderlined(true);
                return;
            case "leave":
                setIsUnderlined(false);
        }
    });
    var handleClickFactory = useCallbackFactory(function (_a) {
        var _b = __read(_a, 1), state = _b[0];
        switch (state) {
            case "down":
                setIsActive(true);
                return;
            case "up":
                setIsActive(false);
        }
    });
    var _c = useDomRect(), ref = _c.ref, width = _c.domRect.width;
    var _d = useStyles({ isUnderlined: isUnderlined, width: width, isActive: isActive }, { props: props }), classes = _d.classes, cx = _d.cx;
    return (_jsxs("div", __assign({ ref: ref, className: cx(classes.root, className) }, { children: [_jsx("a", __assign({ onMouseLeave: handleHoverFactory("leave"), onMouseEnter: handleHoverFactory("enter"), onMouseDown: handleClickFactory("down"), onMouseUp: handleClickFactory("up"), className: classes.link, href: href, onClick: onClick }, { children: label })), _jsx("div", { className: classes.underline })] })));
});
var useStyles = makeStyles({
    "name": "".concat(symToStr({ GlHeaderLink: GlHeaderLink })),
})(function (theme, _a) {
    var isUnderlined = _a.isUnderlined, isActive = _a.isActive, width = _a.width;
    return {
        "root": {
            "display": "flex",
            "flexDirection": "column",
            "alignItems": "center",
        },
        "link": __assign(__assign({ "transition": "color 200ms", "color": isActive
                ? theme.colors.useCases.buttons.actionActive
                : theme.colors.useCases.typography.textPrimary, "textDecoration": "none" }, theme.spacing.rightLeft("padding", "".concat(theme.spacing(3), "px"))), { "whiteSpace": "nowrap" }),
        "underline": {
            "width": isUnderlined ? width - theme.spacing(3) : 0,
            "marginTop": theme.spacing(1),
            "height": 1,
            "backgroundColor": isActive
                ? theme.colors.useCases.buttons.actionActive
                : theme.colors.useCases.typography.textPrimary,
            "transition": "width 200ms, background-color 200ms",
        },
    };
});
//# sourceMappingURL=GlHeaderLink.js.map