(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "tslib", "react", "../hooks/useDebounce", "../utils/getTextWidth", "./style", "./types", "./helpers"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var useDebounce_1 = (0, tslib_1.__importDefault)(require("../hooks/useDebounce"));
    var getTextWidth_1 = (0, tslib_1.__importDefault)(require("../utils/getTextWidth"));
    var style_1 = require("./style");
    var types_1 = require("./types");
    var helpers_1 = require("./helpers");
    var Button = (0, react_1.forwardRef)(function (_a, ref) {
        var children = _a.children, icon = _a.icon, _b = _a.viewType, viewType = _b === void 0 ? types_1.buttonViewTypeDefault : _b, isDisabled = _a.isDisabled, _c = _a.size, size = _c === void 0 ? types_1.buttonSizeDefault : _c, color = _a.color, width = _a.width, href = _a.href, className = _a.className, onClick = _a.onClick, _d = _a.testId, testId = _d === void 0 ? "button" : _d, isPressed = _a.isPressed, props = (0, tslib_1.__rest)(_a, ["children", "icon", "viewType", "isDisabled", "size", "color", "width", "href", "className", "onClick", "testId", "isPressed"]);
        var textButtonRef = (0, react_1.useRef)(null);
        var setButtonTitle = function () {
            if (textButtonRef.current) {
                var parent_1 = textButtonRef.current.parentNode;
                var parentWidth = (0, helpers_1.getWithBlock)(parent_1, Boolean(icon));
                var fontParent = getComputedStyle(parent_1).font;
                parent_1.setAttribute("title", "");
                if (typeof children === "string") {
                    var childrenWidth = (0, getTextWidth_1.default)(children, fontParent);
                    if (parentWidth < childrenWidth) {
                        parent_1.setAttribute("title", children);
                    }
                }
            }
        };
        var setButtonTitleDebounce = (0, useDebounce_1.default)(setButtonTitle, 500);
        (0, react_1.useEffect)(function () {
            window.addEventListener("resize", setButtonTitleDebounce);
            setButtonTitle();
            return function () { return window.removeEventListener("resize", setButtonTitleDebounce); };
        }, []);
        var handleClick = function (event) {
            onClick && !isDisabled && !href && onClick(event);
        };
        return (react_1.default.createElement(style_1.ButtonComponent, (0, tslib_1.__assign)({ isPressed: isPressed, className: className, viewType: viewType, disabled: isDisabled, size: size, as: viewType === "link" && href ? "a" : "button", href: href, "$color": color, width: width, onClick: handleClick, ref: ref, "$content": children, "data-testid": testId }, props),
            icon &&
                (0, react_1.cloneElement)(icon, { className: "lead-icon", width: 16, height: 16 }),
            typeof children === "string" ? (react_1.default.createElement("div", { className: "button-text", ref: textButtonRef }, children)) : (children)));
    });
    Button.displayName = "Button";
    exports.default = Button;
});
