"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = require("react");
var Utilities_1 = require("../../Utilities");
var colors_1 = require("../../utilities/color/colors");
var stylesImport = require("./ColorPicker.scss");
var styles = stylesImport;
var ColorRectangle = (function (_super) {
    tslib_1.__extends(ColorRectangle, _super);
    function ColorRectangle(props) {
        var _this = _super.call(this, props) || this;
        var color = _this.props.color;
        _this.state = {
            isAdjusting: false,
            origin: undefined,
            color: color,
            fullColorString: colors_1.getFullColorString(color)
        };
        return _this;
    }
    ColorRectangle.prototype.componentWillUnmount = function () {
        this._events.dispose();
    };
    ColorRectangle.prototype.componentWillReceiveProps = function (newProps) {
        var color = newProps.color;
        this.setState({
            color: color,
            fullColorString: colors_1.getFullColorString(color)
        });
    };
    ColorRectangle.prototype.render = function () {
        var minSize = this.props.minSize;
        var _a = this.state, color = _a.color, fullColorString = _a.fullColorString;
        return (React.createElement("div", { ref: 'root', className: Utilities_1.css('ms-ColorPicker-colorRect', styles.colorRect), style: { minWidth: minSize, minHeight: minSize, backgroundColor: fullColorString }, onMouseDown: this._onMouseDown },
            React.createElement("div", { className: Utilities_1.css('ms-ColorPicker-light', styles.light) }),
            React.createElement("div", { className: Utilities_1.css('ms-ColorPicker-dark', styles.dark) }),
            React.createElement("div", { className: Utilities_1.css('ms-ColorPicker-thumb', styles.thumb), style: { left: color.s + '%', top: (colors_1.MAX_COLOR_VALUE - color.v) + '%', backgroundColor: color.str } })));
    };
    ColorRectangle.prototype._onMouseDown = function (ev) {
        this._events.on(window, 'mousemove', this._onMouseMove, true);
        this._events.on(window, 'mouseup', this._onMouseUp, true);
        this._onMouseMove(ev);
    };
    ColorRectangle.prototype._onMouseMove = function (ev) {
        var _a = this.props, color = _a.color, onSVChanged = _a.onSVChanged;
        var rectSize = this.refs.root.getBoundingClientRect();
        var sPercentage = (ev.clientX - rectSize.left) / rectSize.width;
        var vPercentage = (ev.clientY - rectSize.top) / rectSize.height;
        var newColor = Utilities_1.assign({}, color, {
            s: Math.min(colors_1.MAX_COLOR_SATURATION, Math.max(0, sPercentage * colors_1.MAX_COLOR_SATURATION)),
            v: Math.min(colors_1.MAX_COLOR_VALUE, Math.max(0, colors_1.MAX_COLOR_VALUE - (vPercentage * colors_1.MAX_COLOR_VALUE))),
        });
        newColor.hex = colors_1.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();
    };
    ColorRectangle.prototype._onMouseUp = function (ev) {
        this._events.off();
        this.setState({
            isAdjusting: false,
            origin: undefined
        });
    };
    return ColorRectangle;
}(Utilities_1.BaseComponent));
ColorRectangle.defaultProps = {
    minSize: 220
};
tslib_1.__decorate([
    Utilities_1.autobind
], ColorRectangle.prototype, "_onMouseDown", null);
tslib_1.__decorate([
    Utilities_1.autobind
], ColorRectangle.prototype, "_onMouseMove", null);
tslib_1.__decorate([
    Utilities_1.autobind
], ColorRectangle.prototype, "_onMouseUp", null);
exports.ColorRectangle = ColorRectangle;

//# sourceMappingURL=ColorRectangle.js.map
