import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import classNames from 'classnames';
import './Textarea.scss';
import Typography from '../Typography';
import { ENTER_NEW_LINE_REGEX } from '../../validations/regex';
const getCharacterCount = (value) => {
    return value.replace(ENTER_NEW_LINE_REGEX, '').length;
};
const Textarea = ({ capacity = 200, name, label, value = '', variant = 'primary', error, helperText = '', disabled = false, required = false, placeholder = 'Enter text', className = '', onChange, onBlur, onFocus, onPaste, rows = 4, cols = 40, resize = false, errorText, readOnly = false, displayCapacity = true, isLabelRequired = true, ...props }) => {
    const labelClasses = classNames('ff-textarea-label ff-textarea-label--' + variant, {
        'ff-textarea-label--danger': error,
        'ff-textarea-label--disabled': disabled && !value,
    });
    const textareaClasses = classNames(`ff-textarea ff-textarea--${variant}`, className, {
        'ff-textarea--danger': error,
        'ff-textarea--resize': !resize,
        'ff-textarea--placeholder': !isLabelRequired,
    });
    const containerClasses = classNames('ff-textarea-container', {
        'ff-textarea-container--disabled': disabled,
        'ff-textarea-container--float': Boolean(value),
    });
    const isError = error || !value;
    const errorMessage = isError ? (_jsx("span", { className: "ff-textarea-error-text", children: _jsx(Typography, { fontSize: 10, children: errorText }) })) : null;
    const handleChange = (e) => {
        const newValue = e.target.value;
        const characterCount = getCharacterCount(newValue);
        if (characterCount <= capacity) {
            onChange?.(e);
        }
    };
    const handleKeyDown = (e) => {
        if (e.key === 'Enter') {
            const textarea = e.currentTarget;
            const cursorPos = textarea.selectionStart;
            const value = textarea.value;
            const textBeforeCursor = value.substring(0, cursorPos);
            const currentLine = textBeforeCursor.split('\n').pop() || '';
            if (currentLine.trim() === '') {
                e.preventDefault();
            }
        }
        props.onKeyDown?.(e);
    };
    const handlePaste = (e) => {
        e.preventDefault();
        const pastedText = e.clipboardData.getData('text/plain');
        const processedText = pastedText
            .split('\n')
            .filter((line) => line.trim() !== '')
            .join('\n');
        const textarea = e.currentTarget;
        const start = textarea.selectionStart;
        const end = textarea.selectionEnd;
        const textBefore = textarea.value.slice(0, start);
        const textAfter = textarea.value.slice(end);
        const currentCount = getCharacterCount(textBefore + textAfter);
        let allowedCount = capacity - currentCount;
        let limitedPastedText = '';
        for (const char of processedText) {
            if (char === '\n') {
                limitedPastedText += char;
            }
            else if (allowedCount > 0) {
                limitedPastedText += char;
                allowedCount--;
            }
        }
        const newValue = textBefore + limitedPastedText + textAfter;
        const event = {
            ...e,
            target: {
                ...textarea,
                value: newValue,
            },
        };
        onChange?.(event);
        onPaste?.(e);
    };
    return (_jsxs("div", { className: containerClasses, children: [isLabelRequired && (_jsx("label", { htmlFor: name, className: classNames('ff-textarea-label-container', {
                    'ff-textarea-label-container--danger': error,
                }), children: _jsxs("span", { className: labelClasses, children: [required && _jsx("span", { className: "required-asterisk", children: "*" }), label] }) })), _jsx("textarea", { name: name, value: value, id: name, className: textareaClasses, placeholder: placeholder, disabled: disabled, onChange: handleChange, onBlur: onBlur, onFocus: onFocus, onKeyDown: handleKeyDown, onPaste: handlePaste, required: required, rows: rows, cols: cols, readOnly: readOnly, ...props }), _jsxs("div", { className: isError ? 'ff-textarea-error-alignment' : 'msg-character-count', children: [errorMessage, capacity > 0 && !readOnly && displayCapacity && (_jsxs("div", { className: 'ff-textarea-character-count', children: [_jsxs(Typography, { fontSize: 8, children: [getCharacterCount(value), "/"] }), _jsx(Typography, { fontSize: 8, children: capacity })] }))] })] }));
};
export default Textarea;
//# sourceMappingURL=Textarea.js.map