/**
 * DevExtreme (renovation/ui/scroll_view/scrollbar.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.Scrollbar = Scrollbar;
exports.viewFunction = void 0;
var _widget = require("../common/widget");
var _combine_classes = require("../../utils/combine_classes");
var _dom_adapter = _interopRequireDefault(require("../../../core/dom_adapter"));
var _scrollbar_props = require("./scrollbar_props");
var _scrollable_utils = require("./scrollable_utils");
var _short = require("../../../events/short");
var Preact = _interopRequireWildcard(require("preact"));
var _hooks = require("preact/hooks");

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 _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}

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 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
}

function _slicedToArray(arr, i) {
    return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest()
}

function _nonIterableRest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
}

function _unsupportedIterableToArray(o, minLen) {
    if (!o) {
        return
    }
    if ("string" === typeof o) {
        return _arrayLikeToArray(o, minLen)
    }
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if ("Object" === n && o.constructor) {
        n = o.constructor.name
    }
    if ("Map" === n || "Set" === n) {
        return Array.from(o)
    }
    if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) {
        return _arrayLikeToArray(o, minLen)
    }
}

function _arrayLikeToArray(arr, len) {
    if (null == len || len > arr.length) {
        len = arr.length
    }
    for (var i = 0, arr2 = new Array(len); i < len; i++) {
        arr2[i] = arr[i]
    }
    return arr2
}

function _iterableToArrayLimit(arr, i) {
    if ("undefined" === typeof Symbol || !(Symbol.iterator in Object(arr))) {
        return
    }
    var _arr = [];
    var _n = true;
    var _d = false;
    var _e = void 0;
    try {
        for (var _s, _i = arr[Symbol.iterator](); !(_n = (_s = _i.next()).done); _n = true) {
            _arr.push(_s.value);
            if (i && _arr.length === i) {
                break
            }
        }
    } catch (err) {
        _d = true;
        _e = err
    } finally {
        try {
            if (!_n && null != _i.return) {
                _i.return()
            }
        } finally {
            if (_d) {
                throw _e
            }
        }
    }
    return _arr
}

function _arrayWithHoles(arr) {
    if (Array.isArray(arr)) {
        return arr
    }
}

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)
}
var SCROLLABLE_SCROLLBAR_CLASS = "dx-scrollable-scrollbar";
var SCROLLABLE_SCROLLBAR_ACTIVE_CLASS = "dx-scrollable-scrollbar-active";
var SCROLLABLE_SCROLL_CLASS = "dx-scrollable-scroll";
var SCROLLABLE_SCROLL_CONTENT_CLASS = "dx-scrollable-scroll-content";
var HOVER_ENABLED_STATE = "dx-scrollbar-hoverable";
var THUMB_MIN_SIZE = 15;
var viewFunction = function(viewModel) {
    var cssClasses = viewModel.cssClasses,
        hoverStateEnabled = viewModel.hoverStateEnabled,
        activeStateEnabled = viewModel.props.activeStateEnabled,
        restAttributes = viewModel.restAttributes,
        scrollRef = viewModel.scrollRef,
        styles = viewModel.styles;
    return Preact.h(_widget.Widget, _extends({
        classes: cssClasses,
        activeStateEnabled: activeStateEnabled,
        hoverStateEnabled: hoverStateEnabled
    }, restAttributes), Preact.h("div", {
        className: SCROLLABLE_SCROLL_CLASS,
        style: styles,
        ref: scrollRef
    }, Preact.h("div", {
        className: SCROLLABLE_SCROLL_CONTENT_CLASS
    })))
};
exports.viewFunction = viewFunction;

function Scrollbar(props) {
    var __scrollRef = (0, _hooks.useRef)();
    var _useState = (0, _hooks.useState)(0),
        _useState2 = _slicedToArray(_useState, 2),
        __state_baseContainerToContentRatio = _useState2[0];
    _useState2[1];
    var _useState3 = (0, _hooks.useState)(false),
        _useState4 = _slicedToArray(_useState3, 2),
        __state_active = _useState4[0],
        __state_setActive = _useState4[1];
    var __feedbackOn = (0, _hooks.useCallback)((function() {
        __state_setActive((function(__state_active) {
            return true
        }))
    }), []);
    var __feedbackOff = (0, _hooks.useCallback)((function() {
        __state_setActive((function(__state_active) {
            return false
        }))
    }), []);
    var __cssClasses = (0, _hooks.useCallback)((function() {
        var _classesMap;
        var direction = props.direction;
        var classesMap = (_classesMap = {}, _defineProperty(_classesMap, SCROLLABLE_SCROLLBAR_CLASS, true), _defineProperty(_classesMap, "dx-scrollbar-".concat(direction), true), _defineProperty(_classesMap, SCROLLABLE_SCROLLBAR_ACTIVE_CLASS, !!__state_active), _defineProperty(_classesMap, HOVER_ENABLED_STATE, !!__hoverStateEnabled()), _classesMap);
        return (0, _combine_classes.combineClasses)(classesMap)
    }), [props.direction, __state_active, props.expandable, props.visibilityMode]);
    var __styles = (0, _hooks.useCallback)((function() {
        var style = __restAttributes().style || {};
        return _objectSpread(_objectSpread({}, style), {}, _defineProperty({
            display: __needScrollbar() ? "" : "none"
        }, "".concat(__getDimension()), THUMB_MIN_SIZE))
    }), [props, props.visibilityMode, __state_baseContainerToContentRatio, props.direction]);
    var __isHidden = (0, _hooks.useCallback)((function() {
        return "never" === props.visibilityMode
    }), [props.visibilityMode]);
    var __needScrollbar = (0, _hooks.useCallback)((function() {
        return !__isHidden() && __state_baseContainerToContentRatio < 1
    }), [props.visibilityMode, __state_baseContainerToContentRatio]);
    var __getDimension = (0, _hooks.useCallback)((function() {
        var _ScrollDirection = new _scrollable_utils.ScrollDirection(props.direction),
            isHorizontal = _ScrollDirection.isHorizontal;
        return isHorizontal ? "width" : "height"
    }), [props.direction]);
    var __hoverStateEnabled = (0, _hooks.useCallback)((function() {
        var expandable = props.expandable,
            visibilityMode = props.visibilityMode;
        return ("onHover" === visibilityMode || "always" === visibilityMode) && expandable
    }), [props.expandable, props.visibilityMode]);
    var __restAttributes = (0, _hooks.useCallback)((function() {
        props.activeStateEnabled, props.bounceEnabled, props.children, props.classes, props.direction, props.expandable, props.forceGeneratePockets, props.hoverStateEnabled, props.inertiaEnabled, props.needScrollViewContentWrapper, props.onScroll, props.pushBackValue, props.scrollByContent, props.scrollByThumb, props.showScrollbar, props.updateManually, props.useKeyboard, props.useSimulatedScrollbar, props.visibilityMode, props.visible;
        var restProps = _objectWithoutProperties(props, ["activeStateEnabled", "bounceEnabled", "children", "classes", "direction", "expandable", "forceGeneratePockets", "hoverStateEnabled", "inertiaEnabled", "needScrollViewContentWrapper", "onScroll", "pushBackValue", "scrollByContent", "scrollByThumb", "showScrollbar", "updateManually", "useKeyboard", "useSimulatedScrollbar", "visibilityMode", "visible"]);
        return restProps
    }), [props]);
    (0, _hooks.useEffect)((function() {
        _short.dxPointerDown.on(__scrollRef.current, (function() {
            __feedbackOn()
        }), {
            namespace: "dxScrollbar"
        });
        return function() {
            return _short.dxPointerDown.off(__scrollRef.current, {
                namespace: "dxScrollbar"
            })
        }
    }), []);
    (0, _hooks.useEffect)((function() {
        _short.dxPointerUp.on(_dom_adapter.default.getDocument(), (function() {
            __feedbackOff()
        }), {
            namespace: "dxScrollbar"
        });
        return function() {
            return _short.dxPointerUp.off(__scrollRef.current, {
                namespace: "dxScrollbar"
            })
        }
    }), []);
    return viewFunction({
        props: _objectSpread({}, props),
        baseContainerToContentRatio: __state_baseContainerToContentRatio,
        active: __state_active,
        scrollRef: __scrollRef,
        cssClasses: __cssClasses(),
        styles: __styles(),
        hoverStateEnabled: __hoverStateEnabled(),
        restAttributes: __restAttributes()
    })
}
Scrollbar.defaultProps = _objectSpread({}, _scrollbar_props.ScrollbarProps);
