"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = BackLink;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const React = tslib_1.__importStar(require("react"));
const icons_1 = require("@gravity-ui/icons");
const uikit_1 = require("@gravity-ui/uikit");
const locationContext_1 = require("../../gravity-blocks/context/locationContext/index.js");
const hooks_1 = require("../../gravity-blocks/hooks/index.js");
const models_1 = require("../../models/index.js");
function BackLink(props) {
    const { history } = React.useContext(locationContext_1.LocationContext);
    const { url, title, theme = 'default', size = 'l', className, shouldHandleBackAction = false, onClick, tabIndex, extraProps, } = props;
    const handleAnalytics = (0, hooks_1.useAnalytics)(models_1.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 ((0, jsx_runtime_1.jsxs)(uikit_1.Button, { className: className, view: theme === 'special' ? 'flat-contrast' : 'flat-secondary', size: size, onClick: shouldHandleBackAction ? backActionHandler : undefined, tabIndex: tabIndex, ...buttonProps, children: [(0, jsx_runtime_1.jsx)(uikit_1.Icon, { data: icons_1.ArrowLeft, size: 20 }), (0, jsx_runtime_1.jsx)("span", { children: title })] }));
}
//# sourceMappingURL=BackLink.js.map
