'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import React from 'react';
function groupSimilarErrors(items) {
    const result = [];
    for (const item of items){
        if (item) {
            const parts = item.split(' → ');
            let inserted = false;
            // Find a place where a similar path exists
            for(let i = 0; i < result.length; i++){
                if (result[i].startsWith(parts[0])) {
                    result.splice(i + 1, 0, item);
                    inserted = true;
                    break;
                }
            }
            // If no similar path was found, add to the end
            if (!inserted) {
                result.push(item);
            }
        }
    }
    return result;
}
export function createErrorsFromMessage(message) {
    const colonIndex = message.indexOf(':');
    const intro = colonIndex >= 0 ? message.slice(0, colonIndex) : message;
    const errorsString = colonIndex >= 0 ? message.slice(colonIndex + 1) : undefined;
    if (!errorsString) {
        return {
            message: intro
        };
    }
    const errors = errorsString.split(',').map((error)=>error.replaceAll(' > ', ' → ').trim());
    if (errors.length === 1) {
        return {
            errors,
            message: `${intro}: `
        };
    }
    return {
        errors: groupSimilarErrors(errors),
        message: `${intro} (${errors.length}):`
    };
}
export function FieldErrorsToast({ errorMessage }) {
    const [{ errors, message }] = React.useState(()=>createErrorsFromMessage(errorMessage));
    return /*#__PURE__*/ _jsxDEV("div", {
        children: [
            message,
            Array.isArray(errors) && errors.length > 0 ? errors.length === 1 ? /*#__PURE__*/ _jsxDEV("span", {
                "data-testid": "field-error",
                children: errors[0]
            }, void 0, false, {
                fileName: "src/elements/Toasts/fieldErrors.tsx",
                lineNumber: 69,
                columnNumber: 11
            }, this) : /*#__PURE__*/ _jsxDEV("ul", {
                "data-testid": "field-errors",
                children: errors.map((error, index)=>{
                    return /*#__PURE__*/ _jsxDEV("li", {
                        children: error
                    }, index, false, {
                        fileName: "src/elements/Toasts/fieldErrors.tsx",
                        lineNumber: 73,
                        columnNumber: 22
                    }, this);
                })
            }, void 0, false, {
                fileName: "src/elements/Toasts/fieldErrors.tsx",
                lineNumber: 71,
                columnNumber: 11
            }, this) : null
        ]
    }, void 0, true, {
        fileName: "src/elements/Toasts/fieldErrors.tsx",
        lineNumber: 65,
        columnNumber: 5
    }, this);
}

//# sourceMappingURL=fieldErrors.js.map