import {type BlueprintPubSubFunctionConfig, type BlueprintPubSubFunctionResource, validatePubSubFunction} from '../../index.js'
import {runValidation} from '../../utils/validation.js'
import {defineFunction} from './index.js'

/**
 * Defines a function that provide pubsub behaviour.
 *
 * @remarks
 * A barebones pubsub function
 * ```ts
 * definePubSubFunction({
 *   name: 'send-email',
 * })
 * ```
 * @public
 * @category Definers
 * @expandType BlueprintPubSubFunctionConfig
 * @param functionConfig The configuration for the pubsub function
 * @returns The validated pubsub function resource
 */
export function definePubSubFunction(functionConfig: BlueprintPubSubFunctionConfig): BlueprintPubSubFunctionResource {
  const functionResource: BlueprintPubSubFunctionResource = {
    ...defineFunction(functionConfig, {skipValidation: true}),
    type: 'sanity.function.pubsub',
  }

  runValidation(() => validatePubSubFunction(functionResource))

  return functionResource
}
