import { ref as w } from "vue";
function p(D) {
  const { feedRef: r, trackingObjects: u, autoHideDelay: v = 500, onUpdate: e } = D, c = w(!1), f = w("");
  let t = null;
  const k = (a, n) => a.getFullYear() === n.getFullYear() && a.getMonth() === n.getMonth() && a.getDate() === n.getDate(), d = () => {
    var g, m;
    if (!r.value || !u.value) {
      e == null || e();
      return;
    }
    const n = r.value.getBoundingClientRect().top;
    let i = null;
    for (const o of u.value) {
      const l = o.getBoundingClientRect();
      if (l.top <= n + 1 && l.bottom > n) {
        i = o;
        break;
      }
    }
    if (!i) {
      for (const o of u.value)
        if (o.getBoundingClientRect().top >= n) {
          i = o;
          break;
        }
    }
    if (!i) {
      e == null || e();
      return;
    }
    const s = ((g = i.dataset) == null ? void 0 : g.timestamp) ?? ((m = i.getAttribute) == null ? void 0 : m.call(i, "data-timestamp"));
    if (s != null && s !== "") {
      const o = Number(s);
      if (Number.isFinite(o)) {
        const l = new Date(o * 1e3);
        f.value = k(l, /* @__PURE__ */ new Date()) ? "Сегодня" : l.toLocaleDateString("ru-RU");
      }
    }
    e == null || e();
  };
  return {
    showStickyDate: c,
    stickyDateText: f,
    updateStickyDate: d,
    show: () => {
      d(), c.value = !0, t && window.clearTimeout(t), t = window.setTimeout(() => {
        c.value = !1;
      }, v);
    },
    hide: () => {
      t && (window.clearTimeout(t), t = null), c.value = !1;
    },
    cleanup: () => {
      t && window.clearTimeout(t);
    }
  };
}
export {
  p as useStickyDate
};
