var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import React from 'react';
import { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { ThemeFactory } from '../../lib/theming/ThemeFactory.js';
import { DUMMY_LOCATION, Popup } from '../../internal/Popup/index.js';
import { isTestEnv } from '../../lib/currentEnvironment.js';
import { CommonWrapper } from '../../internal/CommonWrapper/index.js';
import { rootNode } from '../../lib/rootNode/index.js';
import { createPropsGetter } from '../../lib/createPropsGetter.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
import { getStyles } from './Hint.styles.js';
var HINT_BORDER_COLOR = 'transparent';
/**
 * Всплывающая подсказка `Hint`.
 * По умолчанию отображается при наведении на элемент, но можно задать другие условия отображения.
 *
 * Для подсказки, всплывающей по клику используйте Tooltip.
 */
var Hint = /** @class */ (function (_super) {
    __extends(Hint, _super);
    function Hint() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.getProps = createPropsGetter(Hint_1.defaultProps);
        _this.state = {
            opened: _this.getProps().manual ? !!_this.getProps().opened : false,
            position: DUMMY_LOCATION.position,
        };
        _this.popupRef = React.createRef();
        _this.getAnchorElement = function () {
            var _a;
            return (_a = _this.popupRef.current) === null || _a === void 0 ? void 0 : _a.anchorElement;
        };
        _this.getPositions = function () {
            return _this.props.allowedPositions;
        };
        _this.handleMouseEnter = function (e) {
            if (!_this.getProps().manual && !_this.timer) {
                _this.timer = setTimeout(_this.open, 400);
            }
            if (_this.props.onMouseEnter) {
                _this.props.onMouseEnter(e);
            }
        };
        _this.handleMouseLeave = function (e) {
            if (!_this.getProps().manual && _this.timer) {
                clearTimeout(_this.timer);
                _this.timer = null;
                _this.setState({ opened: false });
            }
            if (_this.props.onMouseLeave) {
                _this.props.onMouseLeave(e);
            }
        };
        _this.open = function () {
            _this.setState({ opened: true });
        };
        return _this;
    }
    Hint_1 = Hint;
    Hint.prototype.getAllowedPositions = function () {
        return this.props.allowedPositions;
    };
    Hint.prototype.componentDidUpdate = function (prevProps) {
        var _a = this.getProps(), opened = _a.opened, manual = _a.manual;
        if (!manual) {
            return;
        }
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = null;
        }
        if (opened !== prevProps.opened) {
            this.setState({ opened: !!opened });
        }
    };
    Hint.prototype.componentWillUnmount = function () {
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = null;
        }
    };
    Hint.prototype.render = function () {
        var _this = this;
        this.styles = getStyles(this.emotion);
        return (React.createElement(ThemeContext.Consumer, null, function (theme) {
            _this.theme = theme;
            return (React.createElement(ThemeContext.Provider, { value: ThemeFactory.create({
                    popupMargin: theme.hintMargin,
                    popupBorder: theme.hintBorder,
                    popupBorderRadius: theme.hintBorderRadius,
                }, _this.theme) }, _this.renderMain()));
        }));
    };
    Hint.prototype.renderMain = function () {
        var _this = this;
        var _a = this.getProps(), disableAnimations = _a.disableAnimations, useWrapper = _a.useWrapper;
        return (React.createElement(CommonWrapper, __assign({ rootNodeRef: this.setRootNode }, this.props),
            React.createElement(Popup, { hasPin: false, opened: this.state.opened, anchorElement: this.props.children, positions: this.getPositions(), pos: this.props.pos, backgroundColor: this.theme.hintBgColor, borderColor: HINT_BORDER_COLOR, onPositionChange: function (position) { return _this.setState({ position: position }); }, disableAnimations: disableAnimations, onMouseEnter: this.handleMouseEnter, onMouseLeave: this.handleMouseLeave, useWrapper: useWrapper, ref: this.popupRef, withoutMobile: true }, this.renderContent())));
    };
    Hint.prototype.renderContent = function () {
        var _a;
        if (!this.props.text) {
            return null;
        }
        var maxWidth = this.getProps().maxWidth;
        var centerAlignPositions = ['top', 'top center', 'bottom', 'bottom center'];
        var className = this.cx((_a = {},
            _a[this.styles.content(this.theme)] = true,
            _a[this.styles.contentCenter(this.theme)] = centerAlignPositions.includes(this.state.position),
            _a));
        return (React.createElement("div", { className: className, style: { maxWidth: maxWidth } }, this.props.text));
    };
    var Hint_1;
    Hint.__KONTUR_REACT_UI__ = 'Hint';
    Hint.displayName = 'Hint';
    Hint.defaultProps = {
        manual: false,
        opened: false,
        maxWidth: 200,
        disableAnimations: isTestEnv,
        useWrapper: false,
    };
    Hint = Hint_1 = __decorate([
        withRenderEnvironment,
        rootNode
    ], Hint);
    return Hint;
}(React.PureComponent));
export { Hint };
//# sourceMappingURL=Hint.js.map