import c from "@geoscene/core/core/Accessor.js";
import { property as l, subclass as h } from "@geoscene/core/core/accessorSupport/decorators.js";
import p from "@geoscene/core/core/Error.js";
import { a as f, r as _ } from "./maybe.js";
/*! All material copyright GeoScene, All Rights Reserved, unless otherwise specified.
See https://js.geoscene.cn/4.33/geoscene/copyright.txt for details.
v4.33.18 */
var v = Object.defineProperty, m = Object.getOwnPropertyDescriptor, i = (r, t, o, e) => {
  for (var s = e > 1 ? void 0 : e ? m(t, o) : t, a = r.length - 1, u; a >= 0; a--)
    (u = r[a]) && (s = (e ? u(t, o, s) : u(s)) || s);
  return e && s && v(t, o, s), s;
};
function d(r = "Aborted") {
  return new p("AbortError", r);
}
function R(r, t = "Aborted") {
  if (A(r))
    throw d(t);
}
function b(r) {
  return r instanceof AbortSignal ? r : r?.signal ?? void 0;
}
function A(r) {
  return b(r)?.aborted ?? !1;
}
function S(r) {
  return r?.name === "AbortError";
}
function g(r) {
  return { ok: !0, value: r };
}
function y(r) {
  return { ok: !1, error: r };
}
function E(r) {
  return r?.ok ? r.value : null;
}
function w(r) {
  return r != null && !r.ok ? r.error : null;
}
function C(r, t) {
  const o = b(r);
  if (o == null)
    return;
  if (o.aborted) {
    t();
    return;
  }
  const e = () => t();
  return o.addEventListener("abort", e, { once: !0 }), { remove: () => o.removeEventListener("abort", e) };
}
function T(r, t) {
  return new n(r, t);
}
let n = class extends c {
  constructor(r, t) {
    super({}), this._abortHandle = null, this.abort = () => {
      this._abortController = f(this._abortController);
    }, this.remove = this.abort, this._abortController = new AbortController();
    const { signal: o } = this._abortController;
    this.promise = r(o), this.promise.then(
      (e) => {
        this._result = g(e), this._cleanup();
      },
      (e) => {
        this._result = y(e), this._cleanup();
      }
    ), this._abortHandle = C(t, this.abort);
  }
  get value() {
    return E(this._result);
  }
  get error() {
    return w(this._result);
  }
  get finished() {
    return this._result != null;
  }
  destroy() {
    this.abort();
  }
  _cleanup() {
    this._abortHandle = _(this._abortHandle), this._abortController = null;
  }
  [Symbol.dispose]() {
    this._cleanup();
  }
};
i([
  l()
], n.prototype, "value", 1);
i([
  l()
], n.prototype, "error", 1);
i([
  l()
], n.prototype, "finished", 1);
i([
  l()
], n.prototype, "promise", 2);
i([
  l()
], n.prototype, "_result", 2);
n = i([
  h("geoscene.components.asyncUtils.ReactiveTask")
], n);
export {
  S as a,
  T as c,
  A as i,
  R as t
};