import type { Config } from 'payload' import type { SeoOption } from './types.js' import { buildAutoFillMetaHook } from './autoFillMeta.js' /** * Injects the auto-fill meta hook into every SEO-enabled collection. * * Runs after @payloadcms/plugin-seo has added the meta group, appending a * beforeChange hook that fills empty meta from document content. Existing * hooks are preserved (plugin hook runs last, so editor input and other hooks * win first). */ export function injectAutoFillMeta(config: Config, seo: SeoOption): Config { // autoFill disabled explicitly — leave collections untouched if (seo.autoFill === false) { return config } const hook = buildAutoFillMetaHook(seo.autoFill) return { ...config, collections: (config.collections ?? []).map((collection) => { if (!seo.collections.includes(collection.slug)) { return collection } return { ...collection, hooks: { ...collection.hooks, beforeChange: [...(collection.hooks?.beforeChange ?? []), hook], }, } }), } }