"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.ColorInput = void 0;
const Popover = __importStar(require("@radix-ui/react-popover"));
const react_1 = __importStar(require("react"));
const inlines_1 = require("inlines");
const varsUtilities_1 = require("../../varsUtilities");
const utils_1 = require("../ColorPicker/utils");
const ColorPicker_1 = require("../ColorPicker");
const bg_1 = require("../ColorPicker/bg");
const hooks_1 = require("../../hooks");
let tester;
const valueToRgba = (value) => {
    if (value) {
        if (value.startsWith('rgba(')) {
            return value;
        }
        if (!tester) {
            tester = document.createElement('div');
            document.documentElement.appendChild(tester);
        }
        tester.style.backgroundColor = value;
        const { backgroundColor } = getComputedStyle(tester);
        const rgba = (0, utils_1.rgbaToArr)(backgroundColor);
        return `rgba(${rgba.join(',')})`;
    }
};
const ColorInput = ({ placeholder, defaultValue: defaultValueProp = valueToRgba(''), value: valueProp, disabled, style, error, autoFocus, onChange: onChangeProp = (e) => console.log(), ...props }) => {
    const [value, setValue] = (0, hooks_1.useControllableState)({
        prop: valueProp,
        defaultProp: defaultValueProp,
        onChange: onChangeProp,
    });
    const [inputVal, setInputVal] = (0, react_1.useState)(value);
    (0, react_1.useEffect)(() => {
        setInputVal(value);
    }, [value]);
    const [open, setOpen] = (0, react_1.useState)(false);
    return (react_1.default.createElement(Popover.Root, { open: open },
        react_1.default.createElement(Popover.Trigger, { asChild: true },
            react_1.default.createElement(inlines_1.styled.div, { tabIndex: 0, style: {
                    display: 'flex',
                    position: 'relative',
                } },
                react_1.default.createElement(inlines_1.styled.input, { autoFocus: autoFocus, ...props, type: "text", value: inputVal || '', onChange: (e) => {
                        setValue(e.target.value);
                    }, placeholder: placeholder, disabled: disabled, style: {
                        width: '100%',
                        padding: '0px 12px',
                        paddingLeft: 36,
                        borderRadius: 8,
                        minHeight: 40,
                        fontSize: '14px',
                        lineHeight: '24px',
                        boxSizing: 'border-box',
                        color: (0, varsUtilities_1.color)('content', 'default', value === valueProp ? 'secondary' : 'primary'),
                        cursor: disabled ? 'not-allowed' : null,
                        backgroundColor: (0, varsUtilities_1.color)('background', 'default'),
                        border: `1px solid ${(0, varsUtilities_1.color)('inputBorder', 'neutralNormal')}`,
                        '&:hover': {
                            border: `1px solid ${(0, varsUtilities_1.color)('inputBorder', 'neutralHover')}`,
                        },
                        '&:focus': {
                            border: `1px solid ${(0, varsUtilities_1.color)('inputBorder', 'active')}`,
                            boxShadow: error
                                ? `0 0 0 2px ${(0, varsUtilities_1.color)('inputBorder', 'alert', 'default')}`
                                : `0 0 0 2px ${(0, varsUtilities_1.color)('border', 'brand', 'subtle')}`,
                        },
                        outline: 'none',
                        ...style,
                    } }),
                react_1.default.createElement("div", { style: {
                        cursor: 'pointer',
                        position: 'absolute',
                        left: 12,
                        top: '50%',
                        transform: 'translate3d(0,-50%,0)',
                        height: 20,
                        width: 20,
                        borderRadius: 4,
                        marginRight: 8,
                        marginLeft: -4,
                        background: bg_1.transparent,
                        backgroundPosition: 'center center',
                    } }),
                react_1.default.createElement("button", { style: {
                        cursor: 'pointer',
                        position: 'absolute',
                        left: 12,
                        top: '50%',
                        transform: 'translate3d(0,-50%,0)',
                        height: 20,
                        width: 20,
                        borderRadius: 4,
                        marginRight: 8,
                        marginLeft: -4,
                        backgroundColor: value,
                        userSelect: 'none',
                        WebkitUserSelect: 'none',
                        border: `1px solid ${(0, varsUtilities_1.color)('inputBorder', 'neutralNormal')}`,
                        pointerEvents: disabled ? 'none' : null,
                    }, onClick: () => setOpen(true) }))),
        react_1.default.createElement(Popover.Portal, null,
            react_1.default.createElement(Popover.Content, { onInteractOutside: (e) => {
                    setOpen(false);
                }, onOpenAutoFocus: (e) => {
                    e.preventDefault();
                }, onCloseAutoFocus: (e) => {
                    e.preventDefault();
                }, sideOffset: 8 },
                react_1.default.createElement(ColorPicker_1.ColorPicker, { style: { maxWidth: 'calc(100vw - 32px)' }, value: value, onChange: (v) => setValue(v) })))));
};
exports.ColorInput = ColorInput;
//# sourceMappingURL=ColorInput.js.map