import type { App } from 'vue'
import { inject } from 'vue'
import type { UiAdapter } from './contracts'
import { assertUiAdapterContract, uiAdapterKey } from './contracts'

export function installUiAdapter(app: App, adapter: UiAdapter): void {
  assertUiAdapterContract(adapter)
  app.provide(uiAdapterKey, adapter)
}

export function useUiAdapter(): UiAdapter {
  const adapter = inject(uiAdapterKey)
  if (!adapter) {
    throw new Error('UI adapter is not installed. Install a validated provider during app bootstrap.')
  }
  return adapter
}
