import { defaultEmojis } from './default_emoji';
import { SlackClient } from './slack-client';

import * as emojione from 'emojione';

export class EmojiService {
    emojiList: { string: string };
    defaultEmojis = defaultEmojis;

    get allEmojis(): string[] {
        return defaultEmojis.concat(Object.keys(this.emojiList));
    }

    constructor(private client: SlackClient) {
        this.initExternalEmojis();
    }

    async initExternalEmojis(): Promise<void> {
        if (!this.emojiList) {
            this.emojiList = await this.client.getEmoji();
        }
    }

    convertEmoji(emoji: string): string {
        if (emoji !== emojione.shortnameToImage(emoji)) {
            return emojione.shortnameToImage(emoji);
        } else if (this.emojiList && !!this.emojiList[emoji.substr(1, emoji.length - 2)]) {
            let image_url = this.emojiList[emoji.substr(1, emoji.length - 2)];
            if (image_url.substr(0, 6) === 'alias:') {
                return this.convertEmoji(`:${image_url.substr(6)}:`);
            } else {
                return `<img class="emojione" src="${image_url}" />`;
            }
        } else {
            return emoji;
        }
    }
}

import { defaultEmojis } from './default_emoji';
import { SlackClient } from './slack-client';

import * as emojione from 'emojione';

export class EmojiService {
    emojiList: { string: string };
    defaultEmojis = defaultEmojis;

    get allEmojis(): string[] {
        return defaultEmojis.concat(Object.keys(this.emojiList));
    }

    constructor(private client: SlackClient) {
        this.initExternalEmojis();
    }

    async initExternalEmojis(): Promise<void> {
        if (!this.emojiList) {
            this.emojiList = await this.client.getEmoji();
        }
    }

    convertEmoji(emoji: string): string {
        if (this.emojiList && !!this.emojiList[emoji.substr(1, emoji.length - 2)]) {
            let image_url = this.emojiList[emoji.substr(1, emoji.length - 2)];
            if (image_url.substr(0, 6) === 'alias:') {
                return this.convertEmoji(`:${image_url.substr(6)}:`);
            } else {
                return `<img class="emojione" src="${image_url}" />`;
            }
        } else if (emoji !== emojione.shortnameToImage(emoji)) {
            return emojione.shortnameToImage(emoji);
        } else {
            return emoji;
        }
    }
}

Fix issue about custom emoji.
Fix issue about custom emoji.

If both custom emoji and emojione's emoji exist, custom emoji should be shown.
