var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import React from 'react';
import { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { isInstanceOf } from '../../lib/isInstanceOf.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
import { defaultScrollbarState, scrollSizeParametersNames } from './ScrollContainer.constants.js';
import { getStyles, globalClasses } from './ScrollContainer.styles.js';
import { getScrollSizeParams } from './ScrollContainer.helpers.js';
var ScrollBar = /** @class */ (function (_super) {
    __extends(ScrollBar, _super);
    function ScrollBar() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.containerRef = React.createRef();
        _this.state = __assign({}, defaultScrollbarState);
        _this.renderMain = function () {
            var _a, _b;
            var state = _this.state;
            var props = _this.props;
            if (!state.active) {
                return null;
            }
            var _c = scrollSizeParametersNames[_this.props.axis], customScrollPos = _c.customScrollPos, customScrollSize = _c.customScrollSize;
            var classNames = _this.cx(props.className, _this.styles.scrollBar(_this.theme), _this.scrollBarStyles, (_a = {},
                _a[_this.styles.scrollBarInvert(_this.theme)] = props.invert,
                _a[_this.styles.visibleScrollBar()] = props.isVisible,
                _a));
            var inlineStyles = (_b = {},
                _b[customScrollPos] = "".concat(state.pos, "%"),
                _b[customScrollSize] = "".concat(state.size, "%"),
                _b);
            return (React.createElement("div", { ref: _this.containerRef, className: _this.scrollBarContainerClassNames, style: props.offset },
                React.createElement("div", { ref: _this.refScroll, style: inlineStyles, className: classNames, onMouseDown: _this.handleScrollMouseDown, "data-tid": "ScrollContainer__ScrollBar-".concat(props.axis) })));
        };
        _this.reflow = function () {
            var _a, _b;
            if (!_this.inner) {
                return;
            }
            var props = _this.props;
            var state = _this.state;
            var _c = getScrollSizeParams(_this.inner, props.axis), scrollSize = _c.scrollSize, scrollPos = _c.scrollPos, scrollActive = _c.scrollActive;
            if (!scrollActive && !state.active) {
                return;
            }
            if (state.active !== scrollActive || state.size !== scrollSize || state.pos !== scrollPos) {
                var scrollState = _this.getImmediateScrollState();
                if (state.pos !== scrollPos) {
                    (_b = (_a = _this.props).onScroll) === null || _b === void 0 ? void 0 : _b.call(_a, props.axis, scrollState, state.scrollState);
                }
                _this.setState(__assign(__assign({}, _this.state), { active: scrollActive, size: scrollSize, pos: scrollPos, scrollState: scrollState }));
            }
        };
        _this.setInnerElement = function (inner) {
            _this.inner = inner;
            _this.reflow();
        };
        _this.refScroll = function (element) {
            var handleScrollWheel = function (event) { return _this.handleScrollWheel(event, _this.props.axis); };
            if (!_this.node && element) {
                element.addEventListener('wheel', handleScrollWheel, { passive: false });
            }
            if (_this.node && !element) {
                _this.node.removeEventListener('wheel', handleScrollWheel);
            }
            _this.node = element;
        };
        _this.handleScrollMouseDown = function (event) {
            if (!_this.inner) {
                return;
            }
            var _a = scrollSizeParametersNames[_this.props.axis], offset = _a.offset, size = _a.size, pos = _a.pos, coord = _a.coord;
            var initialCoord = event[coord];
            var target = _this.globalObject.document;
            var initialScrollPos = _this.inner[pos];
            var state = _this.state;
            var mouseMove = function (mouseMoveEvent) {
                if (!_this.inner || !_this.containerRef.current) {
                    return;
                }
                var remainingScrollingContent = _this.inner[size] - _this.inner[offset];
                var remainingScrollingSpace = (_this.containerRef.current[offset] / 100) * (100 - state.size);
                var ratio = remainingScrollingContent / remainingScrollingSpace;
                var delta = (mouseMoveEvent[coord] - initialCoord) * ratio;
                _this.inner[pos] = initialScrollPos + delta;
                if (mouseMoveEvent.preventDefault) {
                    mouseMoveEvent.preventDefault();
                }
                if (Object.prototype.hasOwnProperty.call(mouseMoveEvent, 'returnValue')) {
                    mouseMoveEvent.returnValue = false;
                }
            };
            var mouseUp = function () {
                target === null || target === void 0 ? void 0 : target.removeEventListener('mousemove', mouseMove);
                target === null || target === void 0 ? void 0 : target.removeEventListener('mouseup', mouseUp);
                _this.setState(__assign(__assign({}, _this.state), { scrollingByMouseDrag: false }));
            };
            target === null || target === void 0 ? void 0 : target.addEventListener('mousemove', mouseMove);
            target === null || target === void 0 ? void 0 : target.addEventListener('mouseup', mouseUp);
            _this.setState(__assign(__assign({}, _this.state), { scrollingByMouseDrag: true }));
            event.preventDefault();
        };
        _this.handleScrollWheel = function (event, axis) {
            if (!_this.inner || !isInstanceOf(event, _this.globalObject.WheelEvent) || (axis === 'x' && !event.shiftKey)) {
                return;
            }
            var _a = scrollSizeParametersNames[axis], offset = _a.offset, size = _a.size, pos = _a.pos;
            var scrollSize = _this.inner[size];
            var scrollPos = _this.inner[pos];
            var offsetHeight = _this.inner[offset];
            if (event.deltaY > 0 && scrollSize <= scrollPos + offsetHeight) {
                return;
            }
            if (event.deltaY < 0 && scrollPos <= 0) {
                return;
            }
            _this.inner[pos] += event.deltaY;
            event.preventDefault();
        };
        _this.getImmediateScrollState = function () {
            var _a = scrollSizeParametersNames[_this.props.axis], pos = _a.pos, size = _a.size, clientSize = _a.clientSize;
            if (!_this.inner || _this.inner[pos] === 0) {
                return 'begin';
            }
            // Zoom in Chrome causes problems
            // https://github.com/skbkontur/retail-ui/pull/2705#issue-806286945
            var maxScrollPos = _this.inner[size] - _this.inner[clientSize];
            if (Math.abs(maxScrollPos - _this.inner[pos]) <= 1) {
                return 'end';
            }
            return 'middle';
        };
        return _this;
    }
    ScrollBar.prototype.componentDidMount = function () {
        this.reflow();
    };
    ScrollBar.prototype.componentDidUpdate = function () {
        this.reflow();
    };
    ScrollBar.prototype.render = function () {
        var _this = this;
        this.styles = getStyles(this.emotion);
        return (React.createElement(ThemeContext.Consumer, null, function (theme) {
            _this.theme = theme;
            return _this.renderMain();
        }));
    };
    ScrollBar.prototype.setHover = function (hover) {
        if (this.state.active && this.state.hover !== hover) {
            this.setState(__assign(__assign({}, this.state), { hover: hover }));
        }
    };
    ScrollBar.prototype.getHover = function () {
        return this.state.hover;
    };
    Object.defineProperty(ScrollBar.prototype, "scrollBarState", {
        get: function () {
            return this.state.scrollState;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(ScrollBar.prototype, "scrollBarStyles", {
        get: function () {
            var _a, _b;
            var state = this.state;
            if (this.props.axis === 'x') {
                return this.cx(this.styles.scrollBarX(this.theme), globalClasses.scrollbarX, (_a = {},
                    _a[this.styles.scrollBarXHover(this.theme)] = state.hover || state.scrollingByMouseDrag,
                    _a));
            }
            return this.cx(this.styles.scrollBarY(this.theme), globalClasses.scrollbarY, (_b = {},
                _b[this.styles.scrollBarYHover(this.theme)] = state.hover || state.scrollingByMouseDrag,
                _b));
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(ScrollBar.prototype, "scrollBarContainerClassNames", {
        get: function () {
            var axis = this.props.axis;
            if (axis === 'x') {
                return this.cx(globalClasses.scrollbarContainerX, this.styles.scrollBarContainerX(this.theme));
            }
            return this.cx(globalClasses.scrollbarContainerY, this.styles.scrollBarContainerY());
        },
        enumerable: false,
        configurable: true
    });
    ScrollBar = __decorate([
        withRenderEnvironment
    ], ScrollBar);
    return ScrollBar;
}(React.Component));
export { ScrollBar };
//# sourceMappingURL=ScrollBar.js.map