import { jsxs, jsx } from 'react/jsx-runtime';
import { useState, useRef } from 'react';
import Typography from '../Typography/Typography.js';
import HighlightText from '../HighlightText/HighlightText.js';
import Icon from '../Icon/Icon.js';
import Tooltip from '../Tooltip/Tooltip.js';
import { capitalize } from '../../utils/capitalize/capitalize.js';
import Input from '../Input/Input.js';
import { useKeyboardActions } from '../../utils/keyBoardActionUtil/UseKeyboardActions.js';

const getErrorMessage = (inputValue, text, customError, customErrorCondition) => {
    if (inputValue === text) {
        return 'No changes were made.';
    }
    if (!inputValue) {
        return 'Text is required';
    }
    if (inputValue.length < 3) {
        return 'Please enter at least 3 characters.';
    }
    if (customErrorCondition) {
        return customError ?? '';
    }
    return '';
};
const EditTextField = ({ label, text, highlightText, customError, customErrorCondition = false, confirmIcon, cancelIcon, editIcon, dropdownData = [], 
// width = '',
// height = '',
isOpen = false, confirmAction, onClick, nameTooltipTitle, }) => {
    const [isEditing, setIsEditing] = useState(isOpen ?? false);
    const [inputValue, setInputValue] = useState(text);
    const [disabled, isDisabled] = useState(true);
    const [dropdownValue, setDropdownValue] = useState(dropdownData[0]?.value ?? '');
    const [showError, setShowError] = useState('');
    const [isTextFieldModified, setIsTextFieldModified] = useState(false);
    const isThrowingError = showError && isEditing ? true : false;
    const containerRef = useRef(null);
    const cancelRef = useRef(null);
    const handleEditButtonClick = () => {
        setIsEditing(true);
        setShowError('');
    };
    const handleConfirm = () => {
        const errorMessage = getErrorMessage(inputValue, text, customError, customErrorCondition);
        if (errorMessage && isEditing) {
            setShowError(errorMessage);
        }
        else {
            setIsEditing(false);
            setShowError('');
            if (confirmAction)
                confirmAction(inputValue, dropdownValue);
        }
    };
    const handleCancel = () => {
        setInputValue(text);
        setDropdownValue(dropdownData[0]?.value ?? '');
        setIsEditing(false);
        setShowError('');
        setIsTextFieldModified(false);
    };
    const handleTextFieldChange = (e) => {
        setInputValue(e.target.value);
        setIsTextFieldModified(true);
        setShowError('');
        isDisabled(false);
    };
    function handleKeyBoard(key) {
        if (key === 'Enter') {
            handleConfirm();
        }
        if (key === 'Escape') {
            handleCancel();
        }
    }
    useKeyboardActions([
        { key: 'Enter', action: () => handleKeyBoard('Enter') },
        { key: 'Escape', action: () => handleKeyBoard('Escape') },
    ], containerRef);
    return (jsxs("div", { className: "ff-label-edit-text-field", ref: containerRef, children: [isEditing ? (jsxs("div", { className: "ff-label-text-field", children: [jsx("div", { className: "ff-label-text-field-without-dropdown", children: jsx(Input, { name: "input", label: label ? label : '', disabled: false, error: isThrowingError, placeholder: "Enter your name", value: inputValue, onChange: handleTextFieldChange, className: `
                
               ${isTextFieldModified ? 'modified' : ''}`, type: "text" }) }), jsxs("div", { className: "ff-icon-container", children: [confirmIcon && (jsx(Tooltip, { title: "confirm", placement: 'bottom', children: jsx(Icon, { color: confirmIcon?.color ?? 'var(--default-confirm-icon-color)', height: confirmIcon?.height ?? 20, disabled: disabled, width: confirmIcon?.width ?? 20, name: confirmIcon?.name ?? 'default-confirm-icon', className: `${confirmIcon?.className ?? 'default-confirm-class'} ${disabled ? 'disabled-confirm-icon' : 'confirm-icon'}`, onClick: handleConfirm }) })), cancelIcon && (jsx(Tooltip, { title: "cancel", placement: 'bottom', children: jsx(Icon, { color: cancelIcon?.color ?? 'var(--label-edit-cancel-icon)', height: cancelIcon?.height ?? 12, width: cancelIcon?.width ?? 20, name: cancelIcon?.name ?? 'default-cancel-icon', className: cancelIcon?.className ?? 'cancel-icon', onClick: handleCancel, ref: cancelRef }) }))] })] })) : (jsxs("div", { className: "display-text-container", children: [nameTooltipTitle ? (jsx(Tooltip, { title: nameTooltipTitle, children: jsx("span", { className: "display-text", onClick: onClick, children: jsx(HighlightText, { text: inputValue, highlight: highlightText }) }) })) : (jsx("span", { className: "display-text", onClick: onClick, children: jsx(HighlightText, { text: inputValue, highlight: highlightText }) })), jsx(Tooltip, { title: capitalize(editIcon?.name ?? 'Edit'), children: jsx(Icon, { color: editIcon?.color ?? 'var(--label-edit-cancel-icon)', height: editIcon?.height ?? 20, width: editIcon?.width ?? 20, name: editIcon?.name ?? 'edit', className: editIcon?.className ?? 'edit-icon', onClick: handleEditButtonClick }) })] })), showError && isEditing && (jsx(Typography, { as: "p", fontSize: 8, className: "error-text", children: showError }))] }));
};

export { EditTextField as default };
//# sourceMappingURL=EditTextField.js.map
