import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { forwardRef } from 'react';
import { Box } from '../box/Box';
import { Input } from '../inputv2/inputv2';
import { AddButton, SubButton } from './InputButtons';
function InternalInputList({ onChange, onBlur, min, max, maxLength, minLength, pattern, required, disabled, maxItems, value, name, ...rest }, _) {
    const isMaxItemsReached = maxItems !== undefined && maxItems !== null && value.length === maxItems;
    const insertEntry = () => {
        if (!isMaxItemsReached) {
            onChange === null || onChange === void 0 ? void 0 : onChange({
                target: { value: [...(value !== null && value !== void 0 ? value : []), ''] },
            });
        }
    };
    const deleteEntry = (entryIndex) => {
        const newValues = value.filter((_, index) => index !== entryIndex);
        const updatedValues = newValues.length === 0 ? [''] : newValues;
        onChange === null || onChange === void 0 ? void 0 : onChange({
            target: { value: updatedValues },
        });
    };
    return (_jsx(_Fragment, { children: value.map((val, index) => (_jsxs(Box, { display: "flex", gap: "0.25rem", alignItems: "center", children: [_jsx(Input, { id: `${name}[${index}]`, "aria-label": `${name}${index}`, size: "1/2", value: val, onChange: (evt) => {
                        const tempValues = [...value];
                        tempValues[index] = evt.target.value;
                        onChange === null || onChange === void 0 ? void 0 : onChange({
                            target: { value: tempValues },
                        });
                    }, ...rest }), _jsx(SubButton, { index: index, deleteEntry: deleteEntry, items: value, disabled: value.length === 1 && value[0] === '' }, `${name}-delete-${value.join(',') + index}`), _jsx(AddButton, { index: index, insertEntry: insertEntry, items: value, disabled: val === '' || isMaxItemsReached }, `${name}-add-${value.join(',') + index}`)] }, index))) }));
}
export const InputList = forwardRef(InternalInputList);
