import * as React from 'react';
import { useCallback, useEffect } from 'react';
import debounce from 'lodash/debounce';
import Input from '../Input';
import { getVariableColor } from '../../Utilities/Helpers/StyleHelper';
import tinycolor from 'tinycolor2';
import { Box, Flex } from '../Flex';
import { twMerge } from '../../twMerge';
import clsx from 'clsx';
export const ColorPicker = React.forwardRef((props, ref) => {
    const ColorPalette = props.api.userInterfaceApi.getColorPalette();
    let { api, value, includeAlpha = true, ...restProps } = props;
    // Create a debounced version of onChange
    // we need this because moving the mouse A LOT in the custom color picker can break the React rendering
    const debouncedOnChange = useCallback(debounce((color) => {
        props.onChange(color);
    }, 30), [props.onChange]);
    // Clean up the debounce on unmount
    useEffect(() => {
        return () => {
            debouncedOnChange.cancel();
        };
    }, [debouncedOnChange]);
    const optionsMap = ColorPalette.reduce((acc, colorItem) => ({ ...acc, [getVariableColor(colorItem)]: colorItem }), {});
    const ABcolorChoicesOptions = Object.keys(optionsMap).map((x) => {
        return (React.createElement("option", { key: x, value: x }, x));
    });
    const ABcolorChoices = React.createElement("datalist", { id: 'ABcolorChoices' }, ABcolorChoicesOptions);
    const preparedValue = React.useMemo(() => {
        const color = getVariableColor(value);
        return tinycolor(color).toHexString();
    }, [value]);
    const preparedAlphaColor = React.useMemo(() => {
        const color = getVariableColor(value);
        return [tinycolor(color).setAlpha(0).toRgbString(), tinycolor(color).setAlpha(1).toRgbString()];
    }, [value]);
    const rangeBackround = `linear-gradient(90deg, ${preparedAlphaColor[0]} 0%, ${preparedAlphaColor[1]} 100%)`;
    const alpha = tinycolor(value).getAlpha();
    return (React.createElement(Flex, { className: clsx('ColorPicker twa:items-center twa:gap-2', {
            'twa:opacity-30': props.disabled,
        }, props.className) },
        React.createElement(Input, { ...restProps, disabled: props.disabled, className: twMerge('twa:w-[70px] twa:p-0', restProps.className), onChange: (event) => {
                /**
                 * The value is not in the map when the color is not in the palette.
                 */
                const color = optionsMap[event.target.value] ?? event.target.value;
                debouncedOnChange(color);
            }, value: preparedValue, ref: ref, type: "color", list: "ABcolorChoices", title: props.title }),
        ABcolorChoices,
        includeAlpha && (React.createElement(Flex, { alignItems: "center", className: "twa:gap-1" },
            React.createElement(Box, null, "Opacity"),
            React.createElement(Flex, { alignItems: "center", className: "twa:min-h-input" },
                React.createElement(Input, { disabled: props.disabled, className: "ab-ColorPicker-range", style: { background: rangeBackround }, value: alpha, onChange: (event) => {
                        const color = tinycolor(value).setAlpha(event.target.value).toRgbString();
                        debouncedOnChange(color);
                    }, min: 0, max: 1, step: 0.01, type: "range" }))))));
});
