import React, { useState, createContext, useEffect } from 'react';
import PropTypes from 'prop-types';
import { getCookie } from '~helpers/cookies';

export const ModalContext = createContext(false);

const ModalProvider = ({ children }) => {
  const [ck, setCk] = useState(true);
  const [showModal, setShowModal] = useState(false);
  const [modalContent, setModalContent] = useState('');

  useEffect(() => {
    const getCk = Boolean(getCookie('CookieConsent')) || false;
    setCk(getCk);
  }, [ck]);

  return (
    <ModalContext.Provider
      value={{ showModal, setShowModal, ck, setCk, modalContent, setModalContent }}
    >
      {children}
    </ModalContext.Provider>
  );
};

ModalProvider.propTypes = {
  children: PropTypes.node,
};

export default ModalProvider;
