import { jsx } from "react/jsx-runtime";
import { InputAdornment, TextField, Tooltip } from "@mui/material";
import apollo_core, { FontVariantToken } from "@uipath/apollo-core";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { ApTypography } from "../ap-typography/index.js";
const ErrorMessage = ({ message })=>/*#__PURE__*/ jsx(ApTypography, {
        variant: FontVariantToken.fontSizeS,
        style: {
            color: 'var(--color-error-text)'
        },
        children: message
    });
function ApTextField(props) {
    const { label, value, size = 'tall', placeholder, error, errorMessage, readOnly, readOnlyTooltip, helperText, required, disabled, type = 'text', startAdornment, endAdornment, customStyle, onChange, className, min = -1 / 0, max = 1 / 0, step = 1, dataTestid } = props;
    const inputRef = useRef(null);
    const [isOverflowing, setIsOverflowing] = useState(false);
    const [compositionValue, setCompositionValue] = useState(value || '');
    const isEllipsisActive = (element)=>element.offsetWidth < element.scrollWidth;
    useEffect(()=>{
        if (inputRef.current) setIsOverflowing(isEllipsisActive(inputRef.current));
        setCompositionValue(value || '');
    }, [
        value
    ]);
    const handleOnInput = useCallback((event)=>{
        const value = event.currentTarget.value;
        setCompositionValue(value);
        onChange?.(value);
    }, [
        onChange
    ]);
    const cachedProps = useMemo(()=>{
        let helperTextValue;
        helperTextValue = errorMessage ? /*#__PURE__*/ jsx(ErrorMessage, {
            message: errorMessage
        }) : error ? /*#__PURE__*/ jsx(ErrorMessage, {
            message: helperText ?? ''
        }) : /*#__PURE__*/ jsx(ApTypography, {
            variant: FontVariantToken.fontSizeS,
            children: helperText
        });
        return {
            InputProps: {
                readOnly,
                startAdornment: startAdornment && /*#__PURE__*/ jsx(InputAdornment, {
                    disableTypography: true,
                    position: "start",
                    sx: {
                        marginRight: '0px',
                        width: 'unset',
                        color: 'var(--color-foreground)',
                        fontSize: apollo_core.FontFamily.FontMSize
                    },
                    children: startAdornment
                }),
                endAdornment: endAdornment && /*#__PURE__*/ jsx(InputAdornment, {
                    disableTypography: true,
                    position: "end",
                    sx: {
                        marginLeft: '0px',
                        width: 'unset',
                        color: 'var(--color-foreground)',
                        fontSize: apollo_core.FontFamily.FontMSize
                    },
                    children: endAdornment
                })
            },
            sx: {
                '& .MuiInputBase-root': {
                    height: 'tall' === size ? apollo_core.Spacing.SpacingXxl : apollo_core.Spacing.SpacingXl,
                    ...(startAdornment || endAdornment) && {
                        paddingLeft: apollo_core.Padding.PadL,
                        paddingRight: apollo_core.Padding.PadL
                    }
                },
                '& .MuiInputBase-input': {
                    ...'small' === size && {
                        height: '0px'
                    },
                    ...(startAdornment || endAdornment) && {
                        paddingLeft: apollo_core.Padding.PadS,
                        paddingRight: apollo_core.Padding.PadS
                    }
                },
                ...customStyle
            },
            helperText: helperTextValue
        };
    }, [
        readOnly,
        size,
        helperText,
        error,
        errorMessage,
        startAdornment,
        endAdornment,
        customStyle
    ]);
    const getTooltipContent = useCallback(()=>{
        if (readOnly && readOnlyTooltip) return readOnlyTooltip;
        if (isOverflowing && value) return value;
        return '';
    }, [
        readOnly,
        readOnlyTooltip,
        isOverflowing,
        value
    ]);
    const shouldShowTooltip = readOnly && !!readOnlyTooltip || isOverflowing && !!value;
    const textField = /*#__PURE__*/ jsx(TextField, {
        className: className,
        label: label,
        value: compositionValue,
        placeholder: placeholder,
        error: error || !!errorMessage,
        required: required,
        type: type,
        disabled: disabled,
        InputLabelProps: {
            shrink: label && placeholder ? true : void 0
        },
        inputProps: {
            'data-testid': dataTestid,
            min: 'number' === type ? min : void 0,
            max: 'number' === type ? max : void 0,
            step: 'number' === type ? step : void 0,
            onInput: handleOnInput
        },
        ...cachedProps,
        inputRef: inputRef
    });
    if (shouldShowTooltip) return /*#__PURE__*/ jsx(Tooltip, {
        title: getTooltipContent(),
        arrow: true,
        children: textField
    });
    return textField;
}
export { ApTextField };
