'use client';
import React, { useCallback, useEffect, useState } from 'react';
import { useConfig } from '../../../providers/Config/index.js';
import { useLocale } from '../../../providers/Locale/index.js';
import LabelComp from '../../Label/index.js';
import useField from '../../useField/index.js';
import { withCondition } from '../../withCondition/index.js';
import { isFieldRTL } from '../shared.js';
import { TextInput } from './Input.js';
const Text = (props)=>{
    const { name, AfterInput, BeforeInput, Description, Error, Label: LabelFromProps, className, hasMany, inputRef, label, localized, maxLength, maxRows, minLength, minRows, path: pathFromProps, placeholder, required, rtl, style, validate, width } = props;
    const Label = LabelFromProps || /*#__PURE__*/ React.createElement(LabelComp, {
        label: label,
        required: required
    });
    const locale = useLocale();
    const { localization: localizationConfig } = useConfig();
    const memoizedValidate = useCallback((value, options)=>{
        if (typeof validate === 'function') return validate(value, {
            ...options,
            maxLength,
            minLength,
            required
        });
    }, [
        validate,
        minLength,
        maxLength,
        required
    ]);
    const { path, readOnly, setValue, showError, value } = useField({
        path: pathFromProps || name,
        validate: memoizedValidate
    });
    const renderRTL = isFieldRTL({
        fieldLocalized: localized,
        fieldRTL: rtl,
        locale,
        localizationConfig: localizationConfig || undefined
    });
    const [valueToRender, setValueToRender] = useState([]) // Only for hasMany
    ;
    const handleHasManyChange = useCallback((selectedOption)=>{
        if (!readOnly) {
            let newValue;
            if (!selectedOption) {
                newValue = [];
            } else if (Array.isArray(selectedOption)) {
                newValue = selectedOption.map((option)=>option.value?.value || option.value);
            } else {
                newValue = [
                    selectedOption.value?.value || selectedOption.value
                ];
            }
            setValue(newValue);
        }
    }, [
        readOnly,
        setValue
    ]);
    // useEffect update valueToRender:
    useEffect(()=>{
        if (hasMany && Array.isArray(value)) {
            setValueToRender(value.map((val, index)=>{
                return {
                    id: `${val}${index}`,
                    label: `${val}`,
                    value: {
                        // React-select automatically uses "label-value" as a key, so we will get that react duplicate key warning if we just pass in the value as multiple values can be the same. So we need to append the index to the toString() of the value to avoid that warning, as it uses that as the key.
                        toString: ()=>`${val}${index}`,
                        value: val?.value || val
                    }
                };
            }));
        }
    }, [
        value,
        hasMany
    ]);
    return /*#__PURE__*/ React.createElement(TextInput, {
        AfterInput: AfterInput,
        BeforeInput: BeforeInput,
        Description: Description,
        Error: Error,
        Label: Label,
        className: className,
        hasMany: hasMany,
        inputRef: inputRef,
        maxRows: maxRows,
        minRows: minRows,
        onChange: hasMany ? handleHasManyChange : (e)=>{
            setValue(e.target.value);
        },
        path: path,
        placeholder: placeholder,
        readOnly: readOnly,
        required: required,
        rtl: renderRTL,
        showError: showError,
        style: style,
        value: value || '',
        valueToRender: valueToRender,
        width: width
    });
};
export default withCondition(Text);

//# sourceMappingURL=index.js.map