import React, { createContext, useState } from "react";
import Preloader from "../components/preloader/Preloader";

interface PreloaderProps{
  setPreloaderVisible: (preloaderVisible: boolean) => void;
  setPreloaderText: (preloaderText: string) => void;
}

export const PreloaderContext = createContext<PreloaderProps>({
  setPreloaderVisible: () => {},
  setPreloaderText: () => {},
});

export function PreloaderProvider({ children }){
  const [visible, setVisible] = useState<boolean>(true);
  const [text, setText] = useState<string>('Страница загружается');

  function setPreloaderVisible(preloaderVisible: boolean){
    setVisible(preloaderVisible);
  }

  function setPreloaderText(preloaderText: string){
    setText(preloaderText);
  }

  let value = {
    setPreloaderVisible: setPreloaderVisible,
    setPreloaderText: setPreloaderText
  }

  return (
    <PreloaderContext.Provider value={value}>
      <Preloader
        visible={visible}
        text={text}
      />
      {children}
    </PreloaderContext.Provider>
  )
}
