import proxify from './proxyUrls';

/**
 * Standardises a 4chan board.
 * 
 * @param  {Object} board   - Directly from 4chan's API
 * @param  {String} boardID - ID used to request board with
 * @return {Array}          - Extracted posts
 */
export function parseBoard( board, boardID ) {
    const img = `https://i.4cdn.org/${boardID}/`;
    var newBoard = [];
    log.app('Parsing board...')

    for (let page in board) {
        if (!board.hasOwnProperty(page)) return false;
        let threads = board[page]['threads'];
		for (let i = 0; i < threads.length; i++) {
		    formatThread(threads[i]);
		}
    }

    if (!newBoard.length) throw new Error("No threads extracted");
    log.app(`Created ${newBoard.length} board posts`);
    return newBoard;

    function formatThread(threadObj) {
        let ext = threadObj['ext']
        let smImg = img + threadObj['tim'] + "s.jpg"
        let lgImg = img + threadObj['tim'] + ext

        let thread = {
        	id: threadObj['no'],
        	date: threadObj['now'],
            title: threadObj['sub'] || "",
            comment: threadObj['com'],
            time: threadObj['tim'] || threadObj['time'] * 1000,
            imgsrc: {
                sm: proxify("/media", {url: smImg, provider: "4chan"}),
                lg: proxify("/media", {url: lgImg, provider: "4chan"})
            },
            replies: {
                textCount: threadObj['replies'],
                imgCount: threadObj['images'],
                ipCount: threadObj['unique_ips']
            }
        }
        newBoard.push(thread);
    }
}


/**
 * Standardises a 4chan thread.
 * 
 * @param  {Object} posts   - From the API
 * @param  {[type]} boardID - Board ID, used for creating media URLs
 * @return {[type]}         [description]
 */
export function parseThread( posts, boardID ) {
    const img = `https://i.4cdn.org/${boardID}/`;

    if (!posts || !posts.length) throw new Error("No thread posts supplied");
    log.app(`Created ${posts.length} 4chan posts`);

    const thread = posts.map( post => {
        let ext = post['ext']
        let smImg = img + post['tim'] + "s.jpg"
        let lgImg = img + post['tim'] + ext

        return {
            id: post['no'],
            date: post['now'],
            name: post['name'],
            hash: post['md5'],
            title: post['sub'] || "",
            time: post['tim'] || post['time'] * 1000,
            comment: post['com'],
            imgsrc: !!post['ext'] ? {
                sm: proxify("/media", {url: smImg, provider: "4chan"}),
                lg: proxify("/media", {url: lgImg, provider: "4chan"})
            } : null,
            ext
        }
    });

    try {
        return connectPosts(thread)
    } catch (e) {
        console.error(thread)
        log.error(e)
        return thread
    }
}


/**
 * Connects thread posts by checking who referenced who then merging 
 * references back into posts
 * 
 * @param  {Array} posts - Each thread post
 * @return {Array}       - Posts merged with references
 * 
 */
function connectPosts(posts) {
    // returns a list of IDs that quoted the current ID
    const ids = posts.map( post => post.id);
    const references = ids.map( (id, index) => {
        var refs = [];
        posts.map( ({ id:referer, comment }) => {
            // Check all comments for ID, add any that refer to ID
            if (comment && comment.includes(id)) {
                refs.push(referer)
            }
        })
        return refs
    });
    
    for (let i=0; i < ids.length; i++) {
        posts[i].references = references[i]
    }
    return posts
}


export function parseBoardList( boardList ) {
    log.app(`Discovered ${boardList.length} 4chan boards`);
    return boardList.map( board => {
        // Essential stuff
        const { board: boardID, title, meta_description } = board, url = `/${boardID}/`;

        // Board info
        const { 
            is_archived, 
            max_filesize,
            max_comment_chars, 
            image_limit, 
            max_webm_duration, 
            max_webm_filesize, 
            ws_board } = board

        return {
            boardID, 
            title,
            url,
            short_desc: `${url} - ${title}`,
            description: meta_description,
            info: {
                is_archived, 
                max_filesize,
                max_comment_chars, 
                image_limit, 
                max_webm_duration, 
                max_webm_filesize, 
                NSFW: 1 - ws_board
            }
        }

    });
}


@@ -2444,29 +2444,20 @@
     
-imgsrc: !!post%5B'
+media: !!
 ext
-'%5D
  ? %7B
@@ -2466,33 +2466,39 @@
                s
-m
+rcSmall
 : proxify(%22/medi
@@ -2543,34 +2543,40 @@
                 
-lg
+srcLarge
 : proxify(%22/medi
@@ -2603,32 +2603,33 @@
 vider: %224chan%22%7D)
+,
 %0A            %7D :
@@ -2629,33 +2629,200 @@
     
-%7D : null,%0A            ext
+    width: post%5B'w'%5D,%0A                height: post%5B'h'%5D,%0A                filesize: post%5B'fsize'%5D,%0A                filename: post%5B'filename'%5D,%0A                filetype: ext%0A            %7D : null
 %0A   

Change 4chan thread parsing structure