# -*- coding: utf8 -*-
from flask import url_for

def get_article_type(item):
    if 'type' in item:
        return item['type']
    if 'article_type' in item:
        return item['article_type']
    return None


def url_for_article(article):
    '''Generate a URL for an article'''

    article_type = get_article_type(article);

    if article_type == 'legacy-promotion':
        return url_for('promotions.promotion_by_slug', slug=article['slug'])

    if article_type == 'video':
        return url_for('video.video_by_slug', slug=article['slug'])

    article_variants = ['gallery']
    route = article_type if article_type in article_variants else 'article'
    return url_for('article.%s_by_slug' % route, slug=article['slug'])

GLOBALS = {
    'url_for_article': url_for_article
}