import b from "@arcgis/core/core/Accessor.js";
import { property as a, subclass as f } from "@arcgis/core/core/accessorSupport/decorators.js";
import d from "@arcgis/core/core/Error.js";
import { a as h, r as p } from "./maybe.js";
/*! All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.34/esri/copyright.txt for details.
v4.34.7 */
var m = Object.defineProperty, w = Object.getOwnPropertyDescriptor, s = (r, t, o, e) => {
  for (var n = e > 1 ? void 0 : e ? w(t, o) : t, l = r.length - 1, u; l >= 0; l--)
    (u = r[l]) && (n = (e ? u(t, o, n) : u(n)) || n);
  return e && n && m(t, o, n), n;
};
function _(r = "Aborted") {
  return new d("AbortError", r);
}
function H(r, t = "Aborted") {
  if (v(r))
    throw _(t);
}
function c(r) {
  return r instanceof AbortSignal ? r : r?.signal ?? void 0;
}
function v(r) {
  return c(r)?.aborted ?? !1;
}
function A(r) {
  return r?.name === "AbortError";
}
function g(r) {
  return { ok: !0, value: r };
}
function C(r) {
  return { ok: !1, error: r };
}
function y(r) {
  return r?.ok ? r.value : null;
}
function E(r) {
  return r != null && !r.ok ? r.error : null;
}
function k(r, t) {
  const o = c(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 P(r, t) {
  return new i(r, t);
}
let i = class extends b {
  constructor(r, t) {
    super({}), this._abortHandle = null, this.abort = () => {
      this._abortController = h(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 = C(e), this._cleanup();
      }
    ), this._abortHandle = k(t, this.abort);
  }
  get value() {
    return y(this._result);
  }
  get error() {
    return E(this._result);
  }
  get finished() {
    return this._result != null;
  }
  destroy() {
    this.abort();
  }
  _cleanup() {
    this._abortHandle = p(this._abortHandle), this._abortController = null;
  }
  [Symbol.dispose]() {
    this._cleanup();
  }
};
s([
  a()
], i.prototype, "value", 1);
s([
  a()
], i.prototype, "error", 1);
s([
  a()
], i.prototype, "finished", 1);
s([
  a()
], i.prototype, "promise", 2);
s([
  a()
], i.prototype, "_result", 2);
i = s([
  f("esri.components.asyncUtils.ReactiveTask")
], i);
function q(r) {
  return r.catch((t) => {
    if (!A(t))
      throw t;
  });
}
function x(r, t = {}) {
  if ("requestIdleCallback" in window)
    return window.requestIdleCallback(r, t);
  const o = t.timeout != null ? t.timeout : 50, e = typeof performance < "u" && performance.now ? performance.now.bind(performance) : Date.now, n = e();
  return window.setTimeout(() => {
    r({
      didTimeout: !1,
      timeRemaining() {
        return Math.max(0, o - (e() - n));
      }
    });
  }, 1);
}
function D(r) {
  "cancelIdleCallback" in window && window.cancelIdleCallback(r), window.clearTimeout(r);
}
export {
  v as a,
  q as b,
  P as c,
  D as d,
  A as i,
  x as r,
  H as t
};
