"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var React = require("react");
var Utilities_1 = require("../../Utilities");
var colors_1 = require("./colors");
var ColorPicker_scss_1 = require("./ColorPicker.scss");
var ColorRectangle = (function (_super) {
    __extends(ColorRectangle, _super);
    function ColorRectangle(props) {
        var _this = _super.call(this, props) || this;
        var color = _this.props.color;
        _this._events = new Utilities_1.EventGroup(_this);
        _this.state = {
            isAdjusting: false,
            origin: null,
            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', ColorPicker_scss_1.default.colorRect), style: { minWidth: minSize, minHeight: minSize, backgroundColor: fullColorString }, onMouseDown: this._onMouseDown },
            React.createElement("div", { className: Utilities_1.css('ms-ColorPicker-light', ColorPicker_scss_1.default.light) }),
            React.createElement("div", { className: Utilities_1.css('ms-ColorPicker-dark', ColorPicker_scss_1.default.dark) }),
            React.createElement("div", { className: Utilities_1.css('ms-ColorPicker-thumb', ColorPicker_scss_1.default.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: null
        });
    };
    return ColorRectangle;
}(React.Component));
ColorRectangle.defaultProps = {
    minSize: 220
};
__decorate([
    Utilities_1.autobind
], ColorRectangle.prototype, "_onMouseDown", null);
__decorate([
    Utilities_1.autobind
], ColorRectangle.prototype, "_onMouseMove", null);
__decorate([
    Utilities_1.autobind
], ColorRectangle.prototype, "_onMouseUp", null);
exports.ColorRectangle = ColorRectangle;

//# sourceMappingURL=ColorRectangle.js.map
