const fs = require('fs')

const emojiFilename = '../static/emoji.json'

const data = fs.readFileSync(emojiFilename, 'utf8')
const currentEmojis = JSON.parse(data)

const currentEmojisArray = Object.entries(currentEmojis).map(([name, codepoint]) => {
  return {
    keyword: name,
    value: codepoint
  }
}).filter(e => {
  const hasSingleCodepoint = Array.from(e.value).length === 1;
  const isSkinToneIndicator = e.value.codePointAt(0) >= 0x1F3FB && e.value.codePointAt(0) <= 0x1F3FF;
  return !(hasSingleCodepoint && isSkinToneIndicator)
})

const collator = new Intl.Collator('en-u-co-emoji')

currentEmojisArray.sort((a, b) => {
  return collator.compare(a.value, b.value)
})

fs.writeFile(emojiFilename, JSON.stringify(currentEmojisArray, null, 2), 'utf8', (err) => {
  if (err) console.log('Error writing file', err)
})
