"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    ApTextField: ()=>ApTextField
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const material_namespaceObject = require("@mui/material");
const apollo_core_namespaceObject = require("@uipath/apollo-core");
var apollo_core_default = /*#__PURE__*/ __webpack_require__.n(apollo_core_namespaceObject);
const external_react_namespaceObject = require("react");
const index_cjs_namespaceObject = require("../ap-typography/index.cjs");
const ErrorMessage = ({ message })=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
        variant: apollo_core_namespaceObject.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 = (0, external_react_namespaceObject.useRef)(null);
    const [isOverflowing, setIsOverflowing] = (0, external_react_namespaceObject.useState)(false);
    const [compositionValue, setCompositionValue] = (0, external_react_namespaceObject.useState)(value || '');
    const isEllipsisActive = (element)=>element.offsetWidth < element.scrollWidth;
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (inputRef.current) setIsOverflowing(isEllipsisActive(inputRef.current));
        setCompositionValue(value || '');
    }, [
        value
    ]);
    const handleOnInput = (0, external_react_namespaceObject.useCallback)((event)=>{
        const value = event.target.value;
        setCompositionValue(value);
        onChange?.(value);
    }, [
        onChange
    ]);
    const cachedProps = (0, external_react_namespaceObject.useMemo)(()=>{
        let helperTextValue;
        helperTextValue = errorMessage ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ErrorMessage, {
            message: errorMessage
        }) : error ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ErrorMessage, {
            message: helperText ?? ''
        }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
            variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
            children: helperText
        });
        return {
            InputProps: {
                readOnly,
                startAdornment: startAdornment && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.InputAdornment, {
                    disableTypography: true,
                    position: "start",
                    sx: {
                        marginRight: '0px',
                        width: 'unset',
                        color: 'var(--color-foreground)',
                        fontSize: apollo_core_default().FontFamily.FontMSize
                    },
                    children: startAdornment
                }),
                endAdornment: endAdornment && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.InputAdornment, {
                    disableTypography: true,
                    position: "end",
                    sx: {
                        marginLeft: '0px',
                        width: 'unset',
                        color: 'var(--color-foreground)',
                        fontSize: apollo_core_default().FontFamily.FontMSize
                    },
                    children: endAdornment
                })
            },
            sx: {
                '& .MuiInputBase-root': {
                    height: 'tall' === size ? apollo_core_default().Spacing.SpacingXxl : apollo_core_default().Spacing.SpacingXl,
                    ...(startAdornment || endAdornment) && {
                        paddingLeft: apollo_core_default().Padding.PadL,
                        paddingRight: apollo_core_default().Padding.PadL
                    }
                },
                '& .MuiInputBase-input': {
                    ...'small' === size && {
                        height: '0px'
                    },
                    ...(startAdornment || endAdornment) && {
                        paddingLeft: apollo_core_default().Padding.PadS,
                        paddingRight: apollo_core_default().Padding.PadS
                    }
                },
                ...customStyle
            },
            helperText: helperTextValue
        };
    }, [
        readOnly,
        size,
        helperText,
        error,
        errorMessage,
        startAdornment,
        endAdornment,
        customStyle
    ]);
    const getTooltipContent = (0, external_react_namespaceObject.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__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.TextField, {
        className: className,
        label: label,
        value: compositionValue,
        onInput: handleOnInput,
        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
        },
        ...cachedProps,
        inputRef: inputRef
    });
    if (shouldShowTooltip) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Tooltip, {
        title: getTooltipContent(),
        arrow: true,
        children: textField
    });
    return textField;
}
exports.ApTextField = __webpack_exports__.ApTextField;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "ApTextField"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
