
var get_posts = function(pages){
    var posts = []
    for (let i = 0; i < pages.data.length; i++) {
        const page = pages.data[i];
        if(page.layout == "post") posts.push(page);
    }
    return posts;
}

var get_projects = function(pages){
    var projects = [];
    pages.forEach(page => {
        if(page.layout == "project") projects.push(page);
    });
    return projects;
}

hexo.extend.helper.register('project_count', function(pages){
    return get_projects(pages).length;
});

hexo.extend.helper.register('blog_count', function(pages){
    return get_posts(pages).length;
});

hexo.extend.helper.register('is_video_provider', function(url, provider){
    url = url.replace('http://', '')
    url = url.replace('https://', '')
    url = url.replace('www', '')
    var parts = url.split('.');
    return parts[0] == provider ? true : false;
});

hexo.extend.helper.register('youtube_embed_link', function(url){
    url = url.replace('http://', '')
    url = url.replace('https://', '')
    var parts = url.split('/');
    return `http://www.youtube.com/embed/${parts[2]}`;
});

hexo.extend.helper.register('vimeo_embed_link', function(url){
    url = url.replace('http://', '')
    url = url.replace('https://', '')
    url = url.replace('vimeo.com/', '')
    return `https://player.vimeo.com/video/${url}`;
});

hexo.extend.helper.register('order_projects', function(pages){
    var projects = get_projects(pages);
    projects.sort(function(a,b){
        return new Date(b.date) - new Date(a.date);
    });
    return projects;
});

hexo.extend.helper.register('order_posts', function(pages){
    var posts = get_posts(pages)
    posts.sort(function(a,b){
        return new Date(b.date) - new Date(a.date);
    });
    return posts;
});

hexo.extend.helper.register('next_post', function(current_post, pages){
    var posts = get_posts(pages);
    posts.sort(function(a,b){
        return new Date(b.date) - new Date(a.date);
    });
    for (let i = 0; i < posts.length; i++) {
        if(posts[i].title == current_post.title){
            if(i + 1 < posts.length) return posts[i + 1];
            else return posts[0];
        }
    }
});

hexo.extend.helper.register('featured_projects', function(pages){
    var projects = get_projects(pages);
    projects.sort(function(a,b){
        return new Date(b.date) - new Date(a.date);
    });
    var featured = [];
    projects.forEach(project => {
        if(project.featured == true) featured.push(project);
    });
    return featured;
});

hexo.extend.helper.register('tagged', function(pages, search_tag){
    var tagged = []
    pages.data.forEach(page => {
        for (let i = 0; i < page.tags.data.length; i++) {
            if(page.tags.data[i].name == search_tag) {
                tagged.push(page);
                break;
            }
        }
    });
    //console.log(`Found ${tagged.length} posts with tag \'${search_tag}\' in ${pages.length} posts`);
    return tagged;
});

hexo.extend.helper.register('limit', function(pages, limit){
    var list = []
    for (let i = 0; i < pages.length; i++) {
        if(i < limit) list.push(pages[i]);
    }
    return list;
});

hexo.extend.helper.register('remove_from_list', function(pages, page){
    var list = { data : [], length : 0 }
    for (let i = 0; i < pages.length; i++) {
        if(pages.data[i].title != page.title) list.data.push(pages.data[i]);
    }
    list.length = list.data.length;
    return list;
});