'use strict';

var React = require('react');

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var React__namespace = /*#__PURE__*/_interopNamespace(React);

// src/db/react.ts
function isNeetruClient(input) {
  const maybe = input;
  if (typeof maybe.syncState !== "undefined") return false;
  const db = maybe.db;
  return !!db && typeof db.syncState !== "undefined" && typeof db.onSyncStateChanged === "function";
}
function resolveSyncStateSource(input) {
  return isNeetruClient(input) ? input.db : input;
}
function serializeQuery(query) {
  if (!query) return "";
  try {
    return JSON.stringify(query, Object.keys(query).sort());
  } catch {
    return String(query);
  }
}
function useDocument(ref, id) {
  const [state, setState] = React__namespace.useState({
    data: null,
    loading: true,
    fromCache: true,
    stale: false
  });
  const mountedRef = React__namespace.useRef(true);
  React__namespace.useEffect(() => {
    mountedRef.current = true;
    setState({ data: null, loading: true, fromCache: true, stale: false });
    const docRef = ref.doc(id);
    const unsubscribe = docRef.onSnapshot(
      (snap) => {
        if (!mountedRef.current) return;
        if (snap === null || snap.docs.length === 0) {
          setState({
            data: null,
            loading: false,
            fromCache: snap?.fromCache ?? true,
            stale: snap?.stale ?? false
          });
        } else {
          setState({
            data: snap.docs[0]?.data ?? null,
            loading: false,
            fromCache: snap.fromCache,
            stale: snap.stale
          });
        }
      }
    );
    return () => {
      mountedRef.current = false;
      unsubscribe();
    };
  }, [ref, id]);
  return state;
}
function useCollection(ref, query) {
  const [state, setState] = React__namespace.useState({
    docs: [],
    loading: true,
    fromCache: true,
    stale: false,
    hasPendingWrites: false
  });
  const mountedRef = React__namespace.useRef(true);
  const queryKey = serializeQuery(query);
  const queryRef = React__namespace.useRef(query);
  queryRef.current = query;
  React__namespace.useEffect(() => {
    mountedRef.current = true;
    setState({
      docs: [],
      loading: true,
      fromCache: true,
      stale: false,
      hasPendingWrites: false
    });
    const unsubscribe = ref.onSnapshot(
      queryRef.current,
      (snap) => {
        if (!mountedRef.current) return;
        setState({
          docs: snap.docs,
          loading: false,
          fromCache: snap.fromCache,
          stale: snap.stale,
          hasPendingWrites: snap.hasPendingWrites
        });
      }
    );
    return () => {
      mountedRef.current = false;
      unsubscribe();
    };
  }, [ref, queryKey]);
  return state;
}
function useSyncState(input) {
  const source = resolveSyncStateSource(input);
  const [state, setState] = React__namespace.useState(() => source.syncState);
  const mountedRef = React__namespace.useRef(true);
  React__namespace.useEffect(() => {
    mountedRef.current = true;
    if (mountedRef.current) {
      setState(source.syncState);
    }
    const unsubscribe = source.onSyncStateChanged((s) => {
      if (!mountedRef.current) return;
      setState(s);
    });
    return () => {
      mountedRef.current = false;
      unsubscribe();
    };
  }, [source]);
  return state;
}

exports.useCollection = useCollection;
exports.useDocument = useDocument;
exports.useSyncState = useSyncState;
//# sourceMappingURL=db-react.cjs.map
//# sourceMappingURL=db-react.cjs.map