import { readonly as defineReadonly } from 'vue' import type { InjectionKey, UnwrapRef } from 'vue' export interface CreateContextOptions { readonly?: boolean createProvider?: boolean native?: boolean } type ShallowUnwrap = { [P in keyof T]: UnwrapRef } export function createContext( context: any, key: InjectionKey = Symbol('context'), options: CreateContextOptions = {}, ) { const { readonly = true, createProvider = false, native = false } = options const state = reactive(context) const provideData = readonly ? defineReadonly(state) : state !createProvider && provide(key, native ? context : provideData) return { state } } export function useContext(key: InjectionKey, native?: boolean): T export function useContext( key: InjectionKey, defaultValue?: any, native?: boolean ): T export function useContext( key: InjectionKey = Symbol('context'), defaultValue?: any, ): ShallowUnwrap { return inject(key, defaultValue || {}) }