import {createPageContext, createSearchContext} from '@hooks';
import {useReducer} from 'react';
import {createContext} from 'use-context-selector';

/** 页码管理 */
export const pageContext = createPageContext();

/** 搜索状态管理 */
export const searchContext = createSearchContext();

// #region 搜索内容
type State = {
  department: string,
  type: string,
  name: string,
};

type Action = {type: 'SEARCH', payload: State};

function initState(): State {
  return {
    department: '',
    type: '',
    name: '',
  };
}

function reducer(state: State, action: Action): State {
  const {type, payload} = action;

  switch (type) {
    case 'SEARCH':
      return {...state, ...payload};
    default:
      return state;
  }
}

export function useContextReducer() {
  return useReducer(reducer, initState());
}

export const context = createContext<ReturnType<typeof useContextReducer>>(
  [initState(), () => null],
);
// #endregion
