const renderMailTemplate = (e, type) => {
  try {
    const language = e.record?.getString('language') ?? 'en'
    const locale = require(`${__hooks}/locales/${language}.json`)

    const subject = $template.loadString(
      locale.mailSubject[type]
    ).render({
      ...e.meta,
      ...e.app.settings().meta
    })

    const html = $template.loadFiles(
      `${__hooks}/templates/${language}/${type}.html`
    ).render({
      ...e.meta,
      ...e.app.settings().meta
    })

    e.message.subject = subject
    e.message.html = html
  } catch (error) {
    console.log(error)
  }

  e.next()
}

module.exports = { renderMailTemplate }
