'use client';
import { jsx as _jsx } from "react/jsx-runtime";
import { useState, useRef, useEffect, useCallback } from 'react';
export function TableCellEditor({ value, type, options, onSave, onCancel }) {
    const formatValue = useCallback(() => {
        if (value === undefined || value === null)
            return '';
        if (value instanceof Date) {
            return value.toISOString().split('T')[0];
        }
        return String(value);
    }, [value]);
    const [inputValue, setInputValue] = useState(formatValue);
    const inputRef = useRef(null);
    useEffect(() => {
        inputRef.current?.focus();
    }, []);
    const handleKeyDown = useCallback((e) => {
        if (e.key === 'Enter') {
            e.preventDefault();
            onSave(inputValue);
        }
        if (e.key === 'Escape') {
            e.preventDefault();
            onCancel();
        }
    }, [inputValue, onSave, onCancel]);
    if (type === 'select') {
        return (_jsx("select", { ref: inputRef, "data-testid": "table-cell-editor-select", className: "bg-background border-primary h-7 w-full rounded border px-1.5 text-xs focus:outline-none", value: inputValue, onChange: (e) => {
                setInputValue(e.target.value);
                onSave(e.target.value);
            }, onKeyDown: handleKeyDown, onBlur: onCancel, children: options?.map((opt) => (_jsx("option", { value: opt.value, children: opt.label }, opt.value))) }));
    }
    if (type === 'date') {
        return (_jsx("input", { ref: inputRef, "data-testid": "table-cell-editor-date", type: "date", className: "bg-background border-primary h-7 w-full rounded border px-1.5 text-xs focus:outline-none", value: inputValue, onChange: (e) => setInputValue(e.target.value), onKeyDown: handleKeyDown, onBlur: () => onSave(inputValue) }));
    }
    return (_jsx("input", { ref: inputRef, "data-testid": "table-cell-editor-text", type: "text", className: "bg-background border-primary h-7 w-full rounded border px-1.5 text-xs focus:outline-none", value: inputValue, onChange: (e) => setInputValue(e.target.value), onKeyDown: handleKeyDown, onBlur: () => onSave(inputValue) }));
}
