import * as React from 'react';
import { REACT_VERSION } from './constants';
import getFramepayApi from './get-framepay-api';
import type { FramePayContext } from '../../types/context';

const defaultContextValue = {
    api: getFramepayApi(),
    error: null,
    ready: false,
};

let ProvidedContext;
if (/^15.*/.test(REACT_VERSION) || /^0.14.*/.test(REACT_VERSION)) {
    const createReactContext = require('create-react-context');
    ProvidedContext = createReactContext(defaultContextValue);
} else {
    ProvidedContext = React.createContext<FramePayContext>(defaultContextValue);
}

const ContextProvider = ProvidedContext.Provider;
const ContextConsumer = ProvidedContext.Consumer;

export { ContextProvider, ContextConsumer };
