'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _tslib = require('../../../_virtual/_tslib.js');
var React = require('react');
var AlertCircle = require('../../../icons/AlertCircle.js');
var index = require('../../../utils/index.js');
var styles = require('../styles.js');
var utils = require('../utils.js');
var styles$1 = require('./styles.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

function TextFieldComponent(_a, ref) {
    var { autoComplete = false, id, name, label, value, type = 'text', placeholder, hasError = false, errorText, assistText, optionalText = 'Optional', disabled = false, variant = 'medium', required = true, maxChar, minChar, showCharCount = false, width, prefix, suffix, onSuffixClick, onChange, touched, onBlur, onFocus, onPaste, onKeyUp } = _a, props = _tslib.__rest(_a, ["autoComplete", "id", "name", "label", "value", "type", "placeholder", "hasError", "errorText", "assistText", "optionalText", "disabled", "variant", "required", "maxChar", "minChar", "showCharCount", "width", "prefix", "suffix", "onSuffixClick", "onChange", "touched", "onBlur", "onFocus", "onPaste", "onKeyUp"]);
    const [charCount, setCharCount] = React.useState(0);
    const [showError, setShowError] = React.useState(false);
    React.useEffect(() => {
        if (value && typeof value === 'string') {
            setCharCount(value.length);
        }
    }, []);
    React.useEffect(() => {
        const composeError = typeof touched === 'undefined' ? hasError : hasError && touched;
        setShowError(composeError);
    }, [hasError, touched]);
    function handleChange(event) {
        setCharCount(event.target.value.length);
        onChange(event);
    }
    const appendTestId = index.getId(props['data-testid']);
    return (React__default['default'].createElement(styles.Wrapper, { "data-testid": appendTestId('text-field'), width: width },
        React__default['default'].createElement(styles.LabelWrapper, { disabled: disabled, hasLabel: Boolean(label === null || label === void 0 ? void 0 : label.length) },
            label && (React__default['default'].createElement(styles.Label, { htmlFor: id || `input-${name}`, disabled: disabled }, label)),
            !required && !showCharCount && React__default['default'].createElement(styles.Text, null, optionalText),
            showCharCount && (React__default['default'].createElement(styles.Text, { "data-testid": appendTestId('char-count') }, utils.getCharacterCount(charCount, maxChar)))),
        React__default['default'].createElement(styles.Border, { hasError: showError, disabled: disabled, hasPrefix: Boolean(prefix), hasSuffix: Boolean(suffix) },
            Boolean(prefix) && (React__default['default'].createElement(styles$1.Prefix, { "data-testid": appendTestId('prefix') }, prefix)),
            React__default['default'].createElement(styles$1.TextFieldInput, { hasError: showError, ref: ref, id: id || `input-${name}`, type: type, variant: variant, disabled: disabled, placeholder: placeholder, autoComplete: autoComplete ? 'on' : 'off', maxLength: maxChar, minLength: minChar, value: String(value), onChange: handleChange, onKeyUp: onKeyUp, onBlur: onBlur, onFocus: onFocus, onPaste: onPaste, "data-trackid": props['data-trackid'], "data-testid": appendTestId(), "aria-label": props['aria-label'] || label, name: name }),
            Boolean(suffix) && (React__default['default'].createElement(styles$1.Sufix, { "data-testid": appendTestId('suffix'), hasHandler: Boolean(onSuffixClick), onClick: onSuffixClick && onSuffixClick }, suffix))),
        React__default['default'].createElement(styles.SupportingText, null,
            Boolean(errorText) && showError && !disabled && (React__default['default'].createElement(styles.ErrorMessage, null,
                React__default['default'].createElement(AlertCircle['default'], { width: 20, height: 20 }),
                errorText)),
            !showError &&
                (typeof assistText === 'string' ? (React__default['default'].createElement(styles.AssistMessage, null, assistText)) : (assistText)))));
}
const TextField = React.forwardRef(TextFieldComponent);

exports.TextField = TextField;
