import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import { ArrowLeft } from '@gravity-ui/icons';
import { Button, Icon } from '@gravity-ui/uikit';
import { LocationContext } from "../../gravity-blocks/context/locationContext/index.js";
import { useAnalytics } from "../../gravity-blocks/hooks/index.js";
import { DefaultEventNames } from "../../models/index.js";
export default function BackLink(props) {
    const { history } = React.useContext(LocationContext);
    const { url, title, theme = 'default', size = 'l', className, shouldHandleBackAction = false, onClick, tabIndex, extraProps, } = props;
    const handleAnalytics = useAnalytics(DefaultEventNames.ShareButton, url);
    const backActionHandler = React.useCallback(async () => {
        handleAnalytics();
        if (!history) {
            return;
        }
        if (onClick) {
            onClick();
        }
        if (history.length > 1) {
            history.goBack();
        }
        else {
            history.push({ pathname: url });
        }
    }, [handleAnalytics, history, onClick, url]);
    const buttonProps = {
        href: shouldHandleBackAction ? undefined : url,
        extraProps,
    };
    return (_jsxs(Button, { className: className, view: theme === 'special' ? 'flat-contrast' : 'flat-secondary', size: size, onClick: shouldHandleBackAction ? backActionHandler : undefined, tabIndex: tabIndex, ...buttonProps, children: [_jsx(Icon, { data: ArrowLeft, size: 20 }), _jsx("span", { children: title })] }));
}
//# sourceMappingURL=BackLink.js.map
