/**
 * DevExtreme (renovation/ui/scroll_view/scrollable_simulated.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";

function _typeof(obj) {
    if ("function" === typeof Symbol && "symbol" === typeof Symbol.iterator) {
        _typeof = function(obj) {
            return typeof obj
        }
    } else {
        _typeof = function(obj) {
            return obj && "function" === typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
        }
    }
    return _typeof(obj)
}
exports.ScrollableSimulated = exports.viewFunction = void 0;
var _subscribe_to_event = require("../../utils/subscribe_to_event");
var _scrollbar = require("./scrollbar");
var _widget = require("../common/widget");
var _combine_classes = require("../../utils/combine_classes");
var _scrollable_props = require("./scrollable_props");
var _scrollable_utils = require("./scrollable_utils");
var _topPocket = require("./topPocket");
var _bottomPocket = require("./bottomPocket");
var _short = require("../../../events/short");
var Preact = _interopRequireWildcard(require("preact"));
var _hooks = require("preact/hooks");
var _compat = require("preact/compat");

function _getRequireWildcardCache() {
    if ("function" !== typeof WeakMap) {
        return null
    }
    var cache = new WeakMap;
    _getRequireWildcardCache = function() {
        return cache
    };
    return cache
}

function _interopRequireWildcard(obj) {
    if (obj && obj.__esModule) {
        return obj
    }
    if (null === obj || "object" !== _typeof(obj) && "function" !== typeof obj) {
        return {
            default: obj
        }
    }
    var cache = _getRequireWildcardCache();
    if (cache && cache.has(obj)) {
        return cache.get(obj)
    }
    var newObj = {};
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc)
            } else {
                newObj[key] = obj[key]
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj)
    }
    return newObj
}

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 _objectWithoutProperties(source, excluded) {
    if (null == source) {
        return {}
    }
    var target = _objectWithoutPropertiesLoose(source, excluded);
    var key, i;
    if (Object.getOwnPropertySymbols) {
        var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
        for (i = 0; i < sourceSymbolKeys.length; i++) {
            key = sourceSymbolKeys[i];
            if (excluded.indexOf(key) >= 0) {
                continue
            }
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) {
                continue
            }
            target[key] = source[key]
        }
    }
    return target
}

function _objectWithoutPropertiesLoose(source, excluded) {
    if (null == source) {
        return {}
    }
    var target = {};
    var sourceKeys = Object.keys(source);
    var key, i;
    for (i = 0; i < sourceKeys.length; i++) {
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) {
            continue
        }
        target[key] = 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
}

function _extends() {
    _extends = Object.assign || function(target) {
        for (var i = 1; i < arguments.length; i++) {
            var source = arguments[i];
            for (var key in source) {
                if (Object.prototype.hasOwnProperty.call(source, key)) {
                    target[key] = source[key]
                }
            }
        }
        return target
    };
    return _extends.apply(this, arguments)
}

function visibilityModeNormalize(mode) {
    if (true === mode) {
        return "onScroll"
    }
    return false === mode ? "never" : mode
}
var viewFunction = function(viewModel) {
    var containerRef = viewModel.containerRef,
        contentRef = viewModel.contentRef,
        cssClasses = viewModel.cssClasses,
        _viewModel$props = viewModel.props,
        children = _viewModel$props.children,
        direction = _viewModel$props.direction,
        disabled = _viewModel$props.disabled,
        forceGeneratePockets = _viewModel$props.forceGeneratePockets,
        height = _viewModel$props.height,
        needScrollViewContentWrapper = _viewModel$props.needScrollViewContentWrapper,
        pulledDownText = _viewModel$props.pulledDownText,
        pullingDownText = _viewModel$props.pullingDownText,
        reachBottomText = _viewModel$props.reachBottomText,
        refreshingText = _viewModel$props.refreshingText,
        rtlEnabled = _viewModel$props.rtlEnabled,
        scrollByThumb = _viewModel$props.scrollByThumb,
        showScrollbar = _viewModel$props.showScrollbar,
        width = _viewModel$props.width,
        restAttributes = viewModel.restAttributes,
        tabIndex = viewModel.tabIndex,
        wrapperRef = viewModel.wrapperRef;
    var targetDirection = null !== direction && void 0 !== direction ? direction : "vertical";
    var isVertical = "horizontal" !== targetDirection;
    var isHorizontal = "vertical" !== targetDirection;
    var visibilityMode = visibilityModeNormalize(showScrollbar);
    return Preact.h(_widget.Widget, _extends({
        classes: cssClasses,
        disabled: disabled,
        rtlEnabled: rtlEnabled,
        height: height,
        width: width
    }, restAttributes), Preact.h("div", {
        className: _scrollable_utils.SCROLLABLE_WRAPPER_CLASS,
        ref: wrapperRef
    }, Preact.h("div", {
        className: _scrollable_utils.SCROLLABLE_CONTAINER_CLASS,
        tabIndex: tabIndex,
        ref: containerRef
    }, Preact.h("div", {
        className: _scrollable_utils.SCROLLABLE_CONTENT_CLASS,
        ref: contentRef
    }, forceGeneratePockets && Preact.h(_topPocket.TopPocket, {
        pullingDownText: pullingDownText,
        pulledDownText: pulledDownText,
        refreshingText: refreshingText,
        refreshStrategy: "simulated"
    }), needScrollViewContentWrapper && Preact.h("div", {
        className: _scrollable_utils.SCROLLVIEW_CONTENT_CLASS
    }, children), !needScrollViewContentWrapper && children, forceGeneratePockets && Preact.h(_bottomPocket.BottomPocket, {
        reachBottomText: reachBottomText
    })), isHorizontal && Preact.h(_scrollbar.Scrollbar, {
        direction: "horizontal",
        visible: scrollByThumb,
        visibilityMode: visibilityMode,
        expandable: scrollByThumb
    }), isVertical && Preact.h(_scrollbar.Scrollbar, {
        direction: "vertical",
        visible: scrollByThumb,
        visibilityMode: visibilityMode,
        expandable: scrollByThumb
    }))))
};
exports.viewFunction = viewFunction;
var ScrollableSimulated = (0, _compat.forwardRef)((function(props, ref) {
    var __wrapperRef = (0, _hooks.useRef)();
    var __contentRef = (0, _hooks.useRef)();
    var __containerRef = (0, _hooks.useRef)();
    var __initHandler = (0, _hooks.useCallback)((function(event) {
        console.log("initHandler", event, this)
    }), []);
    var __handleStart = (0, _hooks.useCallback)((function(event) {}), []);
    var __handleMove = (0, _hooks.useCallback)((function(event) {}), []);
    var __handleEnd = (0, _hooks.useCallback)((function(event) {}), []);
    var __handleStop = (0, _hooks.useCallback)((function(event) {}), []);
    var __handleCancel = (0, _hooks.useCallback)((function(event) {}), []);
    var __getDirection = (0, _hooks.useCallback)((function(event) {
        return "vertical"
    }), []);
    var __validate = (0, _hooks.useCallback)((function(event) {
        return true
    }), []);
    var __cssClasses = (0, _hooks.useCallback)((function() {
        var _classesMap;
        var classes = props.classes,
            direction = props.direction,
            disabled = props.disabled,
            showScrollbar = props.showScrollbar;
        var classesMap = (_classesMap = {
            "dx-scrollable dx-scrollable-simulated dx-scrollable-renovated": true
        }, _defineProperty(_classesMap, "dx-scrollable-".concat(direction), true), _defineProperty(_classesMap, _scrollable_utils.SCROLLABLE_DISABLED_CLASS, !!disabled), _defineProperty(_classesMap, _scrollable_utils.SCROLLABLE_SCROLLBARS_ALWAYSVISIBLE, "always" === showScrollbar), _defineProperty(_classesMap, _scrollable_utils.SCROLLABLE_SCROLLBARS_HIDDEN, !showScrollbar), _defineProperty(_classesMap, "".concat(classes), !!classes), _classesMap);
        return (0, _combine_classes.combineClasses)(classesMap)
    }), [props.classes, props.direction, props.disabled, props.showScrollbar]);
    var __tabIndex = (0, _hooks.useCallback)((function() {
        return props.useKeyboard ? 0 : void 0
    }), [props.useKeyboard]);
    var __restAttributes = (0, _hooks.useCallback)((function() {
        props.bounceEnabled, props.children, props.classes, props.direction, props.disabled, props.forceGeneratePockets, props.height, props.inertiaEnabled, props.needScrollViewContentWrapper, props.onScroll, props.pulledDownText, props.pullingDownText, props.pushBackValue, props.reachBottomText, props.refreshingText, props.rtlEnabled, props.scrollByContent, props.scrollByThumb, props.showScrollbar, props.updateManually, props.useKeyboard, props.useSimulatedScrollbar, props.width;
        var restProps = _objectWithoutProperties(props, ["bounceEnabled", "children", "classes", "direction", "disabled", "forceGeneratePockets", "height", "inertiaEnabled", "needScrollViewContentWrapper", "onScroll", "pulledDownText", "pullingDownText", "pushBackValue", "reachBottomText", "refreshingText", "rtlEnabled", "scrollByContent", "scrollByThumb", "showScrollbar", "updateManually", "useKeyboard", "useSimulatedScrollbar", "width"]);
        return restProps
    }), [props]);
    var __content = (0, _hooks.useCallback)((function() {
        return __contentRef.current
    }), []);
    var __scrollBy = (0, _hooks.useCallback)((function(distance) {
        var location = (0, _scrollable_utils.ensureLocation)(distance);
        var _ScrollDirection = new _scrollable_utils.ScrollDirection(props.direction),
            isHorizontal = _ScrollDirection.isHorizontal,
            isVertical = _ScrollDirection.isVertical;
        if (isVertical) {
            __containerRef.current.scrollTop += Math.round(location.top)
        }
        if (isHorizontal) {
            __containerRef.current.scrollLeft += (0, _scrollable_utils.normalizeCoordinate)("left", Math.round(location.left), props.rtlEnabled)
        }
    }), [props.direction, props.rtlEnabled]);
    var __scrollTo = (0, _hooks.useCallback)((function(targetLocation) {
        var location = (0, _scrollable_utils.ensureLocation)(targetLocation);
        var containerPosition = __scrollOffset();
        var top = location.top - containerPosition.top;
        var left = location.left - containerPosition.left;
        __scrollBy({
            top: top,
            left: left
        })
    }), [props.rtlEnabled, props.direction]);
    var __scrollToElement = (0, _hooks.useCallback)((function(element, offset) {
        if (void 0 === element || null === element) {
            return
        }
        if (element.closest(".".concat(_scrollable_utils.SCROLLABLE_CONTENT_CLASS))) {
            var scrollOffset = _objectSpread({
                top: 0,
                left: 0,
                right: 0,
                bottom: 0
            }, offset);
            __scrollTo({
                top: (0, _scrollable_utils.getElementLocation)(element, scrollOffset, _scrollable_utils.DIRECTION_VERTICAL, __containerRef.current, props.rtlEnabled),
                left: (0, _scrollable_utils.getElementLocation)(element, scrollOffset, _scrollable_utils.DIRECTION_HORIZONTAL, __containerRef.current, props.rtlEnabled)
            })
        }
    }), [props.rtlEnabled, props.direction]);
    var __scrollHeight = (0, _hooks.useCallback)((function() {
        return __content().offsetHeight
    }), []);
    var __scrollWidth = (0, _hooks.useCallback)((function() {
        return __content().offsetWidth
    }), []);
    var __scrollOffset = (0, _hooks.useCallback)((function() {
        var rtlEnabled = props.rtlEnabled;
        var _getContainerOffsetIn = (0, _scrollable_utils.getContainerOffsetInternal)(__containerRef.current),
            left = _getContainerOffsetIn.left,
            top = _getContainerOffsetIn.top;
        return {
            left: (0, _scrollable_utils.getPublicCoordinate)("left", left, __containerRef.current, rtlEnabled),
            top: (0, _scrollable_utils.getPublicCoordinate)("top", top, __containerRef.current, rtlEnabled)
        }
    }), [props.rtlEnabled]);
    var __scrollTop = (0, _hooks.useCallback)((function() {
        return __scrollOffset().top
    }), [props.rtlEnabled]);
    var __scrollLeft = (0, _hooks.useCallback)((function() {
        return __scrollOffset().left
    }), [props.rtlEnabled]);
    var __clientHeight = (0, _hooks.useCallback)((function() {
        return __containerRef.current.clientHeight
    }), []);
    var __clientWidth = (0, _hooks.useCallback)((function() {
        return __containerRef.current.clientWidth
    }), []);
    (0, _hooks.useEffect)((function() {
        return (0, _subscribe_to_event.subscribeToScrollEvent)(__containerRef.current, (function(event) {
            var _props$onScroll;
            return null === (_props$onScroll = props.onScroll) || void 0 === _props$onScroll ? void 0 : _props$onScroll.call(props, _objectSpread({
                event: event,
                scrollOffset: __scrollOffset()
            }, (0, _scrollable_utils.getBoundaryProps)(props.direction, __scrollOffset(), __containerRef.current)))
        }))
    }), [props.onScroll, props.rtlEnabled, props.direction]);
    (0, _hooks.useEffect)((function() {
        _short.dxScrollInit.on(__wrapperRef.current, (function(e) {
            __initHandler(e)
        }), {
            getDirection: function(e) {
                return __getDirection(e)
            },
            validate: function(e) {
                return __validate(e)
            },
            isNative: false,
            scrollTarget: __containerRef.current
        }, {
            namespace: "dxScrollable"
        });
        return function() {
            return _short.dxScrollInit.off(__wrapperRef.current, {
                namespace: "dxScrollable"
            })
        }
    }), []);
    (0, _hooks.useEffect)((function() {
        _short.dxScrollStart.on(__wrapperRef.current, (function(e) {
            __handleStart(e)
        }), {
            namespace: "dxScrollable"
        });
        return function() {
            return _short.dxScrollStart.off(__wrapperRef.current, {
                namespace: "dxScrollable"
            })
        }
    }), []);
    (0, _hooks.useEffect)((function() {
        _short.dxScrollMove.on(__wrapperRef.current, (function(e) {
            __handleMove(e)
        }), {
            namespace: "dxScrollable"
        });
        return function() {
            return _short.dxScrollMove.off(__wrapperRef.current, {
                namespace: "dxScrollable"
            })
        }
    }), []);
    (0, _hooks.useEffect)((function() {
        _short.dxScrollEnd.on(__wrapperRef.current, (function(e) {
            __handleEnd(e)
        }), {
            namespace: "dxScrollable"
        });
        return function() {
            return _short.dxScrollEnd.off(__wrapperRef.current, {
                namespace: "dxScrollable"
            })
        }
    }), []);
    (0, _hooks.useEffect)((function() {
        _short.dxScrollStop.on(__wrapperRef.current, (function(event) {
            __handleStop(event)
        }), {
            namespace: "dxScrollable"
        });
        return function() {
            return _short.dxScrollStop.off(__wrapperRef.current, {
                namespace: "dxScrollable"
            })
        }
    }), []);
    (0, _hooks.useEffect)((function() {
        _short.dxScrollCancel.on(__wrapperRef.current, (function(event) {
            __handleCancel(event)
        }), {
            namespace: "dxScrollable"
        });
        return function() {
            return _short.dxScrollCancel.off(__wrapperRef.current, {
                namespace: "dxScrollable"
            })
        }
    }), []);
    (0, _hooks.useImperativeHandle)(ref, (function() {
        return {
            content: __content,
            scrollBy: __scrollBy,
            scrollTo: __scrollTo,
            scrollToElement: __scrollToElement,
            scrollHeight: __scrollHeight,
            scrollWidth: __scrollWidth,
            scrollOffset: __scrollOffset,
            scrollTop: __scrollTop,
            scrollLeft: __scrollLeft,
            clientHeight: __clientHeight,
            clientWidth: __clientWidth
        }
    }), [__content, __scrollBy, __scrollTo, __scrollToElement, __scrollHeight, __scrollWidth, __scrollOffset, __scrollTop, __scrollLeft, __clientHeight, __clientWidth]);
    return viewFunction({
        props: _objectSpread({}, props),
        wrapperRef: __wrapperRef,
        contentRef: __contentRef,
        containerRef: __containerRef,
        initHandler: __initHandler,
        cssClasses: __cssClasses(),
        tabIndex: __tabIndex(),
        restAttributes: __restAttributes()
    })
}));
exports.ScrollableSimulated = ScrollableSimulated;
ScrollableSimulated.defaultProps = _objectSpread({}, _scrollable_props.ScrollableInternalPropsType);
