/* COPYRIGHT Esri - https://js.arcgis.com/5.2/LICENSE.txt */
import f from "@arcgis/core/core/Accessor.js";
import { property as l, subclass as b } from "@arcgis/core/core/accessorSupport/decorators.js";
import d from "@arcgis/core/core/Error.js";
import { a as p, r as h } from "./maybe.js";
import { log as m } from "@arcgis/toolkit/log";
var w = Object.defineProperty, _ = Object.getOwnPropertyDescriptor, s = (t, r, e, o) => {
  for (var n = o > 1 ? void 0 : o ? _(r, e) : r, a = t.length - 1, u; a >= 0; a--)
    (u = t[a]) && (n = (o ? u(r, e, n) : u(n)) || n);
  return o && n && w(r, e, n), n;
};
function j(t, r, e) {
  if (t && typeof t == "object" && !Array.isArray(t)) {
    const n = "details" in t && t.details && typeof t.details == "object" && !Array.isArray(t.details) ? { detail: t.details } : void 0, a = e ? `${e} - ${String(t)}` : String(t);
    m("error", r, a, n);
  }
}
function v(t = "Aborted") {
  return new d("AbortError", t);
}
function H(t, r = "Aborted") {
  if (g(t))
    throw v(r);
}
function c(t) {
  return t instanceof AbortSignal ? t : t?.signal ?? void 0;
}
function g(t) {
  return c(t)?.aborted ?? !1;
}
function y(t) {
  return t?.name === "AbortError";
}
function A(t) {
  return { ok: !0, value: t };
}
function C(t) {
  return { ok: !1, error: t };
}
function E(t) {
  return t?.ok ? t.value : null;
}
function k(t) {
  return t != null && !t.ok ? t.error : null;
}
function I(t, r) {
  const e = c(t);
  if (e == null)
    return;
  if (e.aborted) {
    r();
    return;
  }
  const o = () => r();
  return e.addEventListener("abort", o, { once: !0 }), { remove: () => e.removeEventListener("abort", o) };
}
function P(t, r) {
  return new i(t, r);
}
let i = class extends f {
  constructor(t, r) {
    super({}), this._abortHandle = null, this.abort = () => {
      this._abortController = p(this._abortController);
    }, this.remove = this.abort, this._abortController = new AbortController();
    const { signal: e } = this._abortController;
    this.promise = t(e), this.promise.then(
      (o) => {
        this._result = A(o), this._cleanup();
      },
      (o) => {
        this._result = C(o), this._cleanup();
      }
    ), this._abortHandle = I(r, this.abort);
  }
  get value() {
    return E(this._result);
  }
  get error() {
    return k(this._result);
  }
  get finished() {
    return this._result != null;
  }
  destroy() {
    this.abort();
  }
  _cleanup() {
    this._abortHandle = h(this._abortHandle), this._abortController = null;
  }
  [Symbol.dispose]() {
    this._cleanup();
  }
};
s([
  l()
], i.prototype, "value", 1);
s([
  l()
], i.prototype, "error", 1);
s([
  l()
], i.prototype, "finished", 1);
s([
  l()
], i.prototype, "promise", 2);
s([
  l()
], i.prototype, "_result", 2);
i = s([
  b("esri.components.asyncUtils.ReactiveTask")
], i);
function q(t) {
  return t.catch((r) => {
    if (!y(r))
      throw r;
  });
}
function x(t, r = {}) {
  if ("requestIdleCallback" in window)
    return window.requestIdleCallback(t, r);
  const e = r.timeout != null ? r.timeout : 50, o = typeof performance < "u" && performance.now ? performance.now.bind(performance) : Date.now, n = o();
  return window.setTimeout(() => {
    t({
      didTimeout: !1,
      timeRemaining() {
        return Math.max(0, e - (o() - n));
      }
    });
  }, 1);
}
function L(t) {
  "cancelIdleCallback" in window && window.cancelIdleCallback(t), window.clearTimeout(t);
}
export {
  g as a,
  q as b,
  P as c,
  L as d,
  y as i,
  j as l,
  x as r,
  H as t
};
