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

// #region 整体内容
type State = {
  name: string;
  code: string;
};

type Action =
| {
  type: 'SEARCH'
  payload: State
}
| {
  type: 'RESET'
};

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

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

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

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

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

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