#!/usr/bin/env ruby
require 'json'
require 'set'

# Input format (https://emojibase.dev/) is:
#
# [
#   {
#     "label": "greedy skull farmer",
#     "hexcode": "000000",
#     "emoji": "X",
#     "text": "",
#     "type": 1,
#     "version": 0
#     "tags": ["greed","skull","farming",...]
#     ...
#   },
#   ...
# ]
#
# This script removes many Emoji based on a variety of criteria:
#
#   * Partial matching label names
#   * Groups
#   * Unicode versions
#
# Customize to your needs.

json_in = ARGF.read

if json_in.length < 1
  puts "Oops, need JSON data. Pipe in or supply filename."
  exit 1
end

list = JSON.parse(json_in)

# For the curious:
# This script often makes a 'newlist' between steps. This makes sense when you
# realize that these steps used to be a series of *individual* scripts - so
# this is still many times more efficient than spawning separate processes and
# serializing/deserializing JSON between steps!

# =============================================================================
# Group stuff
#
# Official group names:
#     0  Smileys & Emotion
#     1  People & Body
#     2  Components
#     3  Animals & Nature
#     4  Food & Drink
#     5  Travel & Places
#     6  Activities
#     7  Objects
#     8  Symbols
#     9  Flags


# Remove regional indicator letters. They're not meant to be used
# stand-alone and I've seen first hand that they are not widely
# supported as stand-alone characters (at least not yet).
newlist = []
list.each do |e|
  if e['label'].match(/^regional indicator/)
    next
  end
  newlist.push e
end
list = newlist

# Previously:
# Put regional indicator letters with the symbols group and re-sort.
# Add "letter" to the tags list
#list.each do |e|
#  if e['label'].match(/^regional indicator/)
#    e['group'] = 8 # "Symbols"
#    if e['tags']
#      e['tags'].push 'letter'
#    else
#      e['tags'] = ['letter']
#    end
#  end
#end
#newlist = list.sort_by! { |l| l["group"] }
#list = newlist

# Remove "facing right" variants
newlist = []
list.each do |e|
  if e['label'].match(/facing right/)
    next
  end
  newlist.push e
end
list = newlist

# Remove keycaps
newlist = []
list.each do |e|
  if e['label'].match(/keycap:/)
    next
  end
  newlist.push e
end
list = newlist

# Remove families (there's so many and I've never found a use for these!)
newlist = []
list.each do |e|
  if e['label'].match(/family:/)
    next
  end
  newlist.push e
end
list = newlist

# Remove genders (your tasteful joke goes here)
newlist = []
list.each do |e|
  if e['label'].match(/(person|(wo)?man):? /)
    next
  end
  newlist.push e
end
list = newlist

# Remove Japanese language elements (my audience doesn't speak it)
newlist = []
list.each do |e|
  if e['label'].match(/^Japanese/)
    next
  end
  newlist.push e
end
list = newlist

# Delete group 2 (Components)
newlist = []
list.each do |e|
  if e["group"] == 2
    next
  end
  newlist.push e
end
list = newlist

# Delete group 9 (Flags) - they're cool, but there's so many!
newlist = []
list.each do |e|
  if e["group"] == 9
    next
  end
  newlist.push e
end
list = newlist

# Sadly, I'm also going to remove versions greater than 13, even
# though it gets rid of some great emoji. My use case doesn't support
# yet.
newlist = []
list.each do |e|
  if e["version"] > 13
    next
  end
  newlist.push e
end
list = newlist

# There's only 49 unique uppercase words, so I don't think
# this is worth the loss of readability for data savings.
#
# Downcase labels
#list.each do |e|
#  e["label"].downcase!
#end
#
# Downcase (and de-dupe) tags
#list.each do |e|
#  e["tags"] = e["tags"].map(&:downcase).to_set.to_a
#end

newlist = []
list.each do |e|
  newlist.push({
    'label': e["label"],
    'emoji': e["emoji"],
    'group': e["group"],
    'tags': e["tags"]
  })
end
list = newlist

# Write out final list
#puts JSON.pretty_generate(list)
puts JSON.generate(list)
