"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
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 localeContext_1 = require("../../gravity-blocks/context/localeContext/index.js");
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");
const utils_1 = require("../../utils/index.js");
const BackLink_1 = tslib_1.__importDefault(require("../BackLink/BackLink.js"));
const FileLink_1 = tslib_1.__importDefault(require("../FileLink/FileLink.js"));
const b = (0, utils_1.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 = (0, utils_1.getQaAttrubutes)(qa, ['normal']);
    const handleAnalytics = (0, hooks_1.useAnalytics)(models_1.DefaultEventNames.Link, url);
    const { hostname } = React.useContext(locationContext_1.LocationContext);
    const { tld } = React.useContext(localeContext_1.LocaleContext);
    const href = (0, utils_1.setUrlTld)(props.url, tld);
    const onClick = () => {
        handleAnalytics(analyticsEvents);
    };
    const getLinkByType = () => {
        switch (theme) {
            case 'back':
                return ((0, jsx_runtime_1.jsx)(BackLink_1.default, { title: children || text, url: href, onClick: onClick, tabIndex: tabIndex, extraProps: extraProps }));
            case 'file-link':
            case 'underline':
                return ((0, jsx_runtime_1.jsx)(FileLink_1.default, { text: children || text, href: href, type: "horizontal", textSize: textSize, onClick: onClick, tabIndex: tabIndex, extraProps: extraProps }));
            case 'normal': {
                const linkProps = (0, utils_1.getLinkProps)(url, hostname, target);
                const content = children || text;
                return ((0, jsx_runtime_1.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 ? ((0, jsx_runtime_1.jsxs)(React.Fragment, { children: [(0, jsx_runtime_1.jsx)("span", { className: b('content'), children: content }), WORD_JOINER_SYM, (0, jsx_runtime_1.jsx)(uikit_1.Icon, { className: b('arrow'), data: icons_1.ChevronRight, size: getArrowSize(textSize) })] })) : (content) }));
            }
            default:
                return null;
        }
    };
    return ((0, jsx_runtime_1.jsx)("div", { className: b({ size: getTextSize({ size: textSize, theme }) }, className), children: getLinkByType() }));
};
exports.default = Link;
//# sourceMappingURL=Link.js.map
