"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var client_exports = {};
__export(client_exports, {
  DocumentStore: () => import_documentStore2.DocumentStore,
  HoudiniClient: () => HoudiniClient,
  createPluginHooks: () => createPluginHooks,
  fetch: () => import_plugins2.fetch,
  mutation: () => import_plugins2.mutation,
  query: () => import_plugins2.query,
  subscription: () => import_plugins2.subscription
});
module.exports = __toCommonJS(client_exports);
var import_cache = __toESM(require("../cache"), 1);
var import_lib = require("../lib");
var import_flatten = require("../lib/flatten");
var import_documentStore = require("./documentStore");
var import_plugins = require("./plugins");
var import_injectedPlugins = __toESM(require("./plugins/injectedPlugins"), 1);
var import_documentStore2 = require("./documentStore");
var import_plugins2 = require("./plugins");
class HoudiniClient {
  url;
  throwOnError_operations;
  cache = null;
  throwOnError;
  fetchParams;
  pipeline;
  extraPlugins;
  proxies = {};
  componentCache = {};
  setCache(cache) {
    this.cache = cache;
  }
  constructor({
    url,
    fetchParams,
    plugins,
    pipeline,
    throwOnError
  } = {}) {
    if (plugins && pipeline) {
      throw new Error(
        "A client cannot be given a pipeline and a list of plugins at the same time."
      );
    }
    this.throwOnError_operations = throwOnError?.operations ?? [];
    let serverPort = globalThis.process?.env?.HOUDINI_PORT ?? "5173";
    this.url = url ?? (globalThis.window ? "" : `https://localhost:${serverPort}`) + (0, import_lib.localApiEndpoint)((0, import_lib.getCurrentConfig)());
    this.throwOnError = throwOnError;
    this.fetchParams = fetchParams;
    this.pipeline = pipeline;
    this.extraPlugins = plugins;
  }
  get plugins() {
    return (0, import_flatten.flatten)(
      [].concat(
        this.throwOnError ? [(0, import_plugins.throwOnError)(this.throwOnError)] : [],
        (0, import_plugins.fetchParams)(this.fetchParams),
        this.pipeline ?? [
          (0, import_plugins.query)(this.cache ?? import_cache.default),
          (0, import_plugins.mutation)(this.cache ?? import_cache.default),
          (0, import_plugins.fragment)(this.cache ?? import_cache.default)
        ].concat(
          this.extraPlugins ?? [],
          import_injectedPlugins.default,
          (0, import_plugins.fetch)()
        )
      )
    );
  }
  observe({
    enableCache = true,
    fetching = false,
    ...rest
  }) {
    return new import_documentStore.DocumentStore({
      client: this,
      plugins: createPluginHooks(this.plugins),
      fetching,
      enableCache,
      cache: this.cache ?? void 0,
      ...rest
    });
  }
  registerProxy(url, handler) {
    this.proxies[url] = handler;
  }
}
function createPluginHooks(plugins) {
  return plugins.reduce((hooks, plugin) => {
    if (typeof plugin !== "function") {
      throw new Error("Encountered client plugin that's not a function");
    }
    const result = plugin();
    if (!result) {
      return hooks;
    }
    if (!Array.isArray(result)) {
      return hooks.concat(result);
    }
    for (const value of result) {
      if (!value) {
        continue;
      }
      if (typeof value === "function") {
        return hooks.concat(createPluginHooks([value]));
      }
      hooks.push(value);
    }
    return hooks;
  }, []);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DocumentStore,
  HoudiniClient,
  createPluginHooks,
  fetch,
  mutation,
  query,
  subscription
});
