const copy = (text, callback) => {
    navigator.clipboard.writeText(text).then(() => {
        callback && callback();
    });
};
const attachEnter = (event, handleEnter) => {
    // 'keypress' event misbehaves on mobile so we track 'Enter' key via 'keydown' event
    if (event.key === 'Enter') {
        event.preventDefault();
        event.stopPropagation();
        handleEnter();
    }
};
const goToAnchor = (anchorId, e) => {
    e && e.preventDefault();
    const anchorElement = document.getElementById(anchorId);
    if (anchorElement) {
        anchorElement.scrollIntoView({ behavior: 'smooth' });
    }
};
const attachCloseToEscape = (handleClose) => {
    const closeOnEscape = (e) => {
        if (e.keyCode === 27)
            handleClose();
    };
    window.addEventListener('keydown', closeOnEscape);
    return () => window.removeEventListener('keydown', closeOnEscape);
};
const executeOnEnter = (event, fn) => {
    if (event.key === 'Enter') {
        event.preventDefault();
        event.stopPropagation();
        fn();
    }
};
const isMobile = () => window.innerWidth < 768;
const limitText = (text, limit) => text && text.length > limit ? text.slice(0, limit) + '...' : text;
const UsabilityUtils = {
    attachCloseToEscape,
    executeOnEnter,
    isMobile,
    goToAnchor,
    attachEnter,
    copy,
    limitText,
};
export default UsabilityUtils;
