1
  5   ˆl–ßi~”Ji?u8 Cq¦®4ê˜´o�þB gd‘•Ò•ÿçÂƒh¹ÁÀ¿      Normal
  L   ˆl–Ü4ý('ÜZÔâ€vàZ¸ÓJbÑ¿�þYŽ1,ãdnGÜmÇ�,ÏÃ„bx_Â_“I|¥ŽõHºl=$¤�zTÖ™	ÂÁ ;²    // Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

import { cloneDeep, keyBy } from 'lodash-es'
import { computed, ref, type Ref, watch } from 'vue'

import type { SelectOption, SelectValue } from '#shared/components/CommonSelect/types.ts'
import useValue from '#shared/components/Form/composables/useValue.ts'
import type { AutoCompleteOption } from '#shared/components/Form/fields/FieldAutocomplete/types'
import type { SelectOptionSorting } from '#shared/components/Form/fields/FieldSelect/types.ts'
import type {
  FlatSelectOption,
  TreeSelectOption,
} from '#shared/components/Form/fields/FieldTreeSelect/types.ts'
import type { FormFieldContext } from '#shared/components/Form/types/field.ts'
import { i18n } from '#shared/i18n.ts'

type AllowedSelectValue = SelectValue | Record<string, unknown>

const useSelectOptions = <T extends SelectOption[] | FlatSelectOption[] | AutoCompleteOption[]>(
  options: Ref<T>,
  context: Ref<
    FormFieldContext<{
      historicalOptions?: Record<string, string>
      multiple?: boolean
      noOptionsLabelTranslation?: boolean
      rejectNonExistentValues?: boolean
      sorting?: SelectOptionSorting
      complexValue?: boolean
    }>
  >,
  appendedTreeOptions?: Ref<TreeSelectOption[]>,
) => {
  const dialog = ref<HTMLElement>()

  const { currentValue, hasValue, valueContainer, clearValue } = useValue(context)

  const appendedOptions = ref<T>([] as unknown as T)

  const availableOptions = computed(() => [...(options.value || []), ...appendedOptions.value])

  const hasStatusProperty = computed(() =>
    availableOptions.value?.some((option) => (option as SelectOption | FlatSelectOption).status),
  )

  const translatedOptions = computed(() => {
    if (!availableOptions.value) return []

    const { noOptionsLabelTranslation } = context.value

    return availableOptions.value.map((option) => {
      const label =
        noOptionsLabelTranslation && !option.labelPlaceholder
          ? option.label || ''
          : i18n.t(option.label, ...(option.labelPlaceholder || []))

      const variant = option as AutoCompleteOption
      const heading =
        noOptionsLabelTranslation && !variant.headingPlaceholder
          ? variant.heading || ''
          : i18n.t(variant.heading, ...(variant.headingPlaceholder || []))

      return Object.assign(option, {
        label,
        heading,
      })
    })
  })

  const optionValueLookup = computed(() => keyBy(translatedOptions.value, 'value'))

  const sortedOptions = computed(() => {
    const { sorting } = context.value

    if (!sorting) return translatedOptions.value

    if (sorting !== 'label' && sorting !== 'value') {
      console.warn(`Unsupported sorting option "${sorting}"`)
      return translatedOptions.value
    }

    return [...translatedOptions.value]?.sort((a, b) => {
      const aLabelOrValue = a[sorting] || a.value
      const bLabelOrValue = b[sorting] || a.value
      return String(aLabelOrValue).localeCompare(String(bLabelOrValue))
    })
  })

  const getSelectedOption = (selectedValue: AllowedSelectValue): T[number] => {
    if (typeof selectedValue === 'object' && selectedValue !== null)
      return selectedValue as unknown as T[number]
    const key = selectedValue.toString()
    return optionValueLookup.value[key]
  }

  const getSelectedOptionIcon = (selectedValue: AllowedSelectValue) => {
    const option = getSelectedOption(selectedValue)
    return option?.icon as string
  }

  const getSelectedOptionValue = (selectedValue: AllowedSelectValue) => {
    if (typeof selectedValue !== 'object') return selectedValue
    const option = getSelectedOption(selectedValue)
    return option?.value
  }

  const getSelectedOptionLabel = (selectedValue: AllowedSelectValue) => {
    const option = getSelectedOption(selectedValue)
    return option?.label
  }

  const getSelectedOptionStatus = (selectedValue: AllowedSelect