import Handlebars from "handlebars"

const createDocumentTemplateHandlebars = () => {
  const instance = Handlebars.create()

  instance.registerHelper("eq", (left, right) => left === right)
  instance.registerHelper("ne", (left, right) => left !== right)
  instance.registerHelper("gt", (left, right) => left > right)
  instance.registerHelper("gte", (left, right) => left >= right)
  instance.registerHelper("lt", (left, right) => left < right)
  instance.registerHelper("lte", (left, right) => left <= right)
  instance.registerHelper("and", (...args) => args.slice(0, -1).every(Boolean))
  instance.registerHelper("or", (...args) => args.slice(0, -1).some(Boolean))
  instance.registerHelper("not", (value) => !value)
  instance.registerHelper("includes", (collection, value) => {
    if (Array.isArray(collection) || typeof collection === 'string') {
      return collection.includes(value)
    }

    return false
  })

  return instance
}

export const documentTemplateHandlebars = createDocumentTemplateHandlebars()
