/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.0.0-canary-b46ab85
 * Released under the MIT license.
 */

import { jsx as _jsx } from "react/jsx-runtime";
import { Children, cloneElement, createContext, useContext, useMemo, } from 'react';
import { mergeProps } from '../../../utils/react/index.js';
import { warn } from '../../../utils/warnings.js';
import { FieldWrapper } from '../FieldWrapper/index.js';
import { useFormProps } from './Form.js';
import { useField } from './use-field/index.js';
function getDefaultValidateTrigger(type) {
    type = type || '';
    return type === 'Number' || type.includes('Text') ? 'onBlur' : 'onChange';
}
function getValueProps(type, value, onChange, allowsCustomValue) {
    type = type || '';
    if (type === 'Number') {
        return {
            value: value != null ? value : null,
            onChange: onChange,
        };
    }
    else if (type === 'Text') {
        return {
            value: typeof value === 'string' || typeof value === 'number'
                ? String(value)
                : '',
            onChange: onChange,
        };
    }
    else if (type === 'Checkbox') {
        return {
            isSelected: value != null ? value : false,
            isIndeterminate: false,
            onChange: onChange,
        };
    }
    else if (type === 'CheckboxGroup') {
        return {
            value: value != null ? value : [],
            onChange: onChange,
        };
    }
    else if (type === 'RangeSlider') {
        return {
            value: value != null ? value : undefined,
            onChange: onChange,
        };
    }
    else if (type === 'ComboBox') {
        return {
            selectedKey: !allowsCustomValue ? value ?? null : undefined,
            inputValue: allowsCustomValue ? value ?? '' : undefined,
            onInputChange(val) {
                if (!allowsCustomValue) {
                    return;
                }
                onChange(val);
            },
            onSelectionChange(val) {
                if (val == null && allowsCustomValue) {
                    return;
                }
                onChange(val);
            },
        };
    }
    else if (type === 'Select') {
        return {
            selectedKey: value ?? null,
            onSelectionChange: onChange,
        };
    }
    return {
        value: value != null ? value : null,
        onChange,
    };
}
export function Field(props) {
    const allProps = useFormProps(props);
    let { children, form, label, extra, necessityLabel, necessityIndicator, tooltip, isHidden, isDisabled, isLoading, styles, labelProps, labelPosition = 'top', labelStyles, labelSuffix, } = allProps;
    if (typeof children === 'function') {
        children = children(form);
    }
    let child = children == null ? null : Children.only(children);
    const inputType = child?.type?.cubeInputType ?? 'Text';
    const __props = useField(allProps, {
        defaultValidationTrigger: getDefaultValidateTrigger(inputType),
    });
    const { validationState, message, description, errorMessage, isRequired, name, id, } = __props;
    if (!child)
        return null;
    if (id) {
        if (!labelProps) {
            labelProps = {};
        }
        labelProps.for = id;
    }
    if (__props.nonInput) {
        return (_jsx(LegacyFieldProvider, { children: _jsx(FieldWrapper, { isHidden: isHidden, isDisabled: isDisabled, validationState: __props.validationState, necessityIndicator: necessityIndicator, necessityLabel: necessityLabel, isRequired: isRequired, label: label, labelProps: labelProps, extra: extra, tooltip: tooltip, message: message, description: description, errorMessage: errorMessage, Component: child, styles: styles, labelPosition: labelPosition, labelStyles: labelStyles, labelSuffix: labelSuffix }) }));
    }
    if (!form) {
        warn('Form Field requires declared form instance if field name is specified');
        return null;
    }
    if (!__props.field) {
        return (_jsx(LegacyFieldProvider, { children: cloneElement(child, mergeProps(child.props, {
                ...getValueProps(inputType),
                label: name,
                name,
                id,
            })) }));
    }
    const newProps = {
        id,
        name,
        onBlur: __props.onBlur,
    };
    if (necessityIndicator != null) {
        newProps.necessityIndicator = necessityIndicator;
    }
    if (necessityLabel) {
        newProps.necessityLabel = necessityLabel;
    }
    if (validationState) {
        newProps.validationState = validationState;
    }
    if (isRequired) {
        newProps.isRequired = isRequired;
    }
    if (label) {
        newProps.label = label;
    }
    if (extra) {
        newProps.extra = extra;
    }
    if (tooltip) {
        newProps.tooltip = tooltip;
    }
    if (message !== undefined) {
        newProps.message = message;
    }
    if (errorMessage !== undefined) {
        newProps.errorMessage = errorMessage;
    }
    if (description !== undefined) {
        newProps.description = description;
    }
    if (isHidden != null) {
        newProps.isHidden = isHidden;
    }
    if (isDisabled != null) {
        newProps.isDisabled = isDisabled;
    }
    if (isLoading != null) {
        newProps.isLoading = isLoading;
    }
    Object.assign(newProps, getValueProps(inputType, __props.field?.value, __props.onChange, child.props.allowsCustomValue));
    const { onChange, onSelectionChange, ...childProps } = child.props;
    // onChange event passed to the child should be executed after Form onChange logic
    return (_jsx(LegacyFieldProvider, { children: cloneElement(child, mergeProps(childProps, newProps, onChange ? { onChange: __props.onChange } : {}, onSelectionChange ? { onSelectionChange: __props.onChange } : {})) }));
}
const LegacyFieldContext = createContext({
    insideLegacyField: false,
});
function LegacyFieldProvider(props) {
    const value = useMemo(() => ({ insideLegacyField: true }), []);
    return (_jsx(LegacyFieldContext.Provider, { value: value, children: props.children }));
}
export function useInsideLegacyField() {
    const { insideLegacyField = false } = useContext(LegacyFieldContext);
    return insideLegacyField;
}

        