text: /* global window */
  
  import { router } from 'utils'
  import { stringify } from 'qs'
  import store from 'store'
  // import { ROLE_TYPE } from 'utils/constant'
  import { queryLayout, pathMatchRegexp } from 'utils'
  import { CANCEL_REQUEST_MESSAGE } from 'utils/constant'
  import { queryRouteList, logoutUser, queryUserInfo } from 'api'
  import config from 'config'
  
  export default {
    namespace: 'app',
    state: {
      user: {},
      permissions: {
        visit: [],
      },
      routeList: [
        {
          id: '1',
          icon: 'laptop',
          name: 'Dashboard',
          zhName: '控制台',
          router: '/dashboard',
        },
      ],
      locationPathname: '',
      locationQuery: {},
      theme: store.get('theme') || 'light',
      collapsed: store.get('collapsed') || false,
      notifications: [
        {
          title: 'New User is registered.',
          date: new Date(Date.now() - 10000000),
        },
        {
          title: 'Application has been approved.',
          date: new Date(Date.now() - 50000000),
        },
      ],
    },
    subscriptions: {
      setupHistory({ dispatch, history }) {
        history.listen(location => {
          dispatch({
            type: 'updateState',
            payload: {
              locationPathname: location.pathname,
              locationQuery: location.query,
            },
          })
        })
      },
  
      setupRequestCancel({ history }) {
        history.listen(() => {
          const { cancelRequest = new Map() } = window
  
          cancelRequest.forEach((value, key) => {
            if (value.pathname !== window.location.pathname) {
              value.cancel(CANCEL_REQUEST_MESSAGE)
              cancelRequest.delete(key)
            }
          })
        })
      },
  
      setup({ dispatch }) {
        dispatch({ type: 'query' })
      },
    },
    effects: {
      *query({ payload }, { call, put, select }) {
        try {
          const { code, respData } = yield call(queryUserInfo, payload)
          const { locationPathname } = yield select(_ => _.app)
  
          if (code === 1 && respData) {
            const { respList } = yield call(queryRouteList)
            const { user } = respData
            // const { permissions } = user
            let routeList = respList
            // if (
            //   permissions.role === ROLE_TYPE.ADMIN ||
            //   permissions.role === ROLE_TYPE.DEVELOPER
            // ) {
            //   permissions.visit = list.map(item => item.id)
            // }
            // else {
            // routeList = list.filter(item => {
            //   const cases = [
            //     permissions.visit.includes(item.id),
            //     item.mpid
            //       ? permissions.visit.includes(item.mpid) || item.mpid === '-1'
            //       : true,
            //     item.bpid ? permissions.visit.includes(item.bpid) : true,
            //   ]
            //   return cases.every(_ => _)
            // }
            // )
            // }
            yield put({
              type: 'updateState',
              payload: {
                user,
                // permissions,
                routeList,
              },
            })
            if (pathMatchRegexp('/login', window.location.pathname)) {
              router.push({
                pathname: '/dashboard',
              })
            }
          } else if (queryLayout(config.layouts, locationPathname) !== 'public') {
            router.push({
              pathname: '/login',
              search: stringify({
                from: locationPathname,
              }),
            })
          }
        } catch (error) {
          console.log(error)
          router.push({
            pathname: '/login',
          })
        }
      },
  
      *signOut({ payload }, { call, put }) {
        const data = yield call(logoutUser, payload)
        if (data.success) {
          localStorage.clear()
          yield put({
            type: 'updateState',
            payload: {
              user: {},
              permissions: { visit: [] },
              menu: [
                {
                  id: '1',
                  icon: 'laptop',
                  name: 'Dashboard',
                  zhName: '控制台',
                  router: '/dashboard',
                },
              ],
            },
          })
          yield put({ type: 'query' })
        } else {
          throw data
        }
      },
    },
    reducers: {
      updateState(state, { payload }) {
        return {
          ...state,
          ...payload,
        }
      },
  
      handleThemeChange(state, { payload }) {
        store.set('theme', payload)
        state.theme = payload
      },
  
      handleCollapseChange(state, { payload }) {
        store.set('collapsed', payload)
        state.collapsed = payload
      },
  
      allNotificationsRead(state) {
        state.notifications = []
      },
    },
  }
  