import createReducer from '../utils/create-reducer'

import {
  EMPTY_STACK,
  FETCH_STACK_SUCCESS,
  VIEW_WEBENTITY
} from '../actions/stacks'
import { SELECT_CORPUS } from '../actions/corpora'

// methods and args → for API calls
const initialState = {
  selected: null,
  lastRefresh: null,
  webentities: [],
  list: [
    {
      name: 'PROSPECT',
      description: 'PROSPECT description',
      method: 'store.advanced_search_webentities',
      args: [[], [['status', 'DISCOVERED']], ['-indegree', 'name'], 50, 0, false, false, true],
      condition: 'DISCOVERED'
    },
    {
      name: 'IN',
      description: 'IN description',
      method: 'store.get_webentities_by_status',
      args: ['IN', 'name', -1, 0],
      condition: 'IN'
    },
    {
      name: 'UNDECIDED',
      description: 'UNDECIDED description',
      method: 'store.get_webentities_by_status',
      args: ['UNDECIDED', 'name', -1, 0],
      condition: 'UNDECIDED'
    },
    {
      name: 'OUT',
      description: 'OUT description',
      method: 'store.get_webentities_by_status',
      args: ['OUT', 'name', -1, 0],
      condition: 'OUT'
    }
    // {
    //   name: 'TOTAG',
    //   description: 'TOTAG description',
    //   method: 'unwritten_yet',
    //   args: ['COMING...']
    // }
  ]
}

export default createReducer(initialState, {
  // reset
  [SELECT_CORPUS]: (state) => ({
    ...state,
    selected: null,
    lastRefresh: null,
    webentities: []
  }),

  [EMPTY_STACK]: (state) => ({
    ...state,
    selected: null
  }),

  [FETCH_STACK_SUCCESS]: (state, { stack, webentities }) => ({
    ...state,
    selected: stack,
    lastRefresh: Date.now(),
    webentities
  }),

  [VIEW_WEBENTITY]: (state, { webentity }) => ({
    ...state,
    webentities: state.webentities.map((w) => {
      if (w.id === webentity.id) {
        w.viewed = true
      }
      return w
    })
  })
})

@@ -571,32 +571,225 @@
 D'%0A    %7D,%0A    %7B%0A
+      name: 'TOTAG',%0A      description: 'TOTAG description',%0A      method: 'store.get_webentities_mistagged',%0A      args: %5B'IN', true, false, 'name', -1, 0%5D,%0A      condition: 'IN'%0A    %7D,%0A    %7B%0A
       name: 'IN'
@@ -1323,156 +1323,8 @@
   %7D%0A
-    // %7B%0A    //   name: 'TOTAG',%0A    //   description: 'TOTAG description',%0A    //   method: 'unwritten_yet',%0A    //   args: %5B'COMING...'%5D%0A    // %7D%0A
   %5D%0A

include list of webentites to tag in HyBro's stacklist