import { inject, provide, proxyRefs, type InjectionKey } from 'vue'
import type { useFilesPage } from './useFilesPage'

export type FilesPageContext = ReturnType<typeof useFilesPage>

/** proxyRefs unwraps Ref fields on property access (required for child v-model). */
export type FilesPageContextProvided = ReturnType<typeof proxyRefs<FilesPageContext>>

export const filesPageKey: InjectionKey<FilesPageContextProvided> = Symbol('filesPage')

export function provideFilesPage(ctx: FilesPageContext): void {
  provide(filesPageKey, proxyRefs(ctx))
}

export function useFilesPageContext(): FilesPageContextProvided {
  const ctx = inject(filesPageKey)
  if (!ctx) {
    throw new Error('useFilesPageContext() must be used inside FilesPage')
  }
  return ctx
}
