import { computed, toValue } from 'vue'; import type { MaybeRefOrGetter } from 'vue'; import type { TicketLinkTypeModel } from '@/types/models/ticket-link-type-model'; import type { TicketLinkActionOption, TicketLinkFormData, } from '@/types/ticket-detail'; export function useTicketLinkActions( ticketLinkTypes: MaybeRefOrGetter, form: TicketLinkFormData, ) { const actions = computed(() => toValue(ticketLinkTypes).flatMap((type) => { const choices: TicketLinkActionOption[] = [ { value: `${type.id}:source`, label: type.label, ticketLinkTypeId: type.id, direction: 'source', }, ]; if ( type.counterpart_label && type.counterpart_label !== type.label ) { choices.push({ value: `${type.id}:counterpart`, label: type.counterpart_label, ticketLinkTypeId: type.id, direction: 'counterpart', }); } return choices; }), ); const selectedAction = computed({ get: () => `${form.ticket_link_type_id}:${form.link_direction}`, set: (value: string | number | null) => { const action = actions.value.find( (option) => option.value === value, ); if (action) { form.ticket_link_type_id = action.ticketLinkTypeId; form.link_direction = action.direction; } }, }); const selectedActionLabel = computed( () => actions.value.find( (action) => action.value === selectedAction.value, )?.label, ); return { actions, selectedAction, selectedActionLabel }; }