import * as tslib_1 from "tslib";
import * as React from 'react';
import { BaseComponent, assign, createRef, classNamesFunction } from '../../../Utilities';
import { MAX_COLOR_SATURATION, MAX_COLOR_VALUE, getFullColorString, hsv2hex } from '../../../utilities/color/colors';
var getClassNames = classNamesFunction();
var ColorRectangleBase = /** @class */ (function (_super) {
    tslib_1.__extends(ColorRectangleBase, _super);
    function ColorRectangleBase(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) {
            var _a = _this.props, color = _a.color, onSVChanged = _a.onSVChanged;
            if (!_this._root.current) {
                return;
            }
            var rectSize = _this._root.current.getBoundingClientRect();
            var sPercentage = (ev.clientX - rectSize.left) / rectSize.width;
            var vPercentage = (ev.clientY - rectSize.top) / rectSize.height;
            var newColor = assign({}, color, {
                s: Math.min(MAX_COLOR_SATURATION, Math.max(0, sPercentage * MAX_COLOR_SATURATION)),
                v: Math.min(MAX_COLOR_VALUE, Math.max(0, MAX_COLOR_VALUE - vPercentage * MAX_COLOR_VALUE))
            });
            newColor.hex = hsv2hex(newColor.h, newColor.s, newColor.v);
            newColor.str = newColor.a === 100 ? '#' + newColor.hex : "rgba(" + newColor.r + ", " + newColor.g + ", " + newColor.b + ", " + newColor.a / 100 + ")";
            _this.setState({
                isAdjusting: true,
                color: newColor
            });
            if (onSVChanged) {
                onSVChanged(newColor.s, newColor.v);
            }
            ev.preventDefault();
            ev.stopPropagation();
        };
        _this._onMouseUp = function (ev) {
            _this._events.off();
            _this.setState({
                isAdjusting: false,
                origin: undefined
            });
        };
        var color = _this.props.color;
        _this.state = {
            isAdjusting: false,
            origin: undefined,
            color: color,
            fullColorString: getFullColorString(color)
        };
        return _this;
    }
    ColorRectangleBase.prototype.componentWillUnmount = function () {
        this._events.dispose();
    };
    ColorRectangleBase.prototype.componentWillReceiveProps = function (newProps) {
        var color = newProps.color;
        this.setState({
            color: color,
            fullColorString: getFullColorString(color)
        });
    };
    ColorRectangleBase.prototype.render = function () {
        var _a = this.props, minSize = _a.minSize, theme = _a.theme, className = _a.className, styles = _a.styles;
        var _b = this.state, color = _b.color, fullColorString = _b.fullColorString;
        var classNames = getClassNames(styles, {
            theme: theme,
            className: className
        });
        return (React.createElement("div", { ref: this._root, className: classNames.root, style: { minWidth: minSize, minHeight: minSize, backgroundColor: fullColorString }, onMouseDown: this._onMouseDown },
            React.createElement("div", { className: classNames.light }),
            React.createElement("div", { className: classNames.dark }),
            React.createElement("div", { className: classNames.thumb, style: { left: color.s + '%', top: MAX_COLOR_VALUE - color.v + '%', backgroundColor: color.str } })));
    };
    ColorRectangleBase.defaultProps = {
        minSize: 220
    };
    return ColorRectangleBase;
}(BaseComponent));
export { ColorRectangleBase };
//# sourceMappingURL=ColorRectangle.base.js.map