// redux状态初始化、切片
import { createSlice } from '@reduxjs/toolkit';
import {useInjectReducer} from "../../../../utils/@reduxjs/injectReducer";
import {
  createUser,
  getAllDataset,
  getAllModals, getAuthList,
  getConditions,
  getDBList,
  getDBSource,
  getDimensions,
  getDomains,
  getIndexByConditions,
  getModals,
  getRoles,
  getTableColumns,
  getTableList,
  getUsers, periodList,
  saveOrUpdateRole
} from "./thunks";

export const initialState = {
  users:[],
  roles:[],
  sourceList:[],
  dbList:[],
  tableList:[],
  tableColumns:[],
  nodes:[],
  edges:[],
  domains:[],
  modals:[],
  indexLib:[],
  conditions:[],
  dataset:[],
  dimensions:[],
  roleAuth:[],
  periods:[]
};

/**
 * dispatch(themeSlice.actions.changeTheme('dark'));
 */
const slice = createSlice({
  name: 'main',
  initialState,
  reducers: {
    updateNodes(state, { payload }) {
      state.nodes = payload;
    },
    updateEdges(state, { payload }) {
      state.edges = payload;
    },
    reset() {
      // 初始化状态
      return initialState;
    },
  },
  extraReducers: builder => {
    // getUsers
    builder.addCase(getUsers.fulfilled, (state, action) => {
      state.users = action.payload.map(item=>({...item,key:item.id}))
    });

    // createUser
    builder.addCase(createUser.fulfilled, (state, action) => {
      console.log(action);
    });

    // getRoles
    builder.addCase(getRoles.fulfilled, (state, action) => {
      state.roles = action.payload.map(item=>({...item,key:item.id}))
    });

    // getDomains
    builder.addCase(getDomains.fulfilled, (state, action) => {
      state.domains = action.payload
    });

    // getModals
    builder.addCase(getModals.fulfilled, (state, action) => {
      state.modals = action.payload
    });

    // getAllModals
    builder.addCase(getAllModals.fulfilled, (state, action) => {
      state.modals = action.payload
    });

    // saveOrUpdateRole
    builder.addCase(saveOrUpdateRole.fulfilled, (state, action) => {
      console.log(action)
    });

    // getDBSource
    builder.addCase(getDBSource.fulfilled, (state, action) => {
      state.sourceList = action.payload.map(item=>({
        ...item,
        value:item.id,
        label:item.name,
        key:item.id
      }))
    });

    // getDBList
    builder.addCase(getDBList.fulfilled, (state, action) => {
      state.dbList = action.payload.map(item=>({
        label:item,
        value:item
      }))
    });
    // getTableList
    builder.addCase(getTableList.fulfilled, (state, action) => {
      state.tableList = action.payload.map(item=>({
        label:item,
        value:item
      }))
    });
    // getTableColumns
    builder.addCase(getTableColumns.fulfilled, (state, action) => {
      state.tableColumns = action.payload.map(item=>({
        ...item,
        label:item.comment || item.columnName,
        value:item.columnName
      }))
    });

    // getIndexByConditions
    builder.addCase(getIndexByConditions.fulfilled, (state, action) => {
      state.indexLib = action.payload.list
    });

    // getConditions
    builder.addCase(getConditions.fulfilled, (state, action) => {
      state.conditions = action.payload
    });

    // getAllDataset
    builder.addCase(getAllDataset.fulfilled, (state, action) => {
      state.dataset = action.payload
    });

    // getDimensions
    builder.addCase(getDimensions.fulfilled, (state, action) => {
      state.dimensions = action.payload
    });

    // getAuthList
    builder.addCase(getAuthList.fulfilled, (state, action) => {
      state.roleAuth = action.payload
    });

    // periodList
    builder.addCase(periodList.fulfilled,(state, action) => {
      state.periods = action.payload
    })
  },
});

/**
 * 暴露Action、reducer 用于测试用例
 */
export const { actions: mainActions, reducer } = slice;

/**
 * 当dispatch此thunk时自动注入Reducer
 */
export const useMainSlice = () => {
  useInjectReducer({ key: slice.name, reducer: slice.reducer });
  return { actions: slice.actions };
};
