import { createSignal, createMemo, untrack, onCleanup, createEffect, on } from "solid-js";
import { createStore } from "solid-js/store";
import "compare-utf8";
import "../../zero-protocol/src/ast.js";
import "js-xxhash";
import { asQueryInternals } from "../../zql/src/query/query-internals.js";
import { addContextToQuery } from "../../zql/src/query/query-registry.js";
import { DEFAULT_TTL_MS } from "../../zql/src/query/ttl.js";
import { UNKNOWN, createSolidViewFactory } from "./solid-view.js";
import { useZero } from "./use-zero.js";
import "../../shared/src/config.js";
import "@rocicorp/resolver";
import "../../shared/src/resolved-promises.js";
import "../../shared/src/sleep.js";
import "../../replicache/src/hash.js";
import "../../replicache/src/deleted-clients.js";
import "@rocicorp/lock";
import "@rocicorp/logger";
import "../../replicache/src/persist/client-groups.js";
import "../../replicache/src/persist/clients.js";
import "../../shared/src/json-schema.js";
import "../../zero-protocol/src/custom-queries.js";
import "../../zero-protocol/src/client-schema.js";
import "../../zero-schema/src/permissions.js";
import "../../shared/src/document-visible.js";
import "../../shared/src/iterables.js";
import "../../replicache/src/btree/node.js";
import "../../replicache/src/sync/push.js";
import "../../shared/src/valita.js";
import "../../zero-protocol/src/connect.js";
import "../../zero-protocol/src/down.js";
import "../../zero-protocol/src/error.js";
import "../../zero-protocol/src/protocol-version.js";
import "../../zero-protocol/src/push.js";
import "../../zero-protocol/src/version.js";
import "../../shared/src/btree-set.js";
import "../../zero-protocol/src/primary-key.js";
import "../../datadog/src/datadog-log-sink.js";
import "../../zero-client/src/client/reload-error-handler.js";
function createQuery(querySignal, options) {
  return useQuery(querySignal, options);
}
function useQuery(querySignal, options) {
  const [state, setState] = createStore([
    {
      "": void 0
    },
    UNKNOWN
  ]);
  const initialRefetchKey = 0;
  const [refetchKey, setRefetchKey] = createSignal(initialRefetchKey);
  const refetch = () => {
    setRefetchKey((k) => k + 1);
  };
  const zero = useZero();
  const q = createMemo(() => {
    const query = querySignal();
    if (!query) return void 0;
    return addContextToQuery(query, zero().context);
  });
  const qi = createMemo(() => {
    const query = q();
    if (!query) return void 0;
    return asQueryInternals(query);
  });
  const hash = createMemo(() => qi()?.hash());
  const ttl = createMemo(() => normalize(options)?.ttl ?? DEFAULT_TTL_MS);
  const initialTTL = ttl();
  const view = createMemo(() => {
    const currentHash = hash();
    refetchKey();
    if (currentHash === void 0) {
      setState([{ "": void 0 }, UNKNOWN]);
      return void 0;
    }
    const untrackedQuery = untrack(q);
    if (!untrackedQuery) {
      setState([{ "": void 0 }, UNKNOWN]);
      return void 0;
    }
    const v = zero().materialize(
      untrackedQuery,
      createSolidViewFactory(setState, refetch),
      {
        ttl: initialTTL
      }
    );
    onCleanup(() => v.destroy());
    return v;
  });
  createEffect(
    on(
      ttl,
      (currentTTL) => {
        view()?.updateTTL(currentTTL);
      },
      { defer: true }
    )
  );
  return [() => state[0][""], () => state[1]];
}
function normalize(options) {
  return typeof options === "function" ? options() : options;
}
export {
  createQuery,
  useQuery
};
//# sourceMappingURL=use-query.js.map
