// Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

import { ref, type Ref } from 'vue'

import { emailFilterValueValidator } from '#shared/components/Form/fields/FieldRecipient/features/filterValueValidators.ts'
import stopEvent from '#shared/utils/events.ts'

import type { DropdownOptionsAction } from '#desktop/components/CommonSelect/types.ts'
import type {
  AutoCompleteOptionValueDictionary,
  ClearFilterInputFunction,
  SelectOptionFunction,
} from '#desktop/components/Form/fields/FieldAutoComplete/types.ts'

export const useAddUnknownValueAction = (
  label?: Ref<string> | ((filter: string) => string),
  filterValueValidator?: (filter: string) => boolean | Promise<boolean>,
) => {
  const actions = ref<DropdownOptionsAction[]>([])
  const actionLabel = label ?? ref(__('add new email address'))

  // A field taking values of more than one kind names the kind the typed value is.
  const labelFor = (filter: string) =>
    typeof actionLabel === 'function' ? actionLabel(filter) : actionLabel.value

  const isValidFilterValue = filterValueValidator ?? emailFilterValueValidator

  const isNewOption = (filter: string, optionValues: AutoCompleteOptionValueDictionary) =>
    !optionValues[filter]

  const addUnknownValue = (
    filter: string,
    selectOption: SelectOptionFunction,
    clearFilter: ClearFilterInputFunction,
    focus: boolean,
  ) => {
    const newOption = {
      value: filter,
      label: filter,
    }

    selectOption(newOption, focus)
    clearFilter()
  }

  const onSearchInteractionUpdate = (
    filter: string,
    optionValues: AutoCompleteOptionValueDictionary,
    selectOption: SelectOptionFunction,
    clearFilter: ClearFilterInputFunction,
  ) => {
    if (!isNewOption(filter, optionValues) || !isValidFilterValue(filter)) {
      actions.value = []
      return
    }

    actions.value = [
      {
        key: 'addUnknownValue',
        label: labelFor(filter),
        icon: 'plus-square-fill',
        onClick: (focus) => {
          addUnknownValue(filter, selectOption, clearFilter, focus)

          // Reset actions after current filter was added.
          actions.value = []
        },
      },
    ]
  }

  const onKeydownFilterInput = (
    event: KeyboardEvent,
    filter: string,
    optionValues: AutoCompleteOptionValueDictionary,
    selectOption: SelectOptionFunction,
    clearFilter: ClearFilterInputFunction,
  ) => {
    const { key } = event

    if (!filter) return

    if (['Enter', 'Tab'].includes(key)) {
      stopEvent(event)

      if (!isNewOption(filter, optionValues) || !isValidFilterValue(filter)) return

      addUnknownValue(filter, selectOption, clearFilter, true)
    }
  }

  return {
    actions,
    isNewOption,
    isValidFilterValue,
    addUnknownValue,
    onSearchInteractionUpdate,
    onKeydownFilterInput,
  }
}
