import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import { StoreBadge } from '@gravity-ui/components';
import { Button as CommonButton, Icon, Platform, } from '@gravity-ui/uikit';
import { LocaleContext } from "../../gravity-blocks/context/localeContext/index.js";
import { useAnalytics } from "../../gravity-blocks/hooks/index.js";
import { Github } from "../../gravity-blocks/icons/index.js";
import { DefaultEventNames } from "../../models/index.js";
import { block, setUrlTld } from "../../utils/index.js";
import { getGravityIcon } from "../../utils/icons.js";
import { i18n } from "./i18n/index.js";
import { ICON_QA, toCommonSize, toCommonView } from "./utils.js";
import './Button.css';
const DEFAULT_IMG_SIZE = 16;
const b = block('button-block');
// eslint-disable-next-line complexity
const Button = (props) => {
    const { tld } = React.useContext(LocaleContext);
    const { className, analyticsEvents, size = 'l', theme = 'normal', url, urlTitle, img, onClick: onClickOrigin, text, width, ...rest } = props;
    const defaultImgPosition = 'left';
    const handleAnalytics = useAnalytics(DefaultEventNames.Button, url);
    const onClick = React.useCallback((e) => {
        handleAnalytics(analyticsEvents);
        if (onClickOrigin) {
            onClickOrigin(e);
        }
    }, [handleAnalytics, analyticsEvents, onClickOrigin]);
    const buttonModifiers = { size, theme, width };
    const buttonImg = img instanceof Object
        ? {
            url: img.url,
            iconData: img.iconData,
            iconSize: img.iconSize,
            className: img.className,
            position: img.position || defaultImgPosition,
            alt: img.alt,
        }
        : { url: img, position: defaultImgPosition };
    const { position, iconData, iconSize, className: iconClassName, url: imgUrl, alt } = buttonImg;
    const buttonClass = img
        ? b({ position, ...buttonModifiers }, className)
        : b({ ...buttonModifiers }, className);
    const buttonProps = {
        ...rest,
        href: url ? setUrlTld(url, tld) : undefined,
        onClick,
    };
    if (theme === 'app-store' || theme === 'google-play') {
        const platform = theme === 'app-store' ? Platform.IOS : Platform.ANDROID;
        return _jsx(StoreBadge, { className: buttonClass, platform: platform, href: url });
    }
    let icon;
    let image;
    if (img && iconData) {
        let finalIconData = iconData;
        if (typeof iconData === 'string') {
            const gravityIcon = getGravityIcon(iconData);
            if (gravityIcon) {
                finalIconData = gravityIcon;
            }
        }
        const finalIconClassName = iconClassName ? b('icon', iconClassName) : b('icon');
        icon = (_jsx(Icon, { className: finalIconClassName, data: finalIconData, size: iconSize, qa: ICON_QA }));
    }
    else if (img && imgUrl) {
        image = (_jsx("img", { className: b('image'), width: iconSize || DEFAULT_IMG_SIZE, height: iconSize || DEFAULT_IMG_SIZE, src: imgUrl, alt: alt || i18n('image-alt') }));
    }
    // Special handling for github theme (for backwards compatibility)
    if (theme === 'github') {
        icon = _jsx(Icon, { className: b('icon'), data: Github, size: 24, qa: ICON_QA });
        image = undefined;
    }
    const buttonTheme = theme === 'scale' ? 'accent' : theme;
    return (_jsxs(CommonButton, { className: buttonClass, view: toCommonView(buttonTheme), size: toCommonSize(size), title: urlTitle, width: width, ...buttonProps, children: [icon && position === 'left' ? icon : null, _jsxs("span", { className: b('content'), children: [image && position === 'left' ? image : null, _jsx("span", { className: b('text'), children: text }), image && position === 'right' ? image : null] }), icon && position === 'right' ? icon : null] }));
};
export default Button;
//# sourceMappingURL=Button.js.map
