import React, { useContext, useReducer } from 'react'
import { useDispatch } from 'react-redux'
import { CustomPrintCalculationApi } from '@/api/codeManage'
import type { CodeParam } from '@/reducers/codeData'
import { useSelector } from '@/reducers/hooks'

interface params {
  productData: CodeParam
  dispatch: any
}
interface PropsType {
  children: React.ReactNode
}

function createCtx<A extends {} | null>() {
  const ctx = React.createContext<A | undefined>(undefined)
  function useCtx() {
    const c = useContext(ctx)
    if (c === undefined) { throw new Error('useCtx must be inside a Provider with a value') }
    return c
  }
  return [useCtx, ctx.Provider] as const
}
export const [useCurrenCode, CurrentUserProvider] = createCtx<params>()

export default (props: PropsType) => {
  const productData = useSelector(state => state.codeData)
  const dispatch = useDispatch()
  return (
    <CurrentUserProvider value={{ productData, dispatch }} children={props.children}></CurrentUserProvider>
  )
}
