var P = Object.prototype.hasOwnProperty;
function N(e, t) {
  var o, n;
  if (e === t) return !0;
  if (e && t && (o = e.constructor) === t.constructor) {
    if (o === Date) return e.getTime() === t.getTime();
    if (o === RegExp) return e.toString() === t.toString();
    if (o === Array) {
      if ((n = e.length) === t.length)
        for (; n-- && N(e[n], t[n]); ) ;
      return n === -1;
    }
    if (!o || typeof e == "object") {
      n = 0;
      for (o in e)
        if (P.call(e, o) && ++n && !P.call(t, o) || !(o in t) || !N(e[o], t[o])) return !1;
      return Object.keys(t).length === n;
    }
  }
  return e !== e && t !== t;
}
let K = Symbol("clean"), T = [], I = 0;
const $ = 4;
let z = (e) => {
  let t = [], o = {
    get() {
      return o.lc || o.listen(() => {
      })(), o.value;
    },
    lc: 0,
    listen(n) {
      return o.lc = t.push(n), () => {
        for (let g = I + $; g < T.length; )
          T[g] === n ? T.splice(g, $) : g += $;
        let l = t.indexOf(n);
        ~l && (t.splice(l, 1), --o.lc || o.off());
      };
    },
    notify(n, l) {
      let g = !T.length;
      for (let b of t)
        T.push(b, o.value, n, l);
      if (g) {
        for (I = 0; I < T.length; I += $)
          T[I](
            T[I + 1],
            T[I + 2],
            T[I + 3]
          );
        T.length = 0;
      }
    },
    /* It will be called on last listener unsubscribing.
       We will redefine it in onMount and onStop. */
    off() {
    },
    set(n) {
      let l = o.value;
      l !== n && (o.value = n, o.notify(l));
    },
    subscribe(n) {
      let l = o.listen(n);
      return n(o.value), l;
    },
    value: e
  };
  return process.env.NODE_ENV !== "production" && (o[K] = () => {
    t = [], o.lc = 0, o.off();
  }), o;
};
const Ae = (e) => {
  const t = z(null);
  return e.observe(({ changes: o }) => {
    const n = t.get();
    if (n) {
      (o.deleted || []).some((g) => g.id === n) && t.set(null);
      const l = (o.updated || []).find(({ oldValue: g }) => g.id === n);
      l && t.set(l.newValue.id);
    }
  }), {
    get current() {
      return t.get();
    },
    subscribe: t.subscribe.bind(t),
    set: t.set.bind(t)
  };
};
var Q = /* @__PURE__ */ ((e) => (e.EDIT = "EDIT", e.SELECT = "SELECT", e.NONE = "NONE", e))(Q || {});
const R = { selected: [] }, be = (e, t, o) => {
  const n = z(R);
  let l = t;
  const g = () => {
    N(n.get(), R) || n.set(R);
  }, b = () => {
    var u;
    return ((u = n.get().selected) == null ? void 0 : u.length) === 0;
  }, m = (u) => {
    if (b())
      return !1;
    const f = typeof u == "string" ? u : u.id;
    return n.get().selected.some((c) => c.id === f);
  }, E = (u, f) => {
    let c;
    if (Array.isArray(u)) {
      if (c = u.map((r) => e.getAnnotation(r)).filter(Boolean), c.length < u.length) {
        console.warn("Invalid selection: " + u.filter((r) => !c.some((s) => s.id === r)));
        return;
      }
    } else {
      const r = e.getAnnotation(u);
      if (!r) {
        console.warn("Invalid selection: " + u);
        return;
      }
      c = [r];
    }
    const h = c.reduce((r, s) => {
      const y = O(s);
      return y === "EDIT" ? [...r, { id: s.id, editable: !0 }] : y === "SELECT" ? [...r, { id: s.id }] : r;
    }, []);
    n.set({ selected: h, event: f });
  }, v = (u, f) => {
    const c = Array.isArray(u) ? u : [u], h = c.map((r) => e.getAnnotation(r)).filter((r) => !!r);
    n.set({
      selected: h.map((r) => {
        const s = f === void 0 ? O(r) === "EDIT" : f;
        return { id: r.id, editable: s };
      })
    }), h.length !== c.length && console.warn("Invalid selection", u);
  }, C = (u) => {
    if (b())
      return !1;
    const { selected: f } = n.get();
    f.some(({ id: c }) => u.includes(c)) && n.set({ selected: f.filter(({ id: c }) => !u.includes(c)) });
  }, A = (u) => {
    l = u, v(n.get().selected.map(({ id: f }) => f));
  }, O = (u) => X(u, l, o);
  return e.observe(
    ({ changes: u }) => C((u.deleted || []).map((f) => f.id))
  ), {
    get event() {
      const u = n.get();
      return u ? u.event : null;
    },
    get selected() {
      const u = n.get();
      return u ? [...u.selected] : null;
    },
    get userSelectAction() {
      return l;
    },
    clear: g,
    evalSelectAction: O,
    isEmpty: b,
    isSelected: m,
    setSelected: v,
    setUserSelectAction: A,
    subscribe: n.subscribe.bind(n),
    userSelect: E
  };
}, X = (e, t, o) => {
  const n = o ? o.serialize(e) : e;
  return typeof t == "function" ? t(n) : t || "EDIT";
}, U = [];
for (let e = 0; e < 256; ++e)
  U.push((e + 256).toString(16).slice(1));
