import type { AnyInteractionLink } from "../interactionLink.schema"
import type { FrontendPayload } from "@c/payload/payloadTypes"
import { PluginFastifyInstance } from "types"

export const registerEvents = (fastify: PluginFastifyInstance) => {
  /*
   * interactionLink created and created
   */
  const created = async (payload: FrontendPayload<AnyInteractionLink>) => {
    fastify.eventbus.emit("broadcastInteractionLinkCreated", payload)
    fastify.log.info(
      `[eventbus] interactionLink broadcast created ${payload.body.items}`,
    )
  }

  /*
   * interactionLink deleted
   */
  const deleted = async (payload: FrontendPayload<AnyInteractionLink>) => {
    fastify.eventbus.emit("broadcastInteractionLinkDeleted", payload)
    fastify.log.info(
      `[eventbus] interactionLink broadcast deleted ${payload.body.items}`,
    )
  }

  /**
   * Register those functions for eventbus events
   */
  fastify.eventbus.on("coreInteractionLinkCreated", created)
  fastify.eventbus.on("coreInteractionLinkDeleted", deleted)
}

export default registerEvents
