"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importStar(require("react"));
const acui_1 = require("@Acme Corporation/acui");
const Select = (0, react_1.forwardRef)(({ name, options, label, helperText, error: externalError, isRequired = false, isDisabled = false, isReadOnly = false, isInvalid: externalInvalid, isMulti = false, size = 'md', placeholder, leftElement, rightElement, className = '', wrapperClassName = '', labelClassName = '', selectClassName = '', errorClassName = '', helperTextClassName = '', onValueChange, validateOnBlur = true, validateOnChange = false, onChange, onBlur, groupBy, ...props }, ref) => {
    const theme = (0, acui_1.useTheme)();
    const formContext = react_1.default.useContext(FormContext);
    const [isFocused, setIsFocused] = react_1.default.useState(false);
    const [localValue, setLocalValue] = react_1.default.useState(isMulti ? [] : props.value || props.defaultValue || '');
    const { errors = {}, touched = {}, setFieldValue, setFieldTouched, setFieldError } = formContext || {};
    const error = externalError || errors[name];
    const isInvalid = externalInvalid || (touched[name] && !!error);
    const isControlled = props.value !== undefined;
    const handleChange = (0, react_1.useCallback)((e) => {
        const newValue = isMulti
            ? Array.from(e.target.selectedOptions, option => option.value)
            : e.target.value;
        if (!isControlled) {
            setLocalValue(newValue);
        }
        onChange?.(e);
        onValueChange?.(newValue);
        if (formContext && validateOnChange) {
            setFieldValue(name, newValue);
        }
    }, [isControlled, isMulti, onChange, onValueChange, formContext, name, setFieldValue, validateOnChange]);
    const handleBlur = (0, react_1.useCallback)((e) => {
        setIsFocused(false);
        onBlur?.(e);
        if (formContext && validateOnBlur) {
            setFieldTouched(name, true);
        }
    }, [onBlur, formContext, name, setFieldTouched, validateOnBlur]);
    const handleFocus = (0, react_1.useCallback)(() => {
        setIsFocused(true);
    }, []);
    const selectId = react_1.default.useId();
    const errorId = react_1.default.useId();
    const helperTextId = react_1.default.useId();
    const groupedOptions = react_1.default.useMemo(() => {
        if (!groupBy)
            return options;
        return options.reduce((acc, option) => {
            const group = option.group || 'Other';
            if (!acc[group])
                acc[group] = [];
            acc[group].push(option);
            return acc;
        }, {});
    }, [options, groupBy]);
    const labelId = react_1.default.useId();
    const errorId = react_1.default.useId();
    const helperTextId = react_1.default.useId();
    // Fix ARIA attribute validation by using proper type-safe values
    const ariaProps = {
        'aria-invalid': isInvalid ? true : undefined,
        'aria-required': isRequired ? true : undefined,
        'aria-labelledby': label ? labelId : undefined,
        'aria-describedby': [
            error ? errorId : null,
            helperText ? helperTextId : null
        ].filter(Boolean).join(' ') || undefined
    };
    return ((0, jsx_runtime_1.jsxs)("div", { className: `bf-select-wrapper ${wrapperClassName}`, "data-size": size, "data-invalid": isInvalid, "data-disabled": isDisabled, "data-readonly": isReadOnly, children: [label && ((0, jsx_runtime_1.jsxs)("label", { htmlFor: selectId, className: `bf-select-label ${labelClassName}`, children: [label, isRequired && (0, jsx_runtime_1.jsx)("span", { className: "bf-required-mark", "aria-hidden": "true", children: "*" })] })), (0, jsx_runtime_1.jsxs)("div", { className: `bf-select-container ${className} ${size} ${isFocused ? 'focused' : ''} ${isInvalid ? 'invalid' : ''} ${isDisabled ? 'disabled' : ''} ${isReadOnly ? 'readonly' : ''}`, children: [leftElement && ((0, jsx_runtime_1.jsx)("div", { className: "bf-select-left-element", "aria-hidden": "true", children: leftElement })), (0, jsx_runtime_1.jsxs)("select", { ref: ref, id: selectId, name: name, className: `bf-select ${selectClassName}`, value: isControlled ? props.value : localValue, onChange: handleChange, onBlur: handleBlur, onFocus: handleFocus, disabled: isDisabled, readOnly: isReadOnly, required: isRequired, multiple: isMulti, ...ariaProps, ...props, children: [placeholder && ((0, jsx_runtime_1.jsx)("option", { value: "", disabled: true, children: placeholder })), groupBy ? (Object.entries(groupedOptions).map(([group, groupOptions]) => ((0, jsx_runtime_1.jsx)("optgroup", { label: group, children: groupOptions.map(option => ((0, jsx_runtime_1.jsx)("option", { value: option.value, disabled: option.disabled, children: option.label }, option.value))) }, group)))) : (options.map(option => ((0, jsx_runtime_1.jsx)("option", { value: option.value, disabled: option.disabled, children: option.label }, option.value))))] }), rightElement && ((0, jsx_runtime_1.jsx)("div", { className: "bf-select-right-element", "aria-hidden": "true", children: rightElement }))] }), error && ((0, jsx_runtime_1.jsx)("div", { id: errorId, className: `bf-select-error ${errorClassName}`, role: "alert", "aria-live": "assertive", children: error })), helperText && !error && ((0, jsx_runtime_1.jsx)("div", { id: helperTextId, className: `bf-select-helper-text ${helperTextClassName}`, role: "status", children: helperText }))] }));
});
Select.displayName = 'Select';
exports.default = (0, react_1.memo)(Select);
//# sourceMappingURL=Select.js.map