import { useState, useEffect } from 'react';
const getNestedProperty = (obj, path) => path.split('.').reduce((acc, key) => acc && acc[key], obj);
export default function useWaitForWindowProperty(propPath, timeout = 5000, interval = 300) {
  const [value, setValue] = useState();
  useEffect(() => {
    if (getNestedProperty(window, propPath) !== undefined) {
      setValue(getNestedProperty(window, propPath));
      return undefined;
    }
    const startTime = Date.now();
    const checkInterval = setInterval(() => {
      const resolvedValue = getNestedProperty(window, propPath);
      if (resolvedValue !== undefined) {
        setValue(resolvedValue);
        clearInterval(checkInterval);
      } else if (Date.now() - startTime >= timeout) {
        clearInterval(checkInterval);
      }
    }, interval);
    return () => {
      clearInterval(checkInterval);
    };
  }, [propPath, interval, timeout]);
  return value;
}