import React, { useContext, useEffect, useMemo, useState } from 'react';
import defaultNetworks from './defaultNetworks';
const valueContext = {
    networks: {},
    updateNetworks: () => { },
};
const NetworksContext = React.createContext(valueContext);
export function useNetworks() {
    return useContext(NetworksContext);
}
function NetworksProvider({ children }) {
    const [networks, setNetworks] = useState(undefined);
    useEffect(() => {
        let networksTmp = {};
        const response = localStorage.getItem('CHAIN_PARAMS');
        if (response) {
            const networksData = JSON.parse(response);
            networksTmp = Object.assign({}, networksData);
        }
        else {
            networksTmp = Object.assign({}, defaultNetworks);
            localStorage.setItem('CHAIN_PARAMS', JSON.stringify(defaultNetworks));
        }
        setNetworks((item) => (Object.assign(Object.assign({}, item), networksTmp)));
    }, []);
    const updateNetworks = (newList) => {
        localStorage.setItem('CHAIN_PARAMS', JSON.stringify(newList));
        setNetworks((item) => (Object.assign(Object.assign({}, item), newList)));
    };
    const value = useMemo(() => ({ networks, updateNetworks }), [networks]);
    return (React.createElement(NetworksContext.Provider, { value: value }, children));
}
export default NetworksProvider;
//# sourceMappingURL=network.js.map