import React, { useRef, useEffect } from "react";
import { useIsomorphicLayoutEffect } from "../../../../hooks";
import { classes } from "../../Slider.tokens";
import { cx } from "../../../../utils";
import { Fill, Rail, RailWrap, Slider } from "./SliderBase.styles";
export 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 = useRef(null);
    var isVertical = orientation === 'vertical';
    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);
    };
    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.createElement(Slider, {
        ref: ref,
        className: cx(orientation === 'vertical' && classes.verticalOrientation)
    }, /*#__PURE__*/ React.createElement(RailWrap, {
        "aria-hidden": "true",
        onMouseDown: onHandleChange
    }, /*#__PURE__*/ React.createElement(Rail, null, /*#__PURE__*/ React.createElement(Fill, {
        style: fillStyle
    }))), children);
};
