import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import { ChevronRight } from '@gravity-ui/icons';
import { Icon } from '@gravity-ui/uikit';
import { LocaleContext } from "../../gravity-blocks/context/localeContext/index.js";
import { LocationContext } from "../../gravity-blocks/context/locationContext/index.js";
import { useAnalytics } from "../../gravity-blocks/hooks/index.js";
import { DefaultEventNames, } from "../../models/index.js";
import { block, getLinkProps, getQaAttrubutes, setUrlTld } from "../../utils/index.js";
import BackLink from "../BackLink/BackLink.js";
import FileLink from "../FileLink/FileLink.js";
import './Link.css';
const b = block('link-block');
const WORD_JOINER_SYM = '\u200b';
function getArrowSize(size) {
    switch (size) {
        case 'l':
            return 20;
        case 'm':
        case 'sm':
            return 18;
        case 's':
            return 14;
        default:
            return 14;
    }
}
function getTextSize({ size, theme }) {
    if (!size) {
        return theme === 'back' ? 'l' : 'm';
    }
    if (size === 'sm') {
        return 'm';
    }
    return size;
}
const Link = (props) => {
    const { text, url, arrow, analyticsEvents, theme = 'file-link', colorTheme = 'light', textSize = 'm', className, target, children, tabIndex, qa, urlTitle, extraProps, } = props;
    const qaAttributes = getQaAttrubutes(qa, ['normal']);
    const handleAnalytics = useAnalytics(DefaultEventNames.Link, url);
    const { hostname } = React.useContext(LocationContext);
    const { tld } = React.useContext(LocaleContext);
    const href = setUrlTld(props.url, tld);
    const onClick = () => {
        handleAnalytics(analyticsEvents);
    };
    const getLinkByType = () => {
        switch (theme) {
            case 'back':
                return (_jsx(BackLink, { title: children || text, url: href, onClick: onClick, tabIndex: tabIndex, extraProps: extraProps }));
            case 'file-link':
            case 'underline':
                return (_jsx(FileLink, { text: children || text, href: href, type: "horizontal", textSize: textSize, onClick: onClick, tabIndex: tabIndex, extraProps: extraProps }));
            case 'normal': {
                const linkProps = getLinkProps(url, hostname, target);
                const content = children || text;
                return (_jsx("a", { className: b('link', { theme: colorTheme, 'has-arrow': arrow }), href: href, onClick: onClick, tabIndex: tabIndex, title: urlTitle, ...linkProps, "data-qa": qaAttributes.normal, ...extraProps, children: arrow ? (_jsxs(React.Fragment, { children: [_jsx("span", { className: b('content'), children: content }), WORD_JOINER_SYM, _jsx(Icon, { className: b('arrow'), data: ChevronRight, size: getArrowSize(textSize) })] })) : (content) }));
            }
            default:
                return null;
        }
    };
    return (_jsx("div", { className: b({ size: getTextSize({ size: textSize, theme }) }, className), children: getLinkByType() }));
};
export default Link;
//# sourceMappingURL=Link.js.map
