// Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ import { storeToRefs } from 'pinia' import { computed, ref, shallowRef } from 'vue' import type { ChangedFieldFunction, FormFieldValue, } from '#shared/components/Form/types.ts' import { useApplicationStore } from '#shared/stores/application.ts' import type { TicketRelationAndRecentListItem } from '#desktop/pages/ticket/components/TicketDetailView/TicketSimpleTable/types.ts' import { getTicketNumberWithHook } from '#desktop/pages/ticket/composables/getTicketNumber.ts' export const useTargetTicketOptions = ( onChangedField: ChangedFieldFunction, updateFieldValues: (values: Record) => void, ) => { const { config } = storeToRefs(useApplicationStore()) const targetTicketId = ref() const formListTargetTicket = shallowRef() const formListTargetTicketOptions = computed(() => { if (!formListTargetTicket.value) return return [ { value: formListTargetTicket.value.id, label: `${getTicketNumberWithHook(config.value.ticket_hook, formListTargetTicket.value.number)} - ${formListTargetTicket.value.title}`, heading: formListTargetTicket.value.customer.fullname, ticket: formListTargetTicket.value, }, ] }) onChangedField('targetTicketId', (value) => { targetTicketId.value = (value as string) ?? undefined if (formListTargetTicket.value?.id === value) return formListTargetTicket.value = undefined }) const handleTicketClick = (ticket: TicketRelationAndRecentListItem) => { updateFieldValues({ targetTicketId: ticket.id, }) formListTargetTicket.value = ticket } return { formListTargetTicketOptions, targetTicketId, handleTicketClick, } }