import type { BrowserWindow, MenuItemConstructorOptions, } from 'electron'; import type { YoutubePlayer } from '../../types/youtube-player'; export type PluginBaseConfig = { enabled: boolean; }; export type BasePlugin = { onLoad?: () => void; onUnload?: () => void; onConfigChange?: (newConfig: Config) => void; } export type RendererPlugin = BasePlugin & { onPlayerApiReady?: (api: YoutubePlayer) => void; }; export type MainPlugin = Omit, 'onLoad' | 'onUnload'> & { onLoad?: (window: BrowserWindow) => void; onUnload?: (window: BrowserWindow) => void; }; export type PreloadPlugin = BasePlugin; type DeepPartial = { [P in keyof T]?: DeepPartial; }; type IF = (args: T) => T; type Promisable = T | Promise; export type PluginContext = { getConfig: () => Promisable; setConfig: (config: DeepPartial) => Promisable; }; export type MainPluginContext = PluginContext & { send: (event: string, ...args: unknown[]) => void; handle: (event: string, listener: (...args: Arguments) => Promisable) => void; on: (event: string, listener: (...args: Arguments) => Promisable) => void; }; export type RendererPluginContext = PluginContext & { invoke: (event: string, ...args: unknown[]) => Promise; on: (event: string, listener: (...args: Arguments) => Promisable) => void; }; export type MenuPluginContext = PluginContext & { window: BrowserWindow; refresh: () => void; }; export type RendererPluginFactory = (context: RendererPluginContext) => Promisable>; export type MainPluginFactory = (context: MainPluginContext) => Promisable>; export type PreloadPluginFactory = (context: PluginContext) => Promisable>; export type MenuPluginFactory = (context: MenuPluginContext) => Promisable; export type PluginBuilder = { createRenderer: IF>; createMain: IF>; createPreload: IF>; createMenu: IF>; id: ID; config: Config; name?: string; styles?: string[]; restartNeeded: boolean; }; export type PluginBuilderOptions = { name?: string; restartNeeded: boolean; config: Config; styles?: string[]; } export const createPluginBuilder = ( id: ID, options: PluginBuilderOptions, ): PluginBuilder & PluginBaseConfig> => ({ createRenderer: (plugin) => plugin, createMain: (plugin) => plugin, createPreload: (plugin) => plugin, createMenu: (plugin) => plugin, id, name: options.name, config: options.config, styles: options.styles, restartNeeded: options.restartNeeded, });