(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "tslib", "react", "../../../utils/colorConvectors/hsvToHex", "./helpers/calculateChange", "./styles"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var hsvToHex_1 = require("../../../utils/colorConvectors/hsvToHex");
    var calculateChange_1 = require("./helpers/calculateChange");
    var styles_1 = require("./styles");
    var SaturationBlock = function (_a) {
        var hsv = _a.hsv, hsl = _a.hsl, setHsv = _a.setHsv, setColorInput = _a.setColorInput;
        var container = (0, react_1.useRef)(null);
        (0, react_1.useEffect)(function () { return function () { return unbindEventListeners(); }; }, []);
        var handleChange = function (e) {
            if (container.current) {
                var change = (0, calculateChange_1.calculateChange)(e, hsl, container.current);
                setHsv(change);
                setColorInput((0, hsvToHex_1.hsvToHex)(change).toUpperCase());
            }
        };
        var handleMouseUp = function () {
            unbindEventListeners();
        };
        var handleMouseDown = function (e) {
            handleChange(e);
            var renderWindow = getContainerRenderWindow();
            if (renderWindow) {
                renderWindow.addEventListener("mousemove", handleChange);
                renderWindow.addEventListener("mouseup", handleMouseUp);
            }
        };
        var unbindEventListeners = function () {
            var renderWindow = getContainerRenderWindow();
            if (renderWindow) {
                renderWindow.removeEventListener("mousemove", handleChange);
                renderWindow.removeEventListener("mouseup", handleMouseUp);
            }
        };
        var getContainerRenderWindow = function () {
            var currentContainer = container === null || container === void 0 ? void 0 : container.current;
            var renderWindow = window;
            while (!renderWindow.document.contains(currentContainer) &&
                renderWindow.parent !== renderWindow) {
                renderWindow = renderWindow.parent;
            }
            return renderWindow;
        };
        return (react_1.default.createElement(styles_1.Saturation, { ref: container, onMouseDown: handleMouseDown, onTouchMove: handleChange, onTouchStart: handleChange, hue: hsl.h },
            react_1.default.createElement(styles_1.SaturationActiveBlock, { ref: container },
                react_1.default.createElement(styles_1.PointButton, { top: Number(String(hsv.v).substr(0, 4)), left: Number(String(hsv.s).substr(0, 4)), hex: (0, hsvToHex_1.hsvToHex)(hsv) }))));
    };
    exports.default = SaturationBlock;
});