function Z(e, t = 0) {
  return (U[e[t + 0]] + U[e[t + 1]] + U[e[t + 2]] + U[e[t + 3]] + "-" + U[e[t + 4]] + U[e[t + 5]] + "-" + U[e[t + 6]] + U[e[t + 7]] + "-" + U[e[t + 8]] + U[e[t + 9]] + "-" + U[e[t + 10]] + U[e[t + 11]] + U[e[t + 12]] + U[e[t + 13]] + U[e[t + 14]] + U[e[t + 15]]).toLowerCase();
}
let M;
const ee = new Uint8Array(16);
function te() {
  if (!M) {
    if (typeof crypto > "u" || !crypto.getRandomValues)
      throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
    M = crypto.getRandomValues.bind(crypto);
  }
  return M(ee);
}
const ne = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), G = { randomUUID: ne };
function oe(e, t, o) {
  var n;
  e = e || {};
  const l = e.random ?? ((n = e.rng) == null ? void 0 : n.call(e)) ?? te();
  if (l.length < 16)
    throw new Error("Random bytes length must be >= 16");
  return l[6] = l[6] & 15 | 64, l[8] = l[8] & 63 | 128, Z(l);
}
function de(e, t, o) {
  return G.randomUUID && !e ? G.randomUUID() : oe(e);
}
const j = (e) => {
  const t = (o) => {
    const n = { ...o };
    return o.created && typeof o.created == "string" && (n.created = new Date(o.created)), o.updated && typeof o.updated == "string" && (n.updated = new Date(o.updated)), n;
  };
  return {
    ...e,
    bodies: (e.bodies || []).map(t),
    target: t(e.target)
  };
}, ie = (e, t) => {
  const o = new Set(e.bodies.map((n) => n.id));
  return t.bodies.filter((n) => !o.has(n.id));
}, ae = (e, t) => {
  const o = new Set(t.bodies.map((n) => n.id));
  return e.bodies.filter((n) => !o.has(n.id));
}, se = (e, t) => t.bodies.map((o) => {
  const n = e.bodies.find((l) => l.id === o.id);
  return { newBody: o, oldBody: n && !N(n, o) ? n : void 0 };
}).filter(({ oldBody: o }) => o).map(({ oldBody: o, newBody: n }) => ({ oldBody: o, newBody: n })), re = (e, t) => !N(e.target, t.target), W = (e, t) => {
  const o = ie(e, t), n = ae(e, t), l = se(e, t);
  return {
    oldValue: e,
    newValue: t,
    bodiesCreated: o.length > 0 ? o : void 0,
    bodiesDeleted: n.length > 0 ? n : void 0,
    bodiesUpdated: l.length > 0 ? l : void 0,
    targetUpdated: re(e, t) ? { oldTarget: e.target, newTarget: t.target } : void 0
  };
};
var S = /* @__PURE__ */ ((e) => (e.LOCAL = "LOCAL", e.REMOTE = "REMOTE", e.SILENT = "SILENT", e))(S || {});
const le = (e, t) => {
  var o, n;
  const { changes: l, origin: g } = t;
  if (!(e.options.origin ? e.options.origin === g : g !== "SILENT"))
    return !1;
  if (e.options.ignore) {
    const { ignore: b } = e.options, m = (E) => E && E.length > 0;
    if (!(m(l.created) || m(l.deleted))) {
      const E = (o = l.updated) == null ? void 0 : o.some((C) => m(C.bodiesCreated) || m(C.bodiesDeleted) || m(C.bodiesUpdated)), v = (n = l.updated) == null ? void 0 : n.some((C) => C.targetUpdated);
      if (b === "BODY_ONLY" && E && !v || b === "TARGET_ONLY" && v && !E)
        return !1;
    }
  }
  if (e.options.annotations) {
    const b = /* @__PURE__ */ new Set([
      ...(l.created || []).map((m) => m.id),
      ...(l.deleted || []).map((m) => m.id),
      ...(l.updated || []).map(({ oldValue: m }) => m.id)
    ]);
    return !!(Array.isArray(e.options.annotations) ? e.options.annotations : [e.options.annotations]).find((m) => b.has(m));
  } else
    return !0;
}, ce = (e, t) => {
  const o = new Set((e.created || []).map((A) => A.id)), n = new Set((e.updated || []).map(({ newValue: A }) => A.id)), l = new Set((t.created || []).map((A) => A.id)), g = new Set((t.deleted || []).map((A) => A.id)), b = new Set((t.updated || []).map(({ oldValue: A }) => A.id)), m = new Set((t.updated || []).filter(({ oldValue: A }) => o.has(A.id) || n.has(A.id)).map(({ oldValue: A }) => A.id)), E = [
    ...(e.created || []).filter((A) => !g.has(A.id)).map((A) => b.has(A.id) ? t.updated.find(({ oldValue: O }) => O.id === A.id).newValue : A),
    ...t.created || []
  ], v = [
    ...(e.deleted || []).filter((A) => !l.has(A.id)),
    ...(t.deleted || []).filter((A) => !o.has(A.id))
  ], C = [
    ...(e.updated || []).filter(({ newValue: A }) => !g.has(A.id)).map((A) => {
      const { oldValue: O, newValue: u } = A;
      if (b.has(u.id)) {
        const f = t.updated.find((c) => c.oldValue.id === u.id).newValue;
        return W(O, f);
      } else
        return A;
    }),
    ...(t.updated || []).filter(({ oldValue: A }) => !m.has(A.id))
  ];
  return { created: E, deleted: v, updated: C };
}, x = (e) => {
  const t = e.id === void 0 ? de() : e.id;
  return {
    ...e,
    id: t,
    bodies: e.bodies === void 0 ? [] : e.bodies.map((o) => ({
      ...o,
      annotation: t
    })),
    target: {
      ...e.target,
      annotation: t
    }
  };
}, ue = (e) => e.id !== void 0, me = () => {
  const e = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map(), o = [], n = (d, a = {}) => {
    o.push({ onChange: d, options: a });
  }, l = (d) => {
    const a = o.findIndex((i) => i.onChange == d);
    a > -1 && o.splice(a, 1);
  }, g = (d, a) => {
    const i = {
      origin: d,
      changes: {
        created: a.created || [],
        updated: a.updated || [],
        deleted: a.deleted || []
      },
      state: [...e.values()]
    };
    o.forEach((p) => {
      le(p, i) && p.onChange(i);
    });
  }, b = (d, a = S.LOCAL) => {
    if (d.id && e.get(d.id))
      throw Error(`Cannot add annotation ${d.id} - exists already`);
    {
      const i = x(d);
      e.set(i.id, i), i.bodies.forEach((p) => t.set(p.id, i.id)), g(a, { created: [i] });
    }
  }, m = (d, a) => {
    const i = x(typeof d == "string" ? a : d), p = typeof d == "string" ? d : d.id, L = p && e.get(p);
    if (L) {
      const w = W(L, i);
      return p === i.id ? e.set(p, i) : (e.delete(p), e.set(i.id, i)), L.bodies.forEach((V) => t.delete(V.id)), i.bodies.forEach((V) => t.set(V.id, i.id)), w;
    } else
      console.warn(`Cannot update annotation ${p} - does not exist`);
  }, E = (d, a = S.LOCAL, i = S.LOCAL) => {
    const p = ue(a) ? i : a, L = m(d, a);
    L && g(p, { updated: [L] });
  }, v = (d, a = S.LOCAL) => {
    e.get(d.id) ? E(d, a) : b(d, a);
  }, C = (d, a = S.LOCAL) => {
    const i = d.reduce((p, L) => {
      const w = m(L);
      return w ? [...p, w] : p;
    }, []);
    i.length > 0 && g(a, { updated: i });
  }, A = (d, a = S.LOCAL) => {
    const i = d.map(x), { toAdd: p, toUpdate: L } = i.reduce((V, B) => e.get(B.id) ? { ...V, toUpdate: [...V.toUpdate, B] } : { ...V, toAdd: [...V.toAdd, B] }, { toAdd: [], toUpdate: [] }), w = L.map((V) => m(V, a)).filter(Boolean);
    p.forEach((V) => {
      e.set(V.id, V), V.bodies.forEach((B) => t.set(B.id, V.id));
    }), g(a, { created: p, updated: w });
  }, O = (d, a = S.LOCAL) => {
    const i = e.get(d.annotation);
    if (i) {
      const p = {
        ...i,
        bodies: [...i.bodies, d]
      };
      e.set(i.id, p), t.set(d.id, p.id), g(a, { updated: [{
        oldValue: i,
        newValue: p,
        bodiesCreated: [d]
      }] });
    } else
      console.warn(`Attempt to add body to missing annotation: ${d.annotation}`);
  }, u = () => [...e.values()], f = (d = S.LOCAL) => {
    const a = [...e.values()];
    e.clear(), t.clear(), g(d, { deleted: a });
  }, c = (d, a = !0, i = S.LOCAL) => {
    const p = d.map(x);
    if (a) {
      const L = [...e.values()];
      e.clear(), t.clear(), p.forEach((w) => {
        e.set(w.id, w), w.bodies.forEach((V) => t.set(V.id, w.id));
      }), g(i, { created: p, deleted: L });
    } else {
      const L = d.reduce((w, V) => {
        const B = V.id && e.get(V.id);
        return B ? [...w, B] : w;
      }, []);
      if (L.length > 0)
        throw Error(`Bulk insert would overwrite the following annotations: ${L.map((w) => w.id).join(", ")}`);
      p.forEach((w) => {
        e.set(w.id, w), w.bodies.forEach((V) => t.set(V.id, w.id));
      }), g(i, { created: p });
    }
  }, h = (d) => {
    const a = typeof d == "string" ? d : d.id, i = e.get(a);
    if (i)
      return e.delete(a), i.bodies.forEach((p) => t.delete(p.id)), i;
    console.warn(`Attempt to delete missing annotation: ${a}`);
  }, r = (d, a = S.LOCAL) => {
    const i = h(d);
    i && g(a, { deleted: [i] });
  }, s = (d, a = S.LOCAL) => {
    const i = d.reduce((p, L) => {
      const w = h(L);
      return w ? [...p, w] : p;
    }, []);
    i.length > 0 && g(a, { deleted: i });
  }, y = (d) => {
    const a = e.get(d.annotation);
    if (a) {
      const i = a.bodies.find((p) => p.id === d.id);
      if (i) {
        t.delete(i.id);
        const p = {
          ...a,
          bodies: a.bodies.filter((L) => L.id !== d.id)
        };
        return e.set(a.id, p), {
          oldValue: a,
          newValue: p,
          bodiesDeleted: [i]
        };
      } else
        console.warn(`Attempt to delete missing body ${d.id} from annotation ${d.annotation}`);
    } else
      console.warn(`Attempt to delete body from missing annotation ${d.annotation}`);
  }, k = (d, a = S.LOCAL) => {
    const i = y(d);
    i && g(a, { updated: [i] });
  }, D = (d, a = S.LOCAL) => {
    const i = d.map((p) => y(p)).filter(Boolean);
    i.length > 0 && g(a, { updated: i });
  }, H = (d) => {
    const a = e.get(d);
    return a ? { ...a } : void 0;
  }, q = (d) => {
    const a = t.get(d);
    if (a) {
      const i = H(a).bodies.find((p) => p.id === d);
      if (i)
        return i;
      console.error(`Store integrity error: body ${d} in index, but not in annotation`);
    } else
      console.warn(`Attempt to retrieve missing body: ${d}`);
  }, Y = (d, a) => {
    if (d.annotation !== a.annotation)
      throw "Annotation integrity violation: annotation ID must be the same when updating bodies";
    const i = e.get(d.annotation);
    if (i) {
      const p = i.bodies.find((w) => w.id === d.id), L = {
        ...i,
        bodies: i.bodies.map((w) => w.id === p.id ? a : w)
      };
      return e.set(i.id, L), p.id !== a.id && (t.delete(p.id), t.set(a.id, L.id)), {
        oldValue: i,
        newValue: L,
        bodiesUpdated: [{ oldBody: p, newBody: a }]
      };
    } else
      console.warn(`Attempt to add body to missing annotation ${d.annotation}`);
  }, F = (d, a, i = S.LOCAL) => {
    const p = Y(d, a);
    p && g(i, { updated: [p] });
  }, J = (d, a = S.LOCAL) => {
    const i = d.map((p) => Y({ id: p.id, annotation: p.annotation }, p)).filter(Boolean);
    g(a, { updated: i });
  }, _ = (d) => {
    const a = e.get(d.annotation);
    if (a) {
      const i = {
        ...a,
        target: {
          ...a.target,
          ...d
        }
      };
      return e.set(a.id, i), {
        oldValue: a,
        newValue: i,
        targetUpdated: {
          oldTarget: a.target,
          newTarget: d
        }
      };
    } else
      console.warn(`Attempt to update target on missing annotation: ${d.annotation}`);
  };
  return {
    addAnnotation: b,
    addBody: O,
    all: u,
    bulkAddAnnotations: c,
    bulkDeleteAnnotations: s,
    bulkDeleteBodies: D,
    bulkUpdateAnnotations: C,
    bulkUpdateBodies: J,
    bulkUpdateTargets: (d, a = S.LOCAL) => {
      const i = d.map((p) => _(p)).filter(Boolean);
      i.length > 0 && g(a, { updated: i });
    },
    bulkUpsertAnnotations: A,
    clear: f,
    deleteAnnotation: r,
    deleteBody: k,
    getAnnotation: H,
    getBody: q,
    observe: n,
    unobserve: l,
    updateAnnotation: E,
    updateBody: F,
    updateTarget: (d, a = S.LOCAL) => {
      const i = _(d);
      i && g(a, { updated: [i] });
    },
    upsertAnnotation: v
  };
};
let pe = () => ({
  emit(e, ...t) {
    for (let o = this.events[e] || [], n = 0, l = o.length; n < l; n++)
      o[n](...t);
  },
  events: {},
  on(e, t) {
    var o;
    return ((o = this.events)[e] || (o[e] = [])).push(t), () => {
      var n;
      this.events[e] = (n = this.events[e]) == null ? void 0 : n.filter((l) => t !== l);
    };
  }
});
const fe = 250, ve = (e, t) => {
  const o = pe(), n = [];
  let l = -1, g = !1, b = 0;
  const m = (f) => {
    if (!g) {
      const { changes: c } = f, h = performance.now();
      if (h - b > fe)
        n.splice(l + 1), n.push(c), l = n.length - 1;
      else {
        const r = n.length - 1;
        n[r] = ce(n[r], c);
      }
      b = h;
    }
    g = !1;
  };
  e.observe(m, { origin: S.LOCAL });
  const E = (f) => f && f.length > 0 && e.bulkDeleteAnnotations(f), v = (f) => f && f.length > 0 && e.bulkAddAnnotations(f, !1), C = (f) => f && f.length > 0 && e.bulkUpdateAnnotations(f.map(({ oldValue: c }) => c)), A = (f) => f && f.length > 0 && e.bulkUpdateAnnotations(f.map(({ newValue: c }) => c)), O = (f) => f && f.length > 0 && e.bulkAddAnnotations(f, !1), u = (f) => f && f.length > 0 && e.bulkDeleteAnnotations(f);
  return {
    canRedo: () => n.length - 1 > l,
    canUndo: () => l > -1,
    destroy: () => e.unobserve(m),
    getHistory: () => ({ changes: [...n], pointer: l }),
    on: (f, c) => o.on(f, c),
    redo: () => {
      if (n.length - 1 > l) {
        g = !0;
        const { created: f, updated: c, deleted: h } = n[l + 1];
        v(f), A(c), u(h), o.emit("redo", n[l + 1]), l += 1;
      }
    },
    undo: () => {
      if (l > -1) {
        g = !0;
        const { created: f, updated: c, deleted: h } = n[l];
        E(f), C(c), O(h), o.emit("undo", n[l]), l -= 1;
      }
    }
  };
}, we = () => {
  const e = z([]);
  return {
    subscribe: e.subscribe.bind(e),
    set: e.set.bind(e)
  };
}, ye = (e, t, o, n) => {
  const { hover: l, selection: g, store: b, viewport: m } = e, E = /* @__PURE__ */ new Map();
  let v = [], C;
  const A = (c, h) => {
    E.has(c) ? E.get(c).push(h) : E.set(c, [h]);
  }, O = (c, h) => {
    const r = E.get(c);
    if (r) {
      const s = r.indexOf(h);
      s !== -1 && r.splice(s, 1);
    }
  }, u = (c, h, r) => {
    E.has(c) && setTimeout(() => {
      E.get(c).forEach((s) => {
        if (o) {
          const y = Array.isArray(h) ? h.map((D) => o.serialize(D)) : o.serialize(h), k = r ? r instanceof PointerEvent ? r : o.serialize(r) : void 0;
          s(y, k);
        } else
          s(h, r);
      });
    }, 1);
  };
  g.subscribe(({ selected: c }) => {
    if (!(v.length === 0 && c.length === 0)) {
      if (v.length === 0 && c.length > 0)
        v = c.map(({ id: h }) => b.getAnnotation(h));
      else if (v.length > 0 && c.length === 0)
        v.forEach((h) => {
          const r = b.getAnnotation(h.id);
          r && !N(r, h) && u("updateAnnotation", r, h);
        }), v = [];
      else {
        const h = new Set(v.map((s) => s.id)), r = new Set(c.map(({ id: s }) => s));
        v.filter((s) => !r.has(s.id)).forEach((s) => {
          const y = b.getAnnotation(s.id);
          y && !N(y, s) && u("updateAnnotation", y, s);
        }), v = [
          // Remove annotations that were deselected
          ...v.filter((s) => r.has(s.id)),
          // Add editable annotations that were selected
          ...c.filter(({ id: s }) => !h.has(s)).map(({ id: s }) => b.getAnnotation(s))
        ];
      }
      u("selectionChanged", v);
    }
  }), l.subscribe((c) => {
    !C && c ? u("mouseEnterAnnotation", b.getAnnotation(c)) : C && !c ? u("mouseLeaveAnnotation", b.getAnnotation(C)) : C && c && (u("mouseLeaveAnnotation", b.getAnnotation(C)), u("mouseEnterAnnotation", b.getAnnotation(c))), C = c;
  }), m?.subscribe((c) => u("viewportIntersect", c.map((h) => b.getAnnotation(h)))), b.observe((c) => {
    const { created: h, deleted: r } = c.changes;
    (h || []).forEach((s) => u("createAnnotation", s)), (r || []).forEach((s) => u("deleteAnnotation", s)), (c.changes.updated || []).filter((s) => [
      ...s.bodiesCreated || [],
      ...s.bodiesDeleted || [],
      ...s.bodiesUpdated || []
    ].length > 0).forEach(({ oldValue: s, newValue: y }) => {
      const k = v.find((D) => D.id === s.id) || s;
      v = v.map((D) => D.id === s.id ? y : D), u("updateAnnotation", y, k);
    });
  }, { origin: S.LOCAL }), b.observe((c) => {
    if (v) {
      const h = new Set(v.map((s) => s.id)), r = (c.changes.updated || []).filter(({ newValue: s }) => h.has(s.id)).map(({ newValue: s }) => s);
      r.length > 0 && (v = v.map((s) => r.find((k) => k.id === s.id) || s));
    }
  }, { origin: S.REMOTE });
  const f = (c) => (h) => {
    const { updated: r } = h;
    c ? (r || []).forEach((s) => u("updateAnnotation", s.oldValue, s.newValue)) : (r || []).forEach((s) => u("updateAnnotation", s.newValue, s.oldValue));
  };
  return t.on("undo", f(!0)), t.on("redo", f(!1)), { on: A, off: O, emit: u };
}, Ee = (e, t, o) => {
  const { store: n, selection: l } = e, g = (r) => {
    n.addAnnotation(j(r), S.REMOTE);
  }, b = () => l.clear(), m = () => n.clear(), E = (r) => n.getAnnotation(r), v = () => n.all(), C = () => {
    var r;
    return (((r = l.selected) == null ? void 0 : r.map((y) => y.id)) || []).map((y) => n.getAnnotation(y)).filter(Boolean);
  }, A = (r, s = !0) => fetch(r).then((y) => y.json()).then((y) => (u(y, s), y)), O = (r) => {
    if (typeof r == "string") {
      const s = n.getAnnotation(r);
      if (n.deleteAnnotation(r), s)
        return s;
    } else {
      const s = r;
      if (s)
        return n.deleteAnnotation(s), r;
    }
  }, u = (r, s = !0) => {
    n.bulkAddAnnotations(r.map(j), s, S.REMOTE);
  }, f = (r, s) => {
    r ? l.setSelected(r, s) : l.clear();
  }, c = (r) => {
    l.setUserSelectAction(r);
  }, h = (r) => {
    {
      const s = n.getAnnotation(r.id);
      return n.updateAnnotation(j(r)), s;
    }
  };
  return {
    addAnnotation: g,
    cancelSelected: b,
    canRedo: t.canRedo,
    canUndo: t.canUndo,
    clearAnnotations: m,
    getAnnotationById: E,
    getAnnotations: v,
    getHistory: t.getHistory,
    getSelected: C,
    loadAnnotations: A,
    redo: t.redo,
    removeAnnotation: O,
    setAnnotations: u,
    setSelected: f,
    setUserSelectAction: c,
    undo: t.undo,
    updateAnnotation: h
  };
}, ge = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
let he = (e = 21) => {
  let t = "", o = crypto.getRandomValues(new Uint8Array(e |= 0));
  for (; e--; )
    t += ge[o[e] & 63];
  return t;
};
const Le = (e) => e.map((t) => {
  var o;
  const { annotation: n, created: l, updated: g, ...b } = t, m = {
    ...b,
    created: l?.toISOString(),
    modified: g?.toISOString()
  };
  return (o = m.id) != null && o.startsWith("temp-") && delete m.id, m;
});
he();
export {
  Ee as $,
  be as B,
  Le as H,
  Q as K,
  me as N,
  S as O,
  Ae as R,
  ye as V,
  ve as k,
  le as u,
  we as z
};
//# sourceMappingURL=annotorious-core.es-B2-qRuDb.js.map
