"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const React = __importStar(require("react"));
const react_hover_observer_1 = __importDefault(require("react-hover-observer"));
const react_jss_1 = __importDefault(require("react-jss"));
const react_popper_1 = require("react-popper");
const classNames = require("classnames");
const styles = {
    hint: {
        width: 137,
        margin: [0, 0, 0, 2],
        padding: [5, 10, 6],
        fontSize: 11,
        fontWeight: 700,
        textAlign: 'center',
        color: '#FFF',
        backgroundColor: '#333',
        borderRadius: 2,
    },
    alternate: {
        borderRadius: 12,
        backgroundImage: ((props) => props.themeGradient || 'initial'),
    },
};
class TooltipImpl extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            isHovering: false,
            tooltipShown: false,
        };
        this.handleHoverChanged = this.handleHoverChanged.bind(this);
    }
    componentWillReceiveProps(nextProps) {
        const tooltipShown = Boolean(nextProps.tooltip && !this.props.tooltip);
        if (tooltipShown) {
            this.setState({ tooltipShown: true });
            return;
        }
        if (!tooltipShown && !this.state.isHovering) {
            this.setState({ tooltipShown: false });
            return;
        }
    }
    handleHoverChanged({ isHovering }) {
        this.setState({ isHovering });
        if (!isHovering && this.state.tooltipShown) {
            this.setState({ isHovering: false, tooltipShown: false });
        }
    }
    render() {
        const { children, tooltip, placement, classes, offset, hintClassname, alternate } = this.props;
        return (React.createElement(react_popper_1.Manager, { className: this.props.className },
            React.createElement(react_popper_1.Target, null,
                React.createElement(react_hover_observer_1.default, { hoverDelayInMs: 500, onHoverChanged: this.handleHoverChanged, shouldDecorateChildren: false }, children)),
            !this.state.tooltipShown &&
                tooltip &&
                this.state.isHovering && (React.createElement(react_popper_1.Popper, { placement: placement, modifiers: {
                    preventOverflow: { enabled: true, boundariesElement: 'viewport' },
                    offset: { offset },
                } },
                React.createElement("div", { className: classNames(classes.hint, hintClassname, { [classes.alternate]: alternate }) }, tooltip)))));
    }
}
TooltipImpl.defaultProps = {
    placement: 'right',
    offset: '0, 2',
};
exports.Tooltip = react_jss_1.default(styles)(TooltipImpl);
//# sourceMappingURL=index.js.map