import type { TInputPlugin } from '@simon_he/vue-tui'
import type { Ref } from 'vue'
import {
  createTInputHostPlugin,
  TInputPluginsContextKey,
} from '@simon_he/vue-tui'
import { computed, inject, provide } from 'vue'

export function provideToastInputHostPlugin(
  showToast: (text: string) => void,
): void {
  const parentPlugins = inject(TInputPluginsContextKey, null) as Readonly<
    Ref<readonly TInputPlugin[]>
  > | null
  const hostPlugin = createTInputHostPlugin({ showToast })
  const mergedPlugins = computed<readonly TInputPlugin[]>(() => [
    ...((parentPlugins?.value ?? []) as readonly TInputPlugin[]),
    hostPlugin,
  ])
  provide(TInputPluginsContextKey, mergedPlugins as any)
}
