import React, { useEffect, useState } from 'react';
import { ConfigProvider, theme } from 'antd';
import { BrowserRouter } from 'react-router-dom';
import Settings from '../pages/Settings';
import AppRoutes from './AppRoutes';
import { LS_IS_DARK_THEME, LS_KEY_DBS_SCHEMA } from '../constants';
import { AppContext } from '../contexts/AppContext';
import { jsx as _jsx } from "react/jsx-runtime";
const {
  defaultAlgorithm,
  darkAlgorithm
} = theme;
export default function App() {
  const [dbs, setDbs] = useState(null);
  useEffect(() => {
    const _dbs = JSON.parse(localStorage.getItem(LS_KEY_DBS_SCHEMA) || '{}');
    setDbs(_dbs);
    if (localStorage.getItem(LS_IS_DARK_THEME) === 'true') {
      document.body.style.backgroundColor = 'black';
    }
  }, []);
  if (!dbs) return /*#__PURE__*/_jsx(Settings, {});
  return /*#__PURE__*/_jsx(AppContext.Provider, {
    value: {
      dbs,
      getTablesByDbName: dbName => {
        return dbs[dbName].tables || [];
      },
      getViewsByDbName: dbName => {
        return dbs[dbName].views || [];
      },
      getViewByDbNameViewName: (dbName, viewName) => {
        const {
          views
        } = dbs[dbName];
        if (!views) {
          return null;
        }
        return views.find(({
          name
        }) => name === viewName) || null;
      }
    },
    children: /*#__PURE__*/_jsx(BrowserRouter, {
      children: /*#__PURE__*/_jsx(ConfigProvider, {
        theme: {
          algorithm: localStorage.getItem(LS_IS_DARK_THEME) === 'true' ? darkAlgorithm : defaultAlgorithm
        },
        children: /*#__PURE__*/_jsx(AppRoutes, {})
      })
    })
  });
}
//# sourceMappingURL=App.js.map