import { inject, provide, computed, type InjectionKey, type Ref } from 'vue'

export type KsFieldLayoutMode = 'filter' | 'form' | 'inline' | 'stacked'

export const KS_FIELD_LAYOUT_KEY: InjectionKey<Ref<KsFieldLayoutMode>> = Symbol('KS_FIELD_LAYOUT_KEY')

export function provideFieldLayout(mode: Ref<KsFieldLayoutMode> | KsFieldLayoutMode): void {
  const modeRef = computed(() => (typeof mode === 'string' ? mode : mode.value))
  provide(KS_FIELD_LAYOUT_KEY, modeRef)
}

export function useFieldLayout(explicitInline?: boolean): { isInline: Ref<boolean> } {
  const parentLayout = inject(KS_FIELD_LAYOUT_KEY, null)
  const isInline = computed(() => {
    if (explicitInline !== undefined) return explicitInline
    if (parentLayout) return parentLayout.value === 'filter' || parentLayout.value === 'inline'
    return false
  })
  return { isInline }
}
