"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 components_1 = require("@gravity-ui/components");
const uikit_1 = require("@gravity-ui/uikit");
const localeContext_1 = require("../../gravity-blocks/context/localeContext/index.js");
const hooks_1 = require("../../gravity-blocks/hooks/index.js");
const icons_1 = require("../../gravity-blocks/icons/index.js");
const models_1 = require("../../models/index.js");
const utils_1 = require("../../utils/index.js");
const icons_2 = require("../../utils/icons.js");
const i18n_1 = require("./i18n/index.js");
const utils_2 = require("./utils.js");
const DEFAULT_IMG_SIZE = 16;
const b = (0, utils_1.block)('button-block');
// eslint-disable-next-line complexity
const Button = (props) => {
    const { tld } = React.useContext(localeContext_1.LocaleContext);
    const { className, analyticsEvents, size = 'l', theme = 'normal', url, urlTitle, img, onClick: onClickOrigin, text, width, ...rest } = props;
    const defaultImgPosition = 'left';
    const handleAnalytics = (0, hooks_1.useAnalytics)(models_1.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 ? (0, utils_1.setUrlTld)(url, tld) : undefined,
        onClick,
    };
    if (theme === 'app-store' || theme === 'google-play') {
        const platform = theme === 'app-store' ? uikit_1.Platform.IOS : uikit_1.Platform.ANDROID;
        return (0, jsx_runtime_1.jsx)(components_1.StoreBadge, { className: buttonClass, platform: platform, href: url });
    }
    let icon;
    let image;
    if (img && iconData) {
        let finalIconData = iconData;
        if (typeof iconData === 'string') {
            const gravityIcon = (0, icons_2.getGravityIcon)(iconData);
            if (gravityIcon) {
                finalIconData = gravityIcon;
            }
        }
        const finalIconClassName = iconClassName ? b('icon', iconClassName) : b('icon');
        icon = ((0, jsx_runtime_1.jsx)(uikit_1.Icon, { className: finalIconClassName, data: finalIconData, size: iconSize, qa: utils_2.ICON_QA }));
    }
    else if (img && imgUrl) {
        image = ((0, jsx_runtime_1.jsx)("img", { className: b('image'), width: iconSize || DEFAULT_IMG_SIZE, height: iconSize || DEFAULT_IMG_SIZE, src: imgUrl, alt: alt || (0, i18n_1.i18n)('image-alt') }));
    }
    // Special handling for github theme (for backwards compatibility)
    if (theme === 'github') {
        icon = (0, jsx_runtime_1.jsx)(uikit_1.Icon, { className: b('icon'), data: icons_1.Github, size: 24, qa: utils_2.ICON_QA });
        image = undefined;
    }
    const buttonTheme = theme === 'scale' ? 'accent' : theme;
    return ((0, jsx_runtime_1.jsxs)(uikit_1.Button, { className: buttonClass, view: (0, utils_2.toCommonView)(buttonTheme), size: (0, utils_2.toCommonSize)(size), title: urlTitle, width: width, ...buttonProps, children: [icon && position === 'left' ? icon : null, (0, jsx_runtime_1.jsxs)("span", { className: b('content'), children: [image && position === 'left' ? image : null, (0, jsx_runtime_1.jsx)("span", { className: b('text'), children: text }), image && position === 'right' ? image : null] }), icon && position === 'right' ? icon : null] }));
};
exports.default = Button;
//# sourceMappingURL=Button.js.map
