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

// #region 货品主体context
type State = {
  /** 品名 */
  number: string;
};

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

function initState(): State {
  return {number: ''};
}

function reducer(state: State, action: Action): State {
  switch (action.type) {
    case 'SEARCH':
      return action.payload;
    default:
      return state;
  }
}

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

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

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