/**
 * DevExtreme (renovation/ui/scroll_view/scrollable_utils.js)
 * Version: 21.1.0 (build 21016-0316)
 * Build date: Sat Jan 16 2021
 *
 * Copyright (c) 2012 - 2021 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.ensureLocation = ensureLocation;
exports.getBoundaryProps = getBoundaryProps;
exports.getContainerOffsetInternal = getContainerOffsetInternal;
exports.normalizeCoordinate = normalizeCoordinate;
exports.getPublicCoordinate = getPublicCoordinate;
exports.getElementLocation = getElementLocation;
exports.ScrollDirection = exports.SCROLLABLE_SCROLLBARS_ALWAYSVISIBLE = exports.SCROLLABLE_SCROLLBARS_HIDDEN = exports.SCROLLABLE_SCROLLBAR_SIMULATED = exports.SCROLLABLE_DISABLED_CLASS = exports.SCROLLVIEW_BOTTOM_POCKET_CLASS = exports.SCROLLVIEW_CONTENT_CLASS = exports.SCROLLABLE_CONTAINER_CLASS = exports.SCROLLABLE_WRAPPER_CLASS = exports.SCROLLABLE_CONTENT_CLASS = exports.DIRECTION_BOTH = exports.DIRECTION_HORIZONTAL = exports.DIRECTION_VERTICAL = void 0;
var _type = require("../../../core/utils/type");
var _scroll_rtl_behavior = _interopRequireDefault(require("../../../core/utils/scroll_rtl_behavior"));
var _inflector = require("../../../core/utils/inflector");

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}

function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function")
    }
}

function _defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) {
            descriptor.writable = true
        }
        Object.defineProperty(target, descriptor.key, descriptor)
    }
}

function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps) {
        _defineProperties(Constructor.prototype, protoProps)
    }
    if (staticProps) {
        _defineProperties(Constructor, staticProps)
    }
    return Constructor
}

function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter((function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable
            }))
        }
        keys.push.apply(keys, symbols)
    }
    return keys
}

function _objectSpread(target) {
    for (var i = 1; i < arguments.length; i++) {
        var source = null != arguments[i] ? arguments[i] : {};
        if (i % 2) {
            ownKeys(Object(source), true).forEach((function(key) {
                _defineProperty(target, key, source[key])
            }))
        } else if (Object.getOwnPropertyDescriptors) {
            Object.defineProperties(target, Object.getOwnPropertyDescriptors(source))
        } else {
            ownKeys(Object(source)).forEach((function(key) {
                Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key))
            }))
        }
    }
    return target
}

function _defineProperty(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        })
    } else {
        obj[key] = value
    }
    return obj
}
var DIRECTION_VERTICAL = "vertical";
exports.DIRECTION_VERTICAL = DIRECTION_VERTICAL;
var DIRECTION_HORIZONTAL = "horizontal";
exports.DIRECTION_HORIZONTAL = DIRECTION_HORIZONTAL;
var DIRECTION_BOTH = "both";
exports.DIRECTION_BOTH = DIRECTION_BOTH;
var SCROLLABLE_CONTENT_CLASS = "dx-scrollable-content";
exports.SCROLLABLE_CONTENT_CLASS = SCROLLABLE_CONTENT_CLASS;
var SCROLLABLE_WRAPPER_CLASS = "dx-scrollable-wrapper";
exports.SCROLLABLE_WRAPPER_CLASS = SCROLLABLE_WRAPPER_CLASS;
var SCROLLABLE_CONTAINER_CLASS = "dx-scrollable-container";
exports.SCROLLABLE_CONTAINER_CLASS = SCROLLABLE_CONTAINER_CLASS;
var SCROLLVIEW_CONTENT_CLASS = "dx-scrollview-content";
exports.SCROLLVIEW_CONTENT_CLASS = SCROLLVIEW_CONTENT_CLASS;
var SCROLLVIEW_BOTTOM_POCKET_CLASS = "dx-scrollview-bottom-pocket";
exports.SCROLLVIEW_BOTTOM_POCKET_CLASS = SCROLLVIEW_BOTTOM_POCKET_CLASS;
var SCROLLABLE_DISABLED_CLASS = "dx-scrollable-disabled";
exports.SCROLLABLE_DISABLED_CLASS = SCROLLABLE_DISABLED_CLASS;
var SCROLLABLE_SCROLLBAR_SIMULATED = "dx-scrollable-scrollbar-simulated";
exports.SCROLLABLE_SCROLLBAR_SIMULATED = SCROLLABLE_SCROLLBAR_SIMULATED;
var SCROLLABLE_SCROLLBARS_HIDDEN = "dx-scrollable-scrollbars-hidden";
exports.SCROLLABLE_SCROLLBARS_HIDDEN = SCROLLABLE_SCROLLBARS_HIDDEN;
var SCROLLABLE_SCROLLBARS_ALWAYSVISIBLE = "dx-scrollable-scrollbars-alwaysvisible";
exports.SCROLLABLE_SCROLLBARS_ALWAYSVISIBLE = SCROLLABLE_SCROLLBARS_ALWAYSVISIBLE;

function ensureLocation(location) {
    if ((0, _type.isNumeric)(location)) {
        return {
            left: location,
            top: location
        }
    }
    return _objectSpread({
        top: 0,
        left: 0
    }, location)
}

function getRelativeLocation(element) {
    var result = {
        top: 0,
        left: 0
    };
    var targetElement = element;
    while (!targetElement.matches(".".concat(SCROLLABLE_CONTENT_CLASS))) {
        result.top += targetElement.offsetTop;
        result.left += targetElement.offsetLeft;
        targetElement = targetElement.offsetParent
    }
    return result
}
var ScrollDirection = function() {
    function ScrollDirection(direction) {
        _classCallCheck(this, ScrollDirection);
        this.DIRECTION_HORIZONTAL = "horizontal";
        this.DIRECTION_VERTICAL = "vertical";
        this.DIRECTION_BOTH = "both";
        this.direction = direction
    }
    _createClass(ScrollDirection, [{
        key: "isHorizontal",
        get: function() {
            return this.direction === DIRECTION_HORIZONTAL || this.direction === DIRECTION_BOTH
        }
    }, {
        key: "isVertical",
        get: function() {
            return this.direction === DIRECTION_VERTICAL || this.direction === DIRECTION_BOTH
        }
    }]);
    return ScrollDirection
}();
exports.ScrollDirection = ScrollDirection;

function getMaxScrollOffset(dimension, containerRef) {
    return containerRef["scroll".concat((0, _inflector.camelize)(dimension, true))] - containerRef["client".concat((0, _inflector.camelize)(dimension, true))]
}

function getBoundaryProps(direction, scrollOffset, containerRef) {
    var pushBackValue = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : 0;
    var left = scrollOffset.left,
        top = scrollOffset.top;
    var boundaryProps = {};
    var _ScrollDirection = new ScrollDirection(direction),
        isHorizontal = _ScrollDirection.isHorizontal,
        isVertical = _ScrollDirection.isVertical;
    if (isHorizontal) {
        boundaryProps.reachedLeft = left <= 0;
        boundaryProps.reachedRight = Math.round(left) >= getMaxScrollOffset("width", containerRef)
    }
    if (isVertical) {
        boundaryProps.reachedTop = top <= 0;
        boundaryProps.reachedBottom = top >= getMaxScrollOffset("height", containerRef) - 2 * pushBackValue
    }
    return boundaryProps
}

function getContainerOffsetInternal(containerRef) {
    return {
        left: containerRef.scrollLeft,
        top: containerRef.scrollTop
    }
}

function getScrollBarSize(dimension, containerRef) {
    return containerRef["offset".concat(dimension)] - containerRef["client".concat(dimension)]
}

function needNormalizeCoordinate(prop, rtlEnabled) {
    return true === rtlEnabled && "left" === prop
}

function normalizeCoordinate(prop, coordinate, rtlEnabled) {
    return needNormalizeCoordinate(prop, rtlEnabled) && (0, _scroll_rtl_behavior.default)().positive ? -1 * coordinate : coordinate
}

function getPublicCoordinate(prop, coordinate, containerRef, rtlEnabled) {
    return needNormalizeCoordinate(prop, rtlEnabled) ? getMaxScrollOffset("width", containerRef) + normalizeCoordinate(prop, coordinate, rtlEnabled) : coordinate
}

function getElementLocationInternal(element, prop, offset, direction, containerRef, rtlEnabled) {
    var dimension = direction === DIRECTION_VERTICAL ? "Height" : "Width";
    var relativeLocation = getRelativeLocation(element)[prop];
    var scrollBarSize = getScrollBarSize(dimension, containerRef);
    var containerSize = containerRef["offset".concat(dimension)];
    var elementOffset = element["offset".concat(dimension)];
    var offsetStart = offset[prop];
    var offsetEnd = offset[direction === DIRECTION_VERTICAL ? "bottom" : "right"];
    var containerLocation = normalizeCoordinate(prop, getContainerOffsetInternal(containerRef)[prop], rtlEnabled);
    if (relativeLocation < containerLocation + offsetStart) {
        if (elementOffset < containerSize - offsetStart - offsetEnd) {
            return relativeLocation - offsetStart
        }
        return relativeLocation + elementOffset - containerSize + offsetEnd + scrollBarSize
    }
    if (relativeLocation + elementOffset >= containerLocation + containerSize - offsetEnd - scrollBarSize) {
        if (elementOffset < containerSize - offsetStart - offsetEnd) {
            return relativeLocation + elementOffset + scrollBarSize - containerSize + offsetEnd
        }
        return relativeLocation - offsetStart
    }
    return containerLocation
}

function getElementLocation(element, offset, direction, containerRef, rtlEnabled) {
    var prop = direction === DIRECTION_VERTICAL ? "top" : "left";
    var location = normalizeCoordinate(prop, getElementLocationInternal(element, prop, offset, direction, containerRef, rtlEnabled), rtlEnabled);
    return getPublicCoordinate(prop, location, containerRef, rtlEnabled)
}
