(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "tslib", "react", "../typography/P2", "./styles", "./helpers/useSlider", "./helpers/defaultTooltipFormatter"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.SliderComponent = void 0;
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var P2_1 = (0, tslib_1.__importDefault)(require("../typography/P2"));
    var styles_1 = require("./styles");
    var useSlider_1 = require("./helpers/useSlider");
    var defaultTooltipFormatter_1 = require("./helpers/defaultTooltipFormatter");
    var SliderComponent = function (props, ref) {
        var _a = props.isDisabled, isDisabled = _a === void 0 ? false : _a, value = props.value, _b = props.isRange, isRange = _b === void 0 ? false : _b, _c = props.max, max = _c === void 0 ? 100 : _c, _d = props.min, min = _d === void 0 ? 0 : _d, _e = props.step, step = _e === void 0 ? 1 : _e, onChange = props.onChange, _f = props.direction, direction = _f === void 0 ? "horizontal" : _f, defaultValue = props.defaultValue, isReset = props.isReset, leadText = props.leadText, trailText = props.trailText, _g = props.status, status = _g === void 0 ? "info" : _g, _h = props.isTooltip, isTooltip = _h === void 0 ? true : _h, _j = props.size, size = _j === void 0 ? "m" : _j, className = props.className, _k = props.tooltipFormatter, tooltipFormatter = _k === void 0 ? defaultTooltipFormatter_1.defaultTooltipFormatter : _k, style = props.style, width = props.width, height = props.height, _l = props.testId, testId = _l === void 0 ? "slider" : _l, otherProps = (0, tslib_1.__rest)(props, ["isDisabled", "value", "isRange", "max", "min", "step", "onChange", "direction", "defaultValue", "isReset", "leadText", "trailText", "status", "isTooltip", "size", "className", "tooltipFormatter", "style", "width", "height", "testId"]);
        var leftHintRef = (0, react_1.useRef)(null);
        var rightHintRef = (0, react_1.useRef)(null);
        var fromInputRef = (0, react_1.useRef)(null);
        var toInputRef = (0, react_1.useRef)(null);
        var sliderControlRef = (0, react_1.useRef)(null);
        var fromDefaultValue = (0, react_1.useMemo)(function () {
            if (isRange) {
                if (typeof value[0] === "number") {
                    return value[0];
                }
                else if (defaultValue && typeof defaultValue[0] === "number") {
                    return defaultValue[0];
                }
            }
            return min;
        }, [isRange, defaultValue, value]);
        var toDefaultValue = (0, react_1.useMemo)(function () {
            if (isRange) {
                if (typeof value[1] === "number") {
                    return value[1];
                }
                else if (defaultValue && typeof defaultValue[1] === "number") {
                    return defaultValue[1];
                }
            }
            else if (typeof value === "number") {
                return value;
            }
            else if (typeof defaultValue === "number") {
                return defaultValue;
            }
            return max;
        }, [isRange, defaultValue, value]);
        (0, react_1.useEffect)(function () {
            if (direction === "vertical" && sliderControlRef.current) {
                if (toInputRef.current) {
                    toInputRef.current.style.width = sliderControlRef.current.clientHeight + "px";
                }
                if (fromInputRef.current) {
                    fromInputRef.current.style.width = sliderControlRef.current.clientHeight + "px";
                }
            }
            else {
                if (toInputRef.current) {
                    toInputRef.current.style.removeProperty("width");
                }
                if (fromInputRef.current) {
                    fromInputRef.current.style.removeProperty("width");
                }
            }
        }, [direction]);
        var _m = (0, useSlider_1.useSlider)({
            isDisabled: isDisabled,
            isRange: isRange,
            value: value,
            min: min,
            max: max,
            step: step,
            onChange: onChange,
            hintsRef: [leftHintRef, rightHintRef],
            direction: direction,
            defaultValue: defaultValue,
            inputsRef: [fromInputRef, toInputRef],
            status: status
        }), currentValue = _m.currentValue, handleFromInput = _m.handleFromInput, handleToInput = _m.handleToInput, onResetRight = _m.onResetRight, onResetLeft = _m.onResetLeft, handleClick = _m.handleClick;
        return (react_1.default.createElement(styles_1.SliderWrapper, (0, tslib_1.__assign)({ size: size, className: className, style: style, ref: ref, direction: direction, width: width, height: height, "data-testid": testId }, otherProps),
            (leadText || (trailText && direction === "horizontal")) && (react_1.default.createElement(styles_1.SliderLeadTextWrapper, { size: size },
                leadText && (react_1.default.createElement(P2_1.default, { type: "corvus", className: "slider__lead-text" }, leadText)),
                trailText && direction === "horizontal" && (react_1.default.createElement(P2_1.default, { type: "corvus", className: "slider__trail-text" }, trailText)))),
            react_1.default.createElement("div", { className: "sliders_control", onMouseDown: handleClick, ref: sliderControlRef },
                isTooltip && isRange && (react_1.default.createElement(styles_1.TagStyled, { sizeSlider: size, direction: direction, ref: leftHintRef, size: "xs", isClosable: isReset && !isDisabled, onClickClose: onResetLeft }, tooltipFormatter(currentValue[0]))),
                isTooltip && (react_1.default.createElement(styles_1.TagStyled, { sizeSlider: size, direction: direction, ref: rightHintRef, size: "xs", isClosable: isReset && !isDisabled, onClickClose: onResetRight }, tooltipFormatter(isRange ? currentValue[1] : currentValue))),
                isRange && direction === "horizontal" && (react_1.default.createElement(styles_1.SliderInputStyled, { direction: direction, sizeS: size, status: status, ref: fromInputRef, id: "fromSlider", type: "range", step: step, defaultValue: fromDefaultValue, min: min, max: max, onInput: handleFromInput, disabled: isDisabled })),
                react_1.default.createElement(styles_1.SliderInputStyled, { direction: direction, sizeS: size, status: status, step: step, ref: toInputRef, id: "toSlider", type: "range", defaultValue: toDefaultValue, min: min, max: max, onInput: handleToInput, disabled: isDisabled }),
                isRange && direction === "vertical" && (react_1.default.createElement(styles_1.SliderInputStyled, { direction: direction, sizeS: size, status: status, ref: fromInputRef, id: "fromSlider", type: "range", step: step, defaultValue: fromDefaultValue, min: min, max: max, onInput: handleFromInput, disabled: isDisabled }))),
            trailText && direction === "vertical" && (react_1.default.createElement(styles_1.SliderTrailTextWrapper, { direction: direction, size: size },
                react_1.default.createElement(P2_1.default, { type: "corvus", className: "slider__trail-text" }, trailText)))));
    };
    exports.SliderComponent = SliderComponent;
    exports.default = (0, react_1.forwardRef)(exports.SliderComponent);
});
