import { getDefaultState as h } from "./mutation.js";
import { notifyManager as n } from "./notifyManager.js";
import { Subscribable as a } from "./subscribable.js";
import { shallowEqualObjects as u, hashKey as r } from "./utils.js";
var l = class extends a {
  #s;
  #i = void 0;
  #t;
  #e;
  constructor(t, s) {
    super(), this.#s = t, this.setOptions(s), this.bindMethods(), this.#o();
  }
  bindMethods() {
    this.mutate = this.mutate.bind(this), this.reset = this.reset.bind(this);
  }
  setOptions(t) {
    const s = this.options;
    this.options = this.#s.defaultMutationOptions(t), u(this.options, s) || this.#s.getMutationCache().notify({
      type: "observerOptionsUpdated",
      mutation: this.#t,
      observer: this
    }), s?.mutationKey && this.options.mutationKey && r(s.mutationKey) !== r(this.options.mutationKey) ? this.reset() : this.#t?.state.status === "pending" && this.#t.setOptions(this.options);
  }
  onUnsubscribe() {
    this.hasListeners() || this.#t?.removeObserver(this);
  }
  onMutationUpdate(t) {
    this.#o(), this.#r(t);
  }
  getCurrentResult() {
    return this.#i;
  }
  reset() {
    this.#t?.removeObserver(this), this.#t = void 0, this.#o(), this.#r();
  }
  mutate(t, s) {
    return this.#e = s, this.#t?.removeObserver(this), this.#t = this.#s.getMutationCache().build(this.#s, this.options), this.#t.addObserver(this), this.#t.execute(t);
  }
  #o() {
    const t = this.#t?.state ?? h();
    this.#i = {
      ...t,
      isPending: t.status === "pending",
      isSuccess: t.status === "success",
      isError: t.status === "error",
      isIdle: t.status === "idle",
      mutate: this.mutate,
      reset: this.reset
    };
  }
  #r(t) {
    n.batch(() => {
      if (this.#e && this.hasListeners()) {
        const s = this.#i.variables, i = this.#i.context, o = {
          client: this.#s,
          meta: this.options.meta,
          mutationKey: this.options.mutationKey
        };
        if (t?.type === "success") {
          try {
            this.#e.onSuccess?.(
              t.data,
              s,
              i,
              o
            );
          } catch (e) {
            Promise.reject(e);
          }
          try {
            this.#e.onSettled?.(
              t.data,
              null,
              s,
              i,
              o
            );
          } catch (e) {
            Promise.reject(e);
          }
        } else if (t?.type === "error") {
          try {
            this.#e.onError?.(
              t.error,
              s,
              i,
              o
            );
          } catch (e) {
            Promise.reject(e);
          }
          try {
            this.#e.onSettled?.(
              void 0,
              t.error,
              s,
              i,
              o
            );
          } catch (e) {
            Promise.reject(e);
          }
        }
      }
      this.listeners.forEach((s) => {
        s(this.#i);
      });
    });
  }
};
export {
  l as MutationObserver
};
