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

export function castNullableStringValue(props) {
    return castNullableField(props, ['value', 'defaultValue'], 'string', (v) => String(v));
}
export function castNullableNumberValue(props) {
    return castNullableField(props, ['value', 'defaultValue'], 'number', (v) => Number(v));
}
export function castNullableArrayValue(props) {
    return castNullableField(props, ['value', 'defaultValue'], 'object', (v) => Array.isArray(v) ? v : []);
}
export function castNullableIsSelected(props) {
    return castNullableField(props, ['isSelected', 'defaultSelected'], 'boolean', (v) => !!v);
}
export function castNullableSelectedKey(props) {
    return castNullableField(props, ['selectedKey', 'defaultSelectedKey']);
}
export function castNullableField(props, keys, type, cast) {
    props = { ...props };
    keys.forEach((key) => {
        if (props[key] === null) {
            props[key] = undefined;
        }
        if (type && cast && props[key] != null && typeof props[key] != type) {
            props[key] = cast(props[key]);
            console.warn('Wrong value type in', props, 'in keys', JSON.stringify(keys));
        }
    });
    return props;
}

        