"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var query_exports = {};
__export(query_exports, {
  query: () => query
});
module.exports = __toCommonJS(query_exports);
var import_types = require("../../lib/types");
var import_utils = require("../utils");
const query = (cache) => (0, import_utils.documentPlugin)(import_types.ArtifactKind.Query, function() {
  let subscriptionSpec = null;
  let lastVariables = null;
  return {
    start(ctx, { next }) {
      const runtimeScalarPayload = {
        session: ctx.session
      };
      ctx.variables = {
        ...lastVariables,
        ...Object.fromEntries(
          Object.entries(ctx.artifact.input?.runtimeScalars ?? {}).map(
            ([field, type]) => {
              const runtimeScalar = ctx.config.features?.runtimeScalars?.[type];
              if (!runtimeScalar) {
                return [field, type];
              }
              return [field, runtimeScalar.resolve(runtimeScalarPayload)];
            }
          )
        ),
        ...ctx.variables
      };
      next(ctx);
    },
    end(ctx, { resolve, marshalVariables, variablesChanged }) {
      if (variablesChanged(ctx) && !ctx.cacheParams?.disableSubscriptions) {
        if (subscriptionSpec) {
          cache.unsubscribe(subscriptionSpec, subscriptionSpec.variables?.() || {});
        }
        lastVariables = { ...marshalVariables(ctx) };
        const variables = lastVariables;
        subscriptionSpec = {
          rootType: ctx.artifact.rootType,
          selection: ctx.artifact.selection,
          variables: () => variables,
          set: (newValue) => {
            resolve(ctx, {
              data: newValue,
              errors: null,
              fetching: false,
              partial: false,
              stale: false,
              source: import_types.DataSource.Cache,
              variables: ctx.variables ?? {}
            });
          }
        };
        cache.subscribe(subscriptionSpec, lastVariables);
      }
      resolve(ctx);
    },
    cleanup() {
      if (subscriptionSpec) {
        cache.unsubscribe(subscriptionSpec, subscriptionSpec.variables?.());
        lastVariables = null;
      }
    }
  };
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  query
});
