"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TwoHandleRange = exports.stepping = void 0;
const react_1 = __importStar(require("react"));
function stepping(value, step) {
    return Math.round(value / step) * step;
}
exports.stepping = stepping;
const TwoHandleRange = ({ min, max, step, value, onChange, uncappedMax = false, }) => {
    const range = (0, react_1.useRef)(null);
    // Convert to percentage
    const getPercent = (0, react_1.useCallback)((value) => Math.round(((value - min) / (max - min)) * 100), [min, max]);
    // Set width of the range to decrease from the left side
    (0, react_1.useEffect)(() => {
        const minPercent = getPercent(value[0]);
        const maxPercent = getPercent(value[1]);
        if (range.current) {
            range.current.style.left = `${minPercent}%`;
            range.current.style.width = `${maxPercent - minPercent}%`;
        }
    }, [value[0], getPercent]);
    // Set width of the range to decrease from the right side
    (0, react_1.useEffect)(() => {
        const minPercent = getPercent(value[0]);
        const maxPercent = getPercent(value[1]);
        if (range.current) {
            range.current.style.width = `${maxPercent - minPercent}%`;
        }
    }, [value[1], getPercent]);
    const minChangeHandler = (event) => {
        const changeEvent = event;
        const discreteValue = stepping(Math.min(Number(changeEvent.target.value), value[1] - 1), step);
        value[0] = discreteValue;
        onChange([discreteValue, value[1]]);
    };
    const maxChangeHandler = (event) => {
        const changeEvent = event;
        const discreteValue = stepping(Math.max(Number(changeEvent.target.value), value[0] + 1), step);
        value[1] = discreteValue;
        onChange([value[0], discreteValue]);
    };
    const leftValue = value[0];
    const rightValue = uncappedMax
        ? (value[1] === max && `${value[1]}+`) || value[1]
        : value[1];
    return (react_1.default.createElement("div", { className: "formRange-inner" },
        react_1.default.createElement("input", { type: "range", min: min, max: max, value: value[0], step: step, onChange: minChangeHandler, onKeyDown: minChangeHandler, "aria-valuemin": min, "aria-valuemax": max, "aria-valuenow": value[0], className: "thumb thumb--left", style: { zIndex: value[0] > max - 100 && 5 } }),
        react_1.default.createElement("input", { type: "range", min: min, max: max, value: value[1], step: step, onChange: maxChangeHandler, onKeyDown: maxChangeHandler, "aria-valuemin": min, "aria-valuemax": max, "aria-valuenow": value[1], className: "thumb thumb--right" }),
        react_1.default.createElement("div", { className: "slider" },
            react_1.default.createElement("div", { className: "slider__track" }),
            react_1.default.createElement("div", { ref: range, className: "slider__range" }),
            react_1.default.createElement("div", { className: "slider__left-value" }, leftValue),
            react_1.default.createElement("div", { className: "slider__right-value" }, rightValue))));
};
exports.TwoHandleRange = TwoHandleRange;
