"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolbarButtonView = void 0;
exports.ToolbarButton = ToolbarButton;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const uikit_1 = require("@gravity-ui/uikit");
const classname_1 = require("../classname.js");
const common_1 = require("../i18n/common/index.js");
const lodash_1 = require("../lodash.js");
const const_1 = require("./const.js");
require("./ToolbarButton.css");
const b = (0, classname_1.cn)('toolbar-button');
exports.ToolbarButtonView = (0, react_1.forwardRef)(function ToolbarButtonView({ title, hint, hotkey, hintWhenDisabled, active, enabled, onClick, className, ...props }, ref) {
    const disabled = !active && !enabled;
    const titleText = (0, lodash_1.isFunction)(title) ? title() : title;
    const hintText = (0, lodash_1.isFunction)(hint) ? hint() : hint;
    const hideHintWhenDisabled = hintWhenDisabled === false || !disabled;
    const hintWhenDisabledText = typeof hintWhenDisabled === 'string'
        ? hintWhenDisabled
        : typeof hintWhenDisabled === 'function'
            ? hintWhenDisabled()
            : (0, common_1.i18n)('toolbar_action_disabled');
    return ((0, jsx_runtime_1.jsx)(uikit_1.Popover, { content: (0, jsx_runtime_1.jsx)("div", { className: b('action-disabled-tooltip'), children: hintWhenDisabledText }), disabled: hideHintWhenDisabled, placement: ['bottom'], children: (_, refForPopover) => ((0, jsx_runtime_1.jsx)(uikit_1.ActionTooltip, { openDelay: const_1.ToolbarTooltipDelay.Open, closeDelay: const_1.ToolbarTooltipDelay.Close, description: hintText, title: titleText, hotkey: hotkey, children: (_, refForTooltip) => ((0, jsx_runtime_1.jsx)(uikit_1.Button, { size: "m", ref: (elem) => {
                    (0, uikit_1.setRef)(ref, elem);
                    (0, uikit_1.setRef)(refForPopover, elem);
                    (0, uikit_1.setRef)(refForTooltip, elem);
                }, selected: active, disabled: disabled, view: active ? 'normal' : 'flat', onClick: onClick, className: b(null, [className]), "aria-label": titleText, children: 'icon' in props ? ((0, jsx_runtime_1.jsx)(uikit_1.Icon, { data: props.icon.data, size: props.icon.size ?? 16 })) : (props.children) })) })) }));
});
function ToolbarButton(props) {
    const { id, editor, focus, isActive, isEnable, exec, onClick } = props;
    const active = isActive(editor);
    const enabled = isEnable(editor);
    return ((0, jsx_runtime_1.jsx)(exports.ToolbarButtonView, { ...props, active: active, enabled: enabled, onClick: () => {
            focus();
            exec(editor);
            onClick?.(id);
        } }));
}
//# sourceMappingURL=ToolbarButton.js.map
