var DEFAULT_DURATION = 300;
// https://css-tricks.com/emulating-css-timing-functions-javascript/
var tfs = {
    linear: function linear(t) {
        return t;
    },
    // eslint-disable-next-line
    easeIn: function easeIn(t) {
        return Math.pow(t, 1.675);
    },
    // eslint-disable-next-line
    easeOut: function easeOut(t) {
        return 1 - Math.pow(1 - t, 1.675);
    },
    easeInOut: function easeInOut(t) {
        return 0.5 * (Math.sin((t - 0.5) * Math.PI) + 1);
    }
};
/**
 * Плавная прокрутка по горизонтали
 * @param {Element} elem
 * @param {number} pos
 * @param {number} duration
 * @param {string} timingFunction
 */ export var animatedScrollToX = function animatedScrollToX(elem, pos) {
    var duration = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : DEFAULT_DURATION, timingFunction = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : 'easeInOut';
    var startTime;
    var startX = elem.scrollLeft;
    var endX = Math.max(0, Math.min(elem.scrollWidth - elem.clientWidth, pos));
    var handleNewAnimationFrame = function handleNewAnimationFrame1() {
        startTime = startTime || Date.now();
        var timePos = Math.min(1, Math.max(1, Date.now() - startTime) / duration);
        var scrollPos = tfs[timingFunction](timePos);
        var left = startX + (endX - startX) * scrollPos;
        elem.scrollTo({
            left: left
        });
        if (timePos !== 1) window.requestAnimationFrame(handleNewAnimationFrame);
    };
    window.requestAnimationFrame(handleNewAnimationFrame);
};
/**
 * Плавная прокрутка по вертикали
 * @param {Element} elem
 * @param {number} pos
 * @param {number} duration
 * @param {string} timingFunction
 */ export var animatedScrollToY = function animatedScrollToY(elem, pos) {
    var duration = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : DEFAULT_DURATION, timingFunction = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : 'easeInOut';
    var startTime;
    var startY = elem.scrollTop;
    var endY = Math.max(0, Math.min(elem.scrollHeight - elem.clientHeight, pos));
    var handleNewAnimationFrame = function handleNewAnimationFrame1() {
        startTime = startTime || Date.now();
        var timePos = Math.min(1, Math.max(1, Date.now() - startTime) / duration);
        var scrollPos = tfs[timingFunction](timePos);
        var top = startY + (endY - startY) * scrollPos;
        elem.scrollTo({
            top: top
        });
        if (timePos !== 1) window.requestAnimationFrame(handleNewAnimationFrame);
    };
    window.requestAnimationFrame(handleNewAnimationFrame);
};
