import { getDevtoolsGlobalHook, getTarget, isProxyAvailable } from './env.js'
import { HOOK_SETUP } from './const.js'
import type { DevtoolsPluginApi } from './api/index.js'
import { ApiProxy } from './proxy.js'
import type { ExtractSettingsTypes, PluginDescriptor, PluginSettingsItem } from './plugin.js'
export * from './api/index.js'
export * from './plugin.js'
export * from './time.js'
export { PluginQueueItem } from './env.js'
// https://github.com/microsoft/TypeScript/issues/30680#issuecomment-752725353
type Cast = A extends B ? A : B
type Narrowable =
| string
| number
| bigint
| boolean
type Narrow = Cast })>
// Prevent properties not in PluginDescriptor
// We need this because of the `extends` in the generic TDescriptor
type Exact = {
[K in keyof C]: K extends keyof T ? T[K] : never
}
export type SetupFunction = (api: DevtoolsPluginApi) => void
export function setupDevtoolsPlugin<
TDescriptor extends Exact,
TSettings = ExtractSettingsTypes ? S : Record : Record>,
>(pluginDescriptor: Narrow, setupFn: SetupFunction) {
const descriptor = pluginDescriptor as unknown as PluginDescriptor
const target = getTarget()
const hook = getDevtoolsGlobalHook()
const enableProxy = isProxyAvailable && descriptor.enableEarlyProxy
if (hook && (target.__VUE_DEVTOOLS_PLUGIN_API_AVAILABLE__ || !enableProxy)) {
hook.emit(HOOK_SETUP, pluginDescriptor, setupFn)
}
else {
const proxy = enableProxy ? new ApiProxy(descriptor, hook) : null
const list = target.__VUE_DEVTOOLS_PLUGINS__ = target.__VUE_DEVTOOLS_PLUGINS__ || []
list.push({
pluginDescriptor: descriptor,
setupFn,
proxy,
})
if (proxy) {
setupFn(proxy.proxiedTarget as DevtoolsPluginApi)
}
}
}