import * as tslib_1 from "tslib";
import * as React from 'react';
import { BaseComponent, css, createRef } from '../../Utilities';
import * as stylesImport from './ColorPicker.scss';
var styles = stylesImport;
var ColorSlider = /** @class */ (function (_super) {
    tslib_1.__extends(ColorSlider, _super);
    function ColorSlider(props) {
        var _this = _super.call(this, props) || this;
        _this._root = createRef();
        _this._onMouseDown = function (ev) {
            _this._events.on(window, 'mousemove', _this._onMouseMove, true);
            _this._events.on(window, 'mouseup', _this._onMouseUp, true);
            _this._onMouseMove(ev);
        };
        _this._onMouseMove = function (ev) {
            if (!_this._root.current) {
                return;
            }
            var _a = _this.props, onChanged = _a.onChanged, minValue = _a.minValue, maxValue = _a.maxValue;
            var rectSize = _this._root.current.getBoundingClientRect();
            var currentPercentage = (ev.clientX - rectSize.left) / rectSize.width;
            var newValue = Math.min(maxValue, Math.max(minValue, currentPercentage * maxValue));
            _this.setState({
                isAdjusting: true,
                currentValue: newValue
            });
            if (onChanged) {
                onChanged(newValue);
            }
            ev.preventDefault();
            ev.stopPropagation();
        };
        _this._onMouseUp = function (ev) {
            _this._events.off();
            _this.setState({
                isAdjusting: false,
                origin: undefined
            });
        };
        var value = _this.props.value;
        _this.state = {
            isAdjusting: false,
            origin: undefined,
            currentValue: value
        };
        return _this;
    }
    ColorSlider.prototype.componentWillReceiveProps = function (newProps) {
        if (newProps && newProps.value) {
            this.setState({ currentValue: newProps.value });
        }
    };
    ColorSlider.prototype.render = function () {
        var _a = this.props, className = _a.className, minValue = _a.minValue, maxValue = _a.maxValue, overlayStyle = _a.overlayStyle;
        var _b = this.state, currentValue = _b.currentValue, isAdjusting = _b.isAdjusting;
        var currentPercentage = (100 * (currentValue - minValue)) / (maxValue - minValue);
        return (React.createElement("div", { ref: this._root, className: css('ms-ColorPicker-slider', styles.slider, className, isAdjusting && 'is-adjusting'), onMouseDown: this._onMouseDown },
            React.createElement("div", { className: css('ms-ColorPicker-sliderOverlay', styles.sliderOverlay), style: overlayStyle }),
            React.createElement("div", { className: css('ms-ColorPicker-thumb is-slider', styles.thumb, styles.thumbIsSlider), style: { left: currentPercentage + '%' } })));
    };
    ColorSlider.defaultProps = {
        minValue: 0,
        maxValue: 100,
        thumbColor: 'inherit',
        value: 0
    };
    return ColorSlider;
}(BaseComponent));
export { ColorSlider };
//# sourceMappingURL=ColorSlider.js.map