var U = Object.prototype.hasOwnProperty;
function S(e, o) {
  var d, t;
  if (e === o) return !0;
  if (e && o && (d = e.constructor) === o.constructor) {
    if (d === Date) return e.getTime() === o.getTime();
    if (d === RegExp) return e.toString() === o.toString();
    if (d === Array) {
      if ((t = e.length) === o.length)
        for (; t-- && S(e[t], o[t]); ) ;
      return t === -1;
    }
    if (!d || typeof e == "object") {
      t = 0;
      for (d in e)
        if (U.call(e, d) && ++t && !U.call(o, d) || !(d in o) || !S(e[d], o[d])) return !1;
      return Object.keys(o).length === t;
    }
  }
  return e !== e && o !== o;
}
const O = [];
for (let e = 0; e < 256; ++e)
  O.push((e + 256).toString(16).slice(1));
typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
const V = (e) => {
  const o = (d) => {
    const t = { ...d };
    return d.created && typeof d.created == "string" && (t.created = new Date(d.created)), d.updated && typeof d.updated == "string" && (t.updated = new Date(d.updated)), t;
  };
  return {
    ...e,
    bodies: (e.bodies || []).map(o),
    target: o(e.target)
  };
}, T = (e, o) => {
  const d = new Set(e.bodies.map((t) => t.id));
  return o.bodies.filter((t) => !d.has(t.id));
}, D = (e, o) => {
  const d = new Set(o.bodies.map((t) => t.id));
  return e.bodies.filter((t) => !d.has(t.id));
}, L = (e, o) => o.bodies.map((d) => {
  const t = e.bodies.find((r) => r.id === d.id);
  return { newBody: d, oldBody: t && !S(t, d) ? t : void 0 };
}).filter(({ oldBody: d }) => d).map(({ oldBody: d, newBody: t }) => ({ oldBody: d, newBody: t })), R = (e, o) => !S(e.target, o.target), I = (e, o) => {
  const d = T(e, o), t = D(e, o), r = L(e, o);
  return {
    oldValue: e,
    newValue: o,
    bodiesCreated: d.length > 0 ? d : void 0,
    bodiesDeleted: t.length > 0 ? t : void 0,
    bodiesUpdated: r.length > 0 ? r : void 0,
    targetUpdated: R(e, o) ? { oldTarget: e.target, newTarget: o.target } : void 0
  };
};
var y = /* @__PURE__ */ ((e) => (e.LOCAL = "LOCAL", e.REMOTE = "REMOTE", e.SILENT = "SILENT", e))(y || {});
const N = (e, o) => {
  var d, t;
  const { changes: r, origin: b } = o;
  if (!(e.options.origin ? e.options.origin === b : b !== "SILENT"))
    return !1;
  if (e.options.ignore) {
    const { ignore: g } = e.options, c = (h) => h && h.length > 0;
    if (!(c(r.created) || c(r.deleted))) {
      const h = (d = r.updated) == null ? void 0 : d.some((A) => c(A.bodiesCreated) || c(A.bodiesDeleted) || c(A.bodiesUpdated)), p = (t = r.updated) == null ? void 0 : t.some((A) => A.targetUpdated);
      if (g === "BODY_ONLY" && h && !p || g === "TARGET_ONLY" && p && !h)
        return !1;
    }
  }
  if (e.options.annotations) {
    const g = /* @__PURE__ */ new Set([
      ...(r.created || []).map((c) => c.id),
      ...(r.deleted || []).map((c) => c.id),
      ...(r.updated || []).map(({ oldValue: c }) => c.id)
    ]);
    return !!(Array.isArray(e.options.annotations) ? e.options.annotations : [e.options.annotations]).find((c) => g.has(c));
  } else
    return !0;
}, B = (e, o) => {
  const d = new Set((e.created || []).map((l) => l.id)), t = new Set((e.updated || []).map(({ newValue: l }) => l.id)), r = new Set((o.created || []).map((l) => l.id)), b = new Set((o.deleted || []).map((l) => l.id)), g = new Set((o.updated || []).map(({ oldValue: l }) => l.id)), c = new Set((o.updated || []).filter(({ oldValue: l }) => d.has(l.id) || t.has(l.id)).map(({ oldValue: l }) => l.id)), h = [
    ...(e.created || []).filter((l) => !b.has(l.id)).map((l) => g.has(l.id) ? o.updated.find(({ oldValue: w }) => w.id === l.id).newValue : l),
    ...o.created || []
  ], p = [
    ...(e.deleted || []).filter((l) => !r.has(l.id)),
    ...(o.deleted || []).filter((l) => !d.has(l.id))
  ], A = [
    ...(e.updated || []).filter(({ newValue: l }) => !b.has(l.id)).map((l) => {
      const { oldValue: w, newValue: f } = l;
      if (g.has(f.id)) {
        const s = o.updated.find((i) => i.oldValue.id === f.id).newValue;
        return I(w, s);
      } else
        return l;
    }),
    ...(o.updated || []).filter(({ oldValue: l }) => !c.has(l.id))
  ];
  return { created: h, deleted: p, updated: A };
};
let C = () => ({
  emit(e, ...o) {
    for (let d = this.events[e] || [], t = 0, r = d.length; t < r; t++)
      d[t](...o);
  },
  events: {},
  on(e, o) {
    var d;
    return ((d = this.events)[e] || (d[e] = [])).push(o), () => {
      var t;
      this.events[e] = (t = this.events[e]) == null ? void 0 : t.filter((r) => o !== r);
    };
  }
});
const k = 250, j = (e, o) => {
  const d = C(), t = [];
  let r = -1, b = !1, g = 0;
  const c = (s) => {
    if (!b) {
      const { changes: i } = s, u = performance.now();
      if (u - g > k)
        t.splice(r + 1), t.push(i), r = t.length - 1;
      else {
        const a = t.length - 1;
        t[a] = B(t[a], i);
      }
      g = u;
    }
    b = !1;
  };
  e.observe(c, { origin: y.LOCAL });
  const h = (s) => s && s.length > 0 && e.bulkDeleteAnnotations(s), p = (s) => s && s.length > 0 && e.bulkAddAnnotations(s, !1), A = (s) => s && s.length > 0 && e.bulkUpdateAnnotations(s.map(({ oldValue: i }) => i)), l = (s) => s && s.length > 0 && e.bulkUpdateAnnotations(s.map(({ newValue: i }) => i)), w = (s) => s && s.length > 0 && e.bulkAddAnnotations(s, !1), f = (s) => s && s.length > 0 && e.bulkDeleteAnnotations(s);
  return {
    canRedo: () => t.length - 1 > r,
    canUndo: () => r > -1,
    destroy: () => e.unobserve(c),
    getHistory: () => ({ changes: [...t], pointer: r }),
    on: (s, i) => d.on(s, i),
    redo: () => {
      if (t.length - 1 > r) {
        b = !0;
        const { created: s, updated: i, deleted: u } = t[r + 1];
        p(s), l(i), f(u), d.emit("redo", t[r + 1]), r += 1;
      }
    },
    undo: () => {
      if (r > -1) {
        b = !0;
        const { created: s, updated: i, deleted: u } = t[r];
        h(s), A(i), w(u), d.emit("undo", t[r]), r -= 1;
      }
    }
  };
}, z = (e, o, d, t) => {
  const { hover: r, selection: b, store: g, viewport: c } = e, h = /* @__PURE__ */ new Map();
  let p = [], A;
  const l = (i, u) => {
    h.has(i) ? h.get(i).push(u) : h.set(i, [u]);
  }, w = (i, u) => {
    const a = h.get(i);
    if (a) {
      const n = a.indexOf(u);
      n !== -1 && a.splice(n, 1);
    }
  }, f = (i, u, a) => {
    h.has(i) && setTimeout(() => {
      h.get(i).forEach((n) => {
        if (d) {
          const m = Array.isArray(u) ? u.map((v) => d.serialize(v)) : d.serialize(u), E = a ? a instanceof PointerEvent ? a : d.serialize(a) : void 0;
          n(m, E);
        } else
          n(u, a);
      });
    }, 1);
  };
  b.subscribe(({ selected: i }) => {
    if (!(p.length === 0 && i.length === 0)) {
      if (p.length === 0 && i.length > 0)
        p = i.map(({ id: u }) => g.getAnnotation(u));
      else if (p.length > 0 && i.length === 0)
        p.forEach((u) => {
          const a = g.getAnnotation(u.id);
          a && !S(a, u) && f("updateAnnotation", a, u);
        }), p = [];
      else {
        const u = new Set(p.map((n) => n.id)), a = new Set(i.map(({ id: n }) => n));
        p.filter((n) => !a.has(n.id)).forEach((n) => {
          const m = g.getAnnotation(n.id);
          m && !S(m, n) && f("updateAnnotation", m, n);
        }), p = [
          // Remove annotations that were deselected
          ...p.filter((n) => a.has(n.id)),
          // Add editable annotations that were selected
          ...i.filter(({ id: n }) => !u.has(n)).map(({ id: n }) => g.getAnnotation(n))
        ];
      }
      f("selectionChanged", p);
    }
  }), r.subscribe((i) => {
    !A && i ? f("mouseEnterAnnotation", g.getAnnotation(i)) : A && !i ? f("mouseLeaveAnnotation", g.getAnnotation(A)) : A && i && (f("mouseLeaveAnnotation", g.getAnnotation(A)), f("mouseEnterAnnotation", g.getAnnotation(i))), A = i;
  }), c?.subscribe((i) => f("viewportIntersect", i.map((u) => g.getAnnotation(u)))), g.observe((i) => {
    const { created: u, deleted: a } = i.changes;
    (u || []).forEach((n) => f("createAnnotation", n)), (a || []).forEach((n) => f("deleteAnnotation", n)), (i.changes.updated || []).filter((n) => [
      ...n.bodiesCreated || [],
      ...n.bodiesDeleted || [],
      ...n.bodiesUpdated || []
    ].length > 0).forEach(({ oldValue: n, newValue: m }) => {
      const E = p.find((v) => v.id === n.id) || n;
      p = p.map((v) => v.id === n.id ? m : v), f("updateAnnotation", m, E);
    });
  }, { origin: y.LOCAL }), g.observe((i) => {
    if (p) {
      const u = new Set(p.map((n) => n.id)), a = (i.changes.updated || []).filter(({ newValue: n }) => u.has(n.id)).map(({ newValue: n }) => n);
      a.length > 0 && (p = p.map((n) => a.find((E) => E.id === n.id) || n));
    }
  }, { origin: y.REMOTE });
  const s = (i) => (u) => {
    const { updated: a } = u;
    i ? (a || []).forEach((n) => f("updateAnnotation", n.oldValue, n.newValue)) : (a || []).forEach((n) => f("updateAnnotation", n.newValue, n.oldValue));
  };
  return o.on("undo", s(!0)), o.on("redo", s(!1)), { on: l, off: w, emit: f };
}, H = (e, o, d) => {
  const { store: t, selection: r } = e, b = (a) => {
    t.addAnnotation(V(a), y.REMOTE);
  }, g = () => r.clear(), c = () => t.clear(), h = (a) => t.getAnnotation(a), p = () => t.all(), A = () => {
    var a;
    return (((a = r.selected) == null ? void 0 : a.map((m) => m.id)) || []).map((m) => t.getAnnotation(m)).filter(Boolean);
  }, l = (a, n = !0) => fetch(a).then((m) => m.json()).then((m) => (f(m, n), m)), w = (a) => {
    if (typeof a == "string") {
      const n = t.getAnnotation(a);
      if (t.deleteAnnotation(a), n)
        return n;
    } else {
      const n = a;
      if (n)
        return t.deleteAnnotation(n), a;
    }
  }, f = (a, n = !0) => {
    t.bulkAddAnnotations(a.map(V), n, y.REMOTE);
  }, s = (a, n) => {
    a ? r.setSelected(a, n) : r.clear();
  }, i = (a) => {
    r.clear(), r.setUserSelectAction(a);
  }, u = (a) => {
    {
      const n = t.getAnnotation(a.id);
      return t.updateAnnotation(V(a)), n;
    }
  };
  return {
    addAnnotation: b,
    cancelSelected: g,
    canRedo: o.canRedo,
    canUndo: o.canUndo,
    clearAnnotations: c,
    getAnnotationById: h,
    getAnnotations: p,
    getHistory: o.getHistory,
    getSelected: A,
    loadAnnotations: l,
    redo: o.redo,
    removeAnnotation: w,
    setAnnotations: f,
    setSelected: s,
    setUserSelectAction: i,
    undo: o.undo,
    updateAnnotation: u
  };
}, x = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
let M = (e = 21) => {
  let o = "", d = crypto.getRandomValues(new Uint8Array(e |= 0));
  for (; e--; )
    o += x[d[e] & 63];
  return o;
};
const Y = (e) => e.map((o) => {
  var d;
  const { annotation: t, created: r, updated: b, ...g } = o, c = {
    ...g,
    created: r?.toISOString(),
    modified: b?.toISOString()
  };
  return (d = c.id) != null && d.startsWith("temp-") && delete c.id, c;
});
M();
export {
  Y as H,
  j as I,
  H as V,
  N as a,
  z
};
//# sourceMappingURL=annotorious-core.es-BU-wTYuu.js.map
