"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "SliderBase", {
    enumerable: true,
    get: function() {
        return SliderBase;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _hooks = require("../../../../hooks");
var _Slidertokens = require("../../Slider.tokens");
var _utils = require("../../../../utils");
var _SliderBasestyles = require("./SliderBase.styles");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && 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;
}
var SliderBase = function SliderBase(param) {
    var max = param.max, min = param.min, setStepSize = param.setStepSize, railFillWidth = param.railFillWidth, children = param.children, _param_railFillXPosition = param.railFillXPosition, railFillXPosition = _param_railFillXPosition === void 0 ? 0 : _param_railFillXPosition, disabled = param.disabled, labelPlacement = param.labelPlacement, rangeValuesPlacement = param.rangeValuesPlacement, onChange = param.onChange, orientation = param.orientation, reversed = param.reversed, size = param.size, sliderAlign = param.sliderAlign;
    var ref = (0, _react.useRef)(null);
    var isVertical = orientation === 'vertical';
    (0, _react.useEffect)(function() {
        var resizeHandler = function resizeHandler() {
            if (ref.current) {
                var railSize = isVertical ? ref.current.offsetHeight : ref.current.offsetWidth;
                var totalSteps = max - min;
                setStepSize(railSize / totalSteps);
            }
        };
        resizeHandler();
    }, [
        labelPlacement,
        rangeValuesPlacement,
        isVertical,
        // для перерасчета размеров
        size,
        sliderAlign
    ]);
    var onHandleChange = function onHandleChange(e) {
        if (!onChange || disabled) {
            return;
        }
        var _e_currentTarget_getBoundingClientRect = e.currentTarget.getBoundingClientRect(), x = _e_currentTarget_getBoundingClientRect.x, width = _e_currentTarget_getBoundingClientRect.width, y = _e_currentTarget_getBoundingClientRect.y, height = _e_currentTarget_getBoundingClientRect.height;
        var lastPos = isVertical ? e.clientY - y : e.clientX - x;
        var sliderWidth = isVertical ? height : width;
        var position = min + lastPos / sliderWidth * (max - min);
        var result = Math.max(min, Math.min(max, position));
        var data = isVertical ? {
            lastY: lastPos
        } : {
            lastX: lastPos
        };
        onChange(result, data);
    };
    (0, _hooks.useIsomorphicLayoutEffect)(function() {
        var resizeHandler = function resizeHandler() {
            if (ref.current) {
                var railSize = isVertical ? ref.current.offsetHeight : ref.current.offsetWidth;
                var totalSteps = max - min;
                setStepSize(railSize / totalSteps);
            }
        };
        resizeHandler();
        window.addEventListener('resize', resizeHandler);
        return function() {
            return window.removeEventListener('resize', resizeHandler);
        };
    }, [
        min,
        max,
        setStepSize,
        labelPlacement,
        rangeValuesPlacement,
        isVertical
    ]);
    var fillStyle = function() {
        if (isVertical && reversed) {
            return {
                bottom: 0,
                height: "calc(100% - ".concat(railFillWidth, "px)"),
                top: 'auto',
                width: '100%'
            };
        }
        if (isVertical) {
            return {
                bottom: "".concat(railFillXPosition, "px"),
                height: "".concat(railFillWidth, "px"),
                width: '100%'
            };
        }
        return {
            left: "".concat(railFillXPosition, "px"),
            width: "".concat(railFillWidth, "px")
        };
    }();
    return /*#__PURE__*/ _react.default.createElement(_SliderBasestyles.Slider, {
        ref: ref,
        className: (0, _utils.cx)(orientation === 'vertical' && _Slidertokens.classes.verticalOrientation)
    }, /*#__PURE__*/ _react.default.createElement(_SliderBasestyles.RailWrap, {
        "aria-hidden": "true",
        onMouseDown: onHandleChange
    }, /*#__PURE__*/ _react.default.createElement(_SliderBasestyles.Rail, null, /*#__PURE__*/ _react.default.createElement(_SliderBasestyles.Fill, {
        style: fillStyle
    }))), children);
};
