#!/usr/bin/env node


var fs = require("fs");

var emoji;
try {
    emoji = require("emojione/emoji.json");
} catch (e) {
    console.log("Please `yarn global add emojione` and re-run.");
    process.exit(1);
}

var results = {
    emoji: {},
    categories: [
        { id: "people",   name: "Smilies & People" },
        { id: "nature",   name: "Animals & Nature" },
        { id: "food",     name: "Food & Drink" },
        { id: "activity", name: "Activity" },
        { id: "travel",   name: "Travel & Places" },
        { id: "objects",  name: "Objects" },
        { id: "symbols",  name: "Symbols" },
        { id: "flags",    name: "Flags" }
    ]
}

var categoriesMap = {};
results.categories.forEach(function(category) {
    categoriesMap[category.id] = category;
    category.emoji = [];
});

var orderedEmoji = Object.keys(emoji).map(function(key) { return emoji[key]; });
orderedEmoji.sort(function(a,b) {
    return parseInt(a.emoji_order, 10) - parseInt(b.emoji_order, 10);
});

orderedEmoji.forEach(function(emoji) {
    var category = categoriesMap[emoji.category];
    if (category) {
        category.emoji.push(emoji.shortname);
        results.emoji[emoji.shortname] = parseInt(emoji.unicode, 16);
    }
});

fs.writeFileSync("frontend/src/metabase/lib/emoji.json", JSON.stringify(results));
