"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColorPicker = void 0;
const react_1 = __importStar(require("react"));
const AlphaSlider_1 = require("./AlphaSlider");
const HueSlider_1 = require("./HueSlider");
const Inputs_1 = require("./Inputs");
const RgbPicker_1 = require("./RgbPicker");
const Swatch_1 = require("./Swatch");
const utils_1 = require("./utils");
const inlines_1 = require("inlines");
const components_1 = require("../../components");
const varsUtilities_1 = require("../../varsUtilities");
const hooks_1 = require("../../hooks");
const ColorPicker = ({ value: valueProp = 'rgba(255,0,0,1)', style, onChange: onChangeProp, }) => {
    const [value, setValue] = (0, hooks_1.useControllableState)({
        prop: valueProp,
        // defaultProp: defaultValueProp,
        onChange: onChangeProp,
    });
    const rgba = (0, utils_1.rgbaToArr)(value);
    const [rgb, setRgb] = (0, react_1.useState)(rgba.slice(0, 3));
    const [hue, setHue] = (0, react_1.useState)((0, utils_1.rgbToHue)(rgb));
    const [alpha, setAlpha] = (0, react_1.useState)(rgba[3]);
    const colorValue = `rgba(${rgb.map(Math.round).join(',')},${alpha})`;
    (0, react_1.useEffect)(() => {
        if (value !== value) {
            const rgb = rgba.slice(0, 3);
            setRgb(rgb);
            setHue((0, utils_1.rgbToHue)(rgb));
            setAlpha(rgba[3]);
        }
    }, [value]);
    (0, react_1.useEffect)(() => {
        if (setValue && colorValue !== value) {
            setValue(colorValue);
        }
    }, [setValue, colorValue]);
    return (react_1.default.createElement(components_1.Column, { style: {
            border: `1px solid ${(0, varsUtilities_1.color)('border', 'default')}`,
            backgroundColor: (0, varsUtilities_1.color)('background', 'default'),
            borderRadius: 4,
            padding: 8,
            height: 300,
            minWidth: 250,
            ...style,
        } },
        react_1.default.createElement(RgbPicker_1.RgbPicker, { hue: hue, rgb: rgb, onChange: setRgb, style: {
                marginBottom: 8,
            } }),
        react_1.default.createElement(inlines_1.styled.div, { style: { display: 'flex', marginBottom: 8 } },
            react_1.default.createElement(inlines_1.styled.div, { style: { flexGrow: 1, marginRight: 8 } },
                react_1.default.createElement(HueSlider_1.HueSlider, { hue: hue, onChange: (newHue) => {
                        const { x, y } = (0, utils_1.rgbToXY)(rgb, hue);
                        const newRgb = (0, utils_1.xyToRgb)(x, y, newHue);
                        setRgb(newRgb);
                        setHue(newHue);
                    }, style: {
                        marginBottom: 8,
                    } }),
                react_1.default.createElement(AlphaSlider_1.AlphaSlider, { rgb: rgb, alpha: alpha, onChange: setAlpha })),
            react_1.default.createElement(Swatch_1.Swatch, { color: colorValue, size: 48 })),
        react_1.default.createElement(Inputs_1.Inputs, { rgb: rgb, alpha: alpha, onRgbChange: (newRgb) => {
                const newHue = (0, utils_1.rgbToHue)(newRgb);
                setRgb(newRgb);
                setHue(newHue);
            }, onAlphaChange: setAlpha })));
};
exports.ColorPicker = ColorPicker;
//# sourceMappingURL=index.js.map