import { getImageBases, gateWayConfig } from '@/services/common/api';
import React, { useEffect, useState } from 'react';
const UploadContext = React.createContext();

const sleep = time =>
  new Promise((res, rej) => {
    setTimeout(() => {
      res();
    }, time);
  });

const PictureWallProvider = props => {
  const { children, ...rest } = props;
  const [imgBed, setImgBed] = useState([]);

  const update = () =>
    getImageBases('icon,androidMenu,menuNew,logo')
      .then(res => {
        if (res.code === 0) {
          const { data } = res;
          let arr = data.filter(item => item.moduleName !== 'menu') || [];
          setImgBed(arr);
        }
      })
      .catch(err => {
        // eslint-disable-next-line no-console
        console.error(err);
        return sleep(3000).then(update);
      });
  useEffect(() => {
    update();
  }, []);
  // const updateDicName = () => {
  //   gateWayConfig()
  //     .then(res => {
  //       if (res.code === 0 && res.data) {
  //         window.DicNameSERVICE = 'GateWay';
  //       } else {
  //         window.DicNameSERVICE = '';
  //       }
  //     })
  //     .catch(err => {
  //       window.DicNameSERVICE = '';
  //     });
  // };
  // useEffect(() => {
  //   updateDicName();
  // }, []);
  return (
    <UploadContext.Provider value={{ imgBed, update }}>
      {children}
    </UploadContext.Provider>
  );
};

export { UploadContext as default, PictureWallProvider };