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

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

type Action =
| {type: 'SEARCH', payload: Omit<State, 'isSearching'>}
| {type: 'CHANGE_SEARCH_STATE', payload: boolean};

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

function reducer(state: State, action: Action): State {
  switch (action.type) {
    case 'SEARCH':
      return {...state, ...action.payload};
    case 'CHANGE_SEARCH_STATE':
      return {...state, isSearching: 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();
