"use client";
'use strict';

var jsxRuntime = require('react/jsx-runtime');
var index = require('../../node_modules/.pnpm/color@5.0.0/node_modules/color/index.cjs');
var React = require('react');
var flex = require('../flex/flex.cjs');
var utils = require('./utils.cjs');

const ColorPickerContext = React.createContext(undefined);
const useColorPicker = () => {
    const context = React.useContext(ColorPickerContext);
    if (!context) {
        throw new Error('useColorPicker must be used within a ColorPickerProvider');
    }
    return context;
};
const ColorPickerRoot = ({ value, defaultValue = '#ffffff', onValueChange, defaultMode = 'hex', mode: providedMode, onModeChange, ...props }) => {
    const providedColor = value && index.default(value).hsl().object();
    const [internalColor, setInternalColor] = React.useState(index.default(defaultValue).hsl().object());
    const [internalMode, setInternalMode] = React.useState(defaultMode);
    const hue = providedColor ? providedColor.h : internalColor.h;
    const saturation = providedColor ? providedColor.s : internalColor.s;
    const lightness = providedColor ? providedColor.l : internalColor.l;
    const alpha = (providedColor ? providedColor?.alpha : internalColor?.alpha) ?? 1;
    const mode = providedMode ?? internalMode;
    const setColor = React.useCallback(value => {
        setInternalColor(_color => {
            const updatedColor = { ..._color, ...value };
            if (!onValueChange)
                return updatedColor;
            const color = index.default.hsl(updatedColor.h, updatedColor.s, updatedColor.l, updatedColor?.alpha ?? 1);
            onValueChange(utils.getColorString(color, mode), mode);
            return updatedColor;
        });
    }, [mode, onValueChange]);
    const setMode = React.useCallback(value => {
        setInternalMode(value);
        onModeChange?.(value);
    }, [onModeChange]);
    return (jsxRuntime.jsx(ColorPickerContext.Provider, { value: {
            hue,
            saturation,
            lightness,
            alpha,
            mode,
            setColor,
            setMode
        }, children: jsxRuntime.jsx(flex.Flex, { direction: 'column', gap: 4, ...props }) }));
};

exports.ColorPickerRoot = ColorPickerRoot;
exports.useColorPicker = useColorPicker;
//# sourceMappingURL=color-picker-root.cjs.map
