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

//#region 搜索参数
type State = {
  code: string;
  name: string;
};

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

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

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

function initState(): State {
  return {code: '', name: ''};
}
export function useContextReducer() {
  return useReducer(reducer, initState());
}

export const context = createContext<[State, Dispatch<Action>]>([initState(), () => null]);
//#endregion

export const pageContext = createPageContext();
