export const getClampedPercent = (rect, x) => {
    const clampedX = Math.max(0, Math.min(x, rect.width));
    const percent = (clampedX / rect.width) * 100;
    return percent;
};
export const percentToValue = (percent, nileSlider) => {
    const value = nileSlider.minValue + (percent / 100) * (nileSlider.maxValue - nileSlider.minValue);
    if (nileSlider.maxValue - nileSlider.minValue < 1) {
        return parseFloat(value.toFixed(2));
    }
    else {
        return Math.floor(value);
    }
};
export const valueToPercent = (value, nileSlider) => {
    return ((value - nileSlider.minValue) / (nileSlider.maxValue - nileSlider.minValue)) * 100;
};
export const handleSingleSlider = (e, rect, nileSlider) => {
    const x = e.clientX - rect.left;
    const percent = getClampedPercent(rect, x);
    nileSlider.value = percentToValue(percent, nileSlider);
    nileSlider.emit("nile-change" /* NileSliderEvents.NILE_CHANGE */, { value: nileSlider.value });
    nileSlider.buttonOne.style.left = `${percent}%`;
    nileSlider.completed.style.width = `${percent}%`;
};
export const handleRangeOne = (e, rect, nileSlider) => {
    const x = e.clientX - rect.left;
    const percent = getClampedPercent(rect, x);
    nileSlider.rangeOneValue = percentToValue(percent, nileSlider);
    nileSlider.rangeOneValue = Math.min(nileSlider.rangeOneValue, nileSlider.rangeTwoValue);
    nileSlider.emit("nile-button-first-change" /* NileSliderEvents.NILE_BUTTON_FIRST_CHANGE */, {
        value: nileSlider.rangeOneValue,
    });
    if (nileSlider.rangeTwoValue > nileSlider.rangeOneValue) {
        nileSlider.buttonOne.style.left = `${percent}%`;
    }
};
export const handleRangeTwo = (e, rect, nileSlider) => {
    const x = e.clientX - rect.left;
    const percent = getClampedPercent(rect, x);
    nileSlider.rangeTwoValue = percentToValue(percent, nileSlider);
    nileSlider.rangeTwoValue = Math.max(nileSlider.rangeTwoValue, nileSlider.rangeOneValue);
    nileSlider.emit("nile-button-last-change" /* NileSliderEvents.NILE_BUTTON_LAST_CHANGE */, {
        value: nileSlider.rangeTwoValue,
    });
    if (nileSlider.rangeTwoValue > nileSlider.rangeOneValue) {
        nileSlider.buttonTwo.style.left = `${percent}%`;
    }
};
export const getHtmlElements = (nileSlider) => {
    nileSlider.range = nileSlider.renderRoot.querySelector('.range');
    nileSlider.buttonOne = nileSlider.renderRoot.querySelector('.range-button');
    nileSlider.buttonTwo = nileSlider.renderRoot.querySelector('.range-button-two');
    nileSlider.completed = nileSlider.renderRoot.querySelector('.range-completed');
    nileSlider.range.addEventListener('mousedown', nileSlider.onMouseDown);
    nileSlider.buttonOne?.addEventListener('mousedown', () => {
        nileSlider.activeThumb = 'one';
        addMoveListeners(nileSlider);
    });
    nileSlider.buttonTwo?.addEventListener('mousedown', () => {
        nileSlider.activeThumb = 'two';
        addMoveListeners(nileSlider);
    });
};
export const addMoveListeners = (nileSlider) => {
    window.addEventListener('mousemove', nileSlider.onMouseMove);
    window.addEventListener('mouseup', nileSlider.onMouseUp);
};
export const removeMoveListeners = (nileSlider) => {
    window.removeEventListener('mousemove', nileSlider.onMouseMove);
    window.removeEventListener('mouseup', nileSlider.onMouseUp);
};
export const handleTwoThumbClick = (value, nileSlider) => {
    const distToOne = Math.abs(value - nileSlider.rangeOneValue);
    const distToTwo = Math.abs(value - nileSlider.rangeTwoValue);
    if (distToOne <= distToTwo) {
        const clamped = Math.min(value, nileSlider.rangeTwoValue);
        nileSlider.rangeOneValue = nileSlider.checkValueValidity(clamped, nileSlider.minValue, nileSlider.maxValue);
    }
    else {
        const clamped = Math.max(value, nileSlider.rangeOneValue);
        nileSlider.rangeTwoValue = nileSlider.checkValueValidity(clamped, nileSlider.minValue, nileSlider.maxValue);
    }
    const percentOne = valueToPercent(nileSlider.rangeOneValue, nileSlider);
    const percentTwo = valueToPercent(nileSlider.rangeTwoValue, nileSlider);
    nileSlider.buttonOne.style.left = `${percentOne}%`;
    nileSlider.buttonTwo.style.left = `${percentTwo}%`;
    const left = Math.min(percentOne, percentTwo);
    const width = Math.abs(percentTwo - percentOne);
    nileSlider.completed.style.left = `${left}%`;
    nileSlider.completed.style.width = `${width}%`;
    nileSlider.emit("nile-button-first-change" /* NileSliderEvents.NILE_BUTTON_FIRST_CHANGE */, {
        value: nileSlider.rangeOneValue,
    });
    nileSlider.emit("nile-button-last-change" /* NileSliderEvents.NILE_BUTTON_LAST_CHANGE */, {
        value: nileSlider.rangeTwoValue,
    });
};
//# sourceMappingURL=nile-slider.utils.js.map