import Taro from '@tarojs/taro'

const CHAT_MESSAGE_TEMPLATE_ID = process.env.TARO_APP_MESSAGE_TEMPLATE_ID || ''

export function hasChatMessageTemplate(): boolean {
  return Boolean(CHAT_MESSAGE_TEMPLATE_ID)
}

export async function requestChatMessageSubscription(): Promise<boolean> {
  if (!CHAT_MESSAGE_TEMPLATE_ID || typeof Taro.requestSubscribeMessage !== 'function') {
    return false
  }

  try {
    const requestSubscribeMessage = Taro.requestSubscribeMessage as unknown as (options: { tmplIds: string[] }) => Promise<Record<string, string>>
    const result = await requestSubscribeMessage({
      tmplIds: [CHAT_MESSAGE_TEMPLATE_ID]
    })
    return result[CHAT_MESSAGE_TEMPLATE_ID] === 'accept'
  } catch {
    return false
  }
}
