import { PropsWithChildren } from "react";
import UserProvider, { useUserAPI } from "./userAPI";
import TeamProvider, { useTeamAPI } from "./teamAPI";
import { useImagekitAPI } from "./imagekitAPI";
import PermissionProvider, { usePermissionAPI } from "./permissionAPI";
import { Scope } from "models";
import BinProvider, { useBinAPI } from "./binAPI";
import GateProvider, { useGateAPI } from "./gateAPI";
import NoteProvider, { useNoteAPI } from "./noteAPI";
import DeviceProvider, { useDeviceAPI } from "./deviceAPI";
import BackpackProvider, { useBackpackAPI } from "./backpackAPI";
import GroupProvider, { useGroupAPI } from "./groupAPI";
import NotificationProvider, { useNotificationAPI } from "./notificationAPI";
import TagProvider, { useTagAPI } from "./tagAPI";
import FirmwareProvider, { useFirmwareAPI } from "./firmwareAPI";
import ComponentProvider, { useComponentAPI } from "./componentAPI";
import InteractionProvider, { useInteractionsAPI } from "./interactionsAPI";
import BinYardProvider, { useBinYardAPI } from "./binYardAPI";
import GrainBagProvider, { useGrainBagAPI } from "./grainBagAPI";
import FieldProvider, { useFieldAPI } from "./fieldAPI";
import TransactionProvider, { useTransactionAPI } from "./transactionAPI";
import FileControllerProvider, { useFileControllerAPI } from "./fileControllerAPI";
import MineProvider, { useMineAPI } from "./mineAPI";
// import NoteProvider from "providers/noteAPI";

export const pondURL = (partial: string, demo: boolean = false): string => {
  let url = import.meta.env.VITE_APP_API_URL + (demo ? "/demo" : "") + partial;
  // console.log(url)
  // if (isStaging()) url = process.env.REACT_APP_STAGING_API_URL + (demo ? "/demo" : "") + partial;
  return url;
};

export const objectQueryParams = (scope: Scope) => {
  return "?" + scope.kind + "_id=" + scope.key;
};

export default function PondProvider(props: PropsWithChildren<any>) {
  const { children } = props;

  return (
    <UserProvider>
      <TeamProvider>
        <PermissionProvider>
          <BinProvider>
            <GateProvider>
              <NoteProvider>
                <DeviceProvider>
                  <GroupProvider>
                    <BackpackProvider>
                      <NotificationProvider>
                        <TagProvider>
                          <FirmwareProvider>
                            <ComponentProvider>
                              <InteractionProvider>
                                <BinYardProvider>
                                  <GrainBagProvider>
                                    <FieldProvider>
                                      <TransactionProvider>
                                        <FileControllerProvider>
                                          <MineProvider>
                                            {children}
                                          </MineProvider>
                                        </FileControllerProvider>
                                      </TransactionProvider>
                                    </FieldProvider>
                                  </GrainBagProvider>
                                </BinYardProvider>
                              </InteractionProvider>
                            </ComponentProvider>
                          </FirmwareProvider>
                        </TagProvider>
                      </NotificationProvider>
                    </BackpackProvider>
                  </GroupProvider>
                </DeviceProvider>
              </NoteProvider>
            </GateProvider>
          </BinProvider>
        </PermissionProvider>
      </TeamProvider>                                
    </UserProvider>
  );
}

export {
  useBackpackAPI,
  useComponentAPI,
  useDeviceAPI,
  useFirmwareAPI,
  useUserAPI,
  useTagAPI,
  useTeamAPI,
  useImagekitAPI,
  useInteractionsAPI,
  usePermissionAPI,
  useBinAPI,
  useBinYardAPI,
  useGateAPI,
  useGroupAPI,
  useGrainBagAPI,
  useNoteAPI,
  useNotificationAPI,
  useFieldAPI,
  useTransactionAPI,
  useFileControllerAPI,
  useMineAPI,
};
