"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__, {
    ApTextArea: ()=>ApTextArea
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const material_namespaceObject = require("@mui/material");
const styles_namespaceObject = require("@mui/material/styles");
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");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const index_cjs_namespaceObject = require("../ap-typography/index.cjs");
const StyledTextareaAutosize = (0, styles_namespaceObject.styled)(material_namespaceObject.TextareaAutosize, {
    shouldForwardProp: (prop)=>'hasError' !== prop && 'width' !== prop && 'resize' !== prop
})(({ hasError, width, resize = 'vertical' })=>({
        width: width || '100%',
        padding: `${apollo_core_default().Spacing.SpacingXs} ${apollo_core_default().Spacing.SpacingS}`,
        fontSize: apollo_core_default().FontFamily.FontMSize,
        lineHeight: apollo_core_default().FontFamily.FontMLineHeight,
        fontFamily: apollo_core_default().FontFamily.FontMFamily,
        color: 'var(--color-foreground)',
        backgroundColor: 'var(--color-background)',
        border: `${apollo_core_default().Border.BorderThickS} solid ${hasError ? 'var(--color-error-icon)' : 'var(--color-border)'}`,
        borderRadius: apollo_core_default().Border.BorderRadiusS,
        resize,
        outline: 'none',
        transition: 'border-color 0.2s ease-in-out',
        '&::placeholder': {
            color: 'var(--color-foreground-de-emp)'
        },
        '&:hover:not(:disabled):not(:focus)': {
            borderColor: hasError ? 'var(--color-error-icon)' : 'var(--color-border)'
        },
        '&:focus': {
            borderColor: hasError ? 'var(--color-error-icon)' : 'var(--color-focus-indicator)',
            boxShadow: hasError ? `0 0 0 ${apollo_core_default().Border.BorderThickS} var(--color-error-icon)` : `0 0 0 ${apollo_core_default().Border.BorderThickS} var(--color-focus-indicator)`
        },
        '&:disabled': {
            backgroundColor: 'var(--color-background-disabled)',
            color: 'var(--color-foreground-de-emp)',
            cursor: 'not-allowed'
        },
        '&:read-only': {
            backgroundColor: 'var(--color-background-secondary)',
            cursor: 'default'
        }
    }));
const ApTextArea = /*#__PURE__*/ external_react_default().forwardRef(({ label, value, placeholder, readOnly = false, required = false, disabled = false, errorMessage, helperText, width, rows = 4, minRows, maxRows, characterLimit, resize = 'vertical', dataTestid, onChange, ...restProps }, ref)=>{
    const [cursorPosition, setCursorPosition] = (0, external_react_namespaceObject.useState)(null);
    const textareaRef = (0, external_react_namespaceObject.useRef)(null);
    const hasError = Boolean(errorMessage);
    const stringValue = 'string' == typeof value ? value : value?.toString() || '';
    const currentLength = stringValue.length;
    const showCharacterCounter = void 0 !== characterLimit;
    const isOverLimit = void 0 !== characterLimit && currentLength > characterLimit;
    const handleChange = (0, external_react_namespaceObject.useCallback)((event)=>{
        const newValue = event.target.value;
        const textarea = event.target;
        if (void 0 !== characterLimit && newValue.length > characterLimit) {
            const start = textarea.selectionStart;
            const trimmedValue = newValue.slice(0, characterLimit);
            event.target.value = trimmedValue;
            setCursorPosition(Math.min(start, characterLimit));
        }
        onChange?.(event);
    }, [
        characterLimit,
        onChange
    ]);
    external_react_default().useEffect(()=>{
        if (null !== cursorPosition && textareaRef.current) {
            textareaRef.current.setSelectionRange(cursorPosition, cursorPosition);
            setCursorPosition(null);
        }
    }, [
        cursorPosition
    ]);
    const combinedRef = (0, external_react_namespaceObject.useCallback)((node)=>{
        textareaRef.current = node;
        if ('function' == typeof ref) ref(node);
        else if (ref) ref.current = node;
    }, [
        ref
    ]);
    const labelId = label ? `textarea-label-${dataTestid || 'default'}` : void 0;
    const helperId = helperText || errorMessage ? `textarea-helper-${dataTestid || 'default'}` : void 0;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(material_namespaceObject.Box, {
        sx: {
            width: width || '100%'
        },
        children: [
            label && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Box, {
                sx: {
                    marginBottom: apollo_core_default().Spacing.SpacingMicro
                },
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(index_cjs_namespaceObject.ApTypography, {
                    variant: apollo_core_namespaceObject.FontVariantToken.fontSizeM,
                    id: labelId,
                    style: {
                        color: 'var(--color-foreground)'
                    },
                    children: [
                        label,
                        required && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Box, {
                            component: "span",
                            sx: {
                                color: 'var(--color-error-text)',
                                marginLeft: apollo_core_default().Spacing.SpacingMicro
                            },
                            children: "*"
                        })
                    ]
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(StyledTextareaAutosize, {
                ref: combinedRef,
                value: stringValue,
                placeholder: placeholder,
                readOnly: readOnly,
                disabled: disabled,
                hasError: hasError,
                width: width,
                minRows: void 0 !== minRows ? minRows : rows,
                maxRows: void 0 !== maxRows ? maxRows : void 0 !== rows ? rows : void 0,
                resize: resize,
                onChange: handleChange,
                "data-testid": dataTestid,
                "aria-labelledby": labelId,
                "aria-describedby": helperId,
                "aria-invalid": hasError,
                "aria-required": required,
                ...restProps
            }),
            (errorMessage || helperText || showCharacterCounter) && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(material_namespaceObject.Box, {
                sx: {
                    display: 'flex',
                    justifyContent: 'space-between',
                    alignItems: 'flex-start',
                    marginTop: apollo_core_default().Spacing.SpacingMicro,
                    gap: apollo_core_default().Spacing.SpacingXs
                },
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(material_namespaceObject.Box, {
                        sx: {
                            flex: 1
                        },
                        children: [
                            errorMessage && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
                                id: helperId,
                                style: {
                                    color: 'var(--color-error-text)'
                                },
                                children: errorMessage
                            }),
                            !errorMessage && helperText && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
                                id: helperId,
                                style: {
                                    color: 'var(--color-foreground-de-emp)'
                                },
                                children: helperText
                            })
                        ]
                    }),
                    showCharacterCounter && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(index_cjs_namespaceObject.ApTypography, {
                        variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
                        style: {
                            color: isOverLimit ? 'var(--color-error-text)' : 'var(--color-foreground-de-emp)',
                            whiteSpace: 'nowrap'
                        },
                        children: [
                            currentLength,
                            "/",
                            characterLimit
                        ]
                    })
                ]
            })
        ]
    });
});
ApTextArea.displayName = 'ApTextArea';
exports.ApTextArea = __webpack_exports__.ApTextArea;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "ApTextArea"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
