import {
  ApolloLink,
  CombinedGraphQLErrors
} from "./chunk-JDA2TODV.js";
import {
  Observable,
  isNonNullObject,
  print
} from "./chunk-52FJAJZT.js";
import "./chunk-2PYFANNH.js";
import {
  __publicField
} from "./chunk-2TUXWMP5.js";

// node_modules/@apollo/client/link/subscriptions/index.js
function isLikeCloseEvent(val) {
  return isNonNullObject(val) && "code" in val && "reason" in val;
}
function isLikeErrorEvent(err) {
  var _a;
  return isNonNullObject(err) && ((_a = err.target) == null ? void 0 : _a.readyState) === WebSocket.CLOSED;
}
var GraphQLWsLink = class extends ApolloLink {
  constructor(client) {
    super();
    __publicField(this, "client");
    this.client = client;
  }
  request(operation) {
    return new Observable((observer) => {
      const { query, variables, operationName, extensions } = operation;
      return this.client.subscribe({ variables, operationName, extensions, query: print(query) }, {
        next: observer.next.bind(observer),
        complete: observer.complete.bind(observer),
        error: (err) => {
          if (err instanceof Error) {
            return observer.error(err);
          }
          const likeClose = isLikeCloseEvent(err);
          if (likeClose || isLikeErrorEvent(err)) {
            return observer.error(
              // reason will be available on clean closes
              new Error(`Socket closed${likeClose ? ` with event ${err.code}` : ""}${likeClose ? ` ${err.reason}` : ""}`)
            );
          }
          return observer.error(new CombinedGraphQLErrors({
            errors: Array.isArray(err) ? err : [err]
          }));
        }
        // casting around a wrong type in graphql-ws, which incorrectly expects `Sink<ExecutionResult>`
      });
    });
  }
};
export {
  GraphQLWsLink
};
//# sourceMappingURL=@apollo_client_link_subscriptions.js.map
