import {fromJS, Map, Set, List} from 'immutable'
import {call, put} from 'redux-saga/effects';
import {Apis} from 'shared/api_client';

/**
    This loadFollows both 'blog' and 'ignore'
*/

// Test limit with 2 (not 1, infinate looping)
export function* loadFollows(method, account, type, start = '', limit = 100) {
    const res = fromJS(yield Apis.follow(method, account, start, type, limit))
    // console.log('res.toJS()', res.toJS())

    let cnt = 0
    let lastAccountName = null
    const accountNameKey = method === "get_following" ? "following" : "follower";

    yield put({type: 'global/UPDATE', payload: {
        key: ['follow', method, account],
        notSet: Map(),
        updater: m => {
            m = m.asMutable()
            res.forEach(value => {
                cnt++

                let whatList = value.get('what')
                if(typeof whatList === 'string')
                    whatList = new List([whatList]) // TODO: after shared-db upgrade, this line can be removed

                const accountName = lastAccountName = value.get(accountNameKey)
                whatList.forEach(what => {
                    //currently this is always true: what === type
                    m.update(what + '_inprogress', Set(), s => s.add(accountName))
                })
            })
            m.merge({[type + '_loading']: true})
            return m.asImmutable()
        }
    }})

    if(cnt === limit) {
        // This is paging each block of up to limit results
        yield call(loadFollows, method, account, type, lastAccountName)
    } else {
        // This condition happens only once at the very end of the list.
        // Every account has a different followers and following list for: blog, ignore
        yield put({type: 'global/UPDATE', payload: {
            key: ['follow', method, account],
            updater: m => {
                m = m.asMutable()

                const result = m.get(type + '_inprogress', Set())

                m.delete(type + '_inprogress')
                m.merge({
                    // Count may be set separately without loading the full xxx_result set
                    [type + '_count']: result.size,
                    [type + '_result']: result.sort().reverse(),
                    [type + '_loading']: false,
                })
                return m.asImmutable()
            }
        }})
    }
}

@@ -674,24 +674,212 @@
 Set: Map(),%0A
+        updater: m =%3E m.set(type + '_loading', true),%0A    %7D%7D)%0A%0A    yield put(%7Btype: 'global/UPDATE', payload: %7B%0A        key: %5B'follow_inprogress', method, account%5D,%0A        notSet: Map(),%0A
         upda
@@ -1411,32 +1411,16 @@
 ate(what
- + '_inprogress'
 , Set(),
@@ -1483,57 +1483,8 @@
  %7D)%0A
-            m.merge(%7B%5Btype + '_loading'%5D: true%7D)%0A
     
@@ -1938,33 +1938,8 @@
 y: %5B
-'follow', method, account
 %5D,%0A 
@@ -2036,25 +2036,27 @@
  = m.get
-(type + '
+In(%5B'follow
 _inprogr
@@ -2064,16 +2064,40 @@
 ss',
+ method, account, type%5D,
  Set())%0A
 %0A   
@@ -2088,25 +2088,24 @@
 pe%5D, Set())%0A
-%0A
             
@@ -2116,25 +2116,27 @@
 m.delete
-(type + '
+In(%5B'follow
 _inprogr
@@ -2139,34 +2139,112 @@
 rogress'
-)%0A                
+, method, account, type%5D)%0A                m.updateIn(%5B'follow', method, account%5D, Map(), mm =%3E m
 m.merge(
@@ -2511,32 +2511,33 @@
               %7D)
+)
 %0A               

Refactor follow, moved _inprogress outside of follow tree. #745