import {computed, type ComputedRef, toValue, type WritableComputedRef} from 'vue'; import {isOfType} from '@myparcel/ts-utils'; import {createFormElement, createObjectWithKeys} from '../../utils'; import { type ArrayItem, type ElementInstance, type RadioGroupEmits, type RadioGroupModelValue, type RadioGroupProps, type SelectOption, type SelectOptionWithLabel, } from '../../types'; import {useInputWithOptionsContext} from './useInputWithOptionsContext'; interface RadioGroupContext { id: string; model: WritableComputedRef>; options: ComputedRef[]>; elements: ComputedRef, ElementInstance>>; } export const useRadioGroupContext = < T extends RadioGroupModelValue = RadioGroupModelValue, Props extends RadioGroupProps = RadioGroupProps, >( props: Props, emit: RadioGroupEmits, ): RadioGroupContext => { const context = useInputWithOptionsContext(props, emit); const elements = computed(() => { const optionValues = (toValue(context.options) ?? []).map((option) => option.value); return createObjectWithKeys(optionValues, (value) => { const option = (toValue(context.options) ?? []).find((option) => option.value === value) as SelectOption; return createFormElement({ ref: context.model, name: `${props.element.name}${value.toString()}`, // @ts-expect-error this works but the form builder does not technically allow it label: isOfType>(option, 'label') ? option.label : option.plainLabel, props: { value: option.value, image: option.image, icon: option.icon, }, }); }); }); return { ...context, // @ts-expect-error todo elements, }; };