// Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ import type { App } from 'vue' import { plugin as formPlugin, bindings as bindingsPlugin } from '@formkit/vue' import type { FormKitConfig, FormKitPlugin } from '@formkit/core' import { createThemePlugin } from '@formkit/themes' import '@formkit/dev' import type { FormAppSpecificTheme, FormDecoratorIcons, FormFieldTypeImportModules, } from '#shared/types/form.ts' import type { ImportGlobEagerOutput, ImportGlobEagerDefault, } from '#shared/types/utils.ts' import createFieldPlugin from './core/createFieldPlugin.ts' import createValidationPlugin from './core/createValidationPlugin.ts' import createI18nPlugin from './core/createI18nPlugin.ts' import createTailwindClasses from './core/createTailwindClasses.ts' import createCustomIcons from './core/createCustomIcons.ts' export const getFormPlugins = ( modules: ImportGlobEagerOutput, ): FormKitPlugin[] => { const plugins: Array = [] Object.values(modules).forEach( (module: ImportGlobEagerDefault) => { const plugin = module.default plugins.push(plugin) }, ) return plugins } const pluginModules: ImportGlobEagerOutput = import.meta.glob( './plugins/global/*.ts', { eager: true }, ) const plugins = getFormPlugins(pluginModules) const checkIcon = '' export const buildFormKitPluginConfig = ( config?: FormKitConfig, appSpecificFieldModules: ImportGlobEagerOutput = {}, appSpecificPlugins: FormKitPlugin[] = [], appSpecificTheme: FormAppSpecificTheme = {}, appSpecificDecoratorIcons: FormDecoratorIcons = {}, ) => { const customIcons = createCustomIcons() return { plugins: [ createFieldPlugin(appSpecificFieldModules), bindingsPlugin, createI18nPlugin(), createValidationPlugin(), createThemePlugin( undefined, { checkboxDecorator: appSpecificDecoratorIcons.checkboxDecorator || checkIcon, radioDecorator: appSpecificDecoratorIcons.radioDecorator || checkIcon, ...customIcons, }, undefined, () => undefined, ), ...plugins, ...appSpecificPlugins, ], locale: 'staticLocale', config: { classes: createTailwindClasses(appSpecificTheme), ...config, }, props: { dirtyBehavior: 'compare', }, } } export default function initializeForm( app: App, appSpecificConfig?: FormKitConfig, appSpecificFieldModules: ImportGlobEagerOutput = {}, appSpecificPlugins: FormKitPlugin[] = [], appSpecificTheme: FormAppSpecificTheme = {}, appSpecificDecoratorIcons: FormDecoratorIcons = {}, ) { app.use( formPlugin, buildFormKitPluginConfig( appSpecificConfig, appSpecificFieldModules, appSpecificPlugins, appSpecificTheme, appSpecificDecoratorIcons, ), ) }