import { createContext, useContext } from 'react'

import {
  defaultLockedAttachmentContextValue,
  type LockedAttachmentContextValue,
} from './LockedAttachment/types'

export interface CustomMessageRegistry {
  LockedAttachment: LockedAttachmentContextValue
}

const customMessageDefaults: CustomMessageRegistry = {
  LockedAttachment: defaultLockedAttachmentContextValue,
}

const CustomMessageContext = createContext<Partial<CustomMessageRegistry>>({})

export const CustomMessageProvider = CustomMessageContext.Provider

export function useCustomMessage<K extends keyof CustomMessageRegistry>(
  key: K
): CustomMessageRegistry[K] {
  const ctx = useContext(CustomMessageContext)
  return (ctx[key] ?? customMessageDefaults[key]) as CustomMessageRegistry[K]
}
