import { defineComponent as D, ref as x, createVNode as m, watch as w, onBeforeMount as de, onActivated as ce, onMounted as te, onUnmounted as ae, computed as he, onUpdated as ge, openBlock as ve, createBlock as ye, unref as me, nextTick as Se } from "vue";
import pe from "./ChatMessage-e66c5d3e.js";
import { watchDebounced as ze } from "./composables-5f691338.js";
import "./_plugin-vue_export-helper-6a641814.js";
function J(n, e) {
  var t = Object.keys(n);
  if (Object.getOwnPropertySymbols) {
    var a = Object.getOwnPropertySymbols(n);
    e && (a = a.filter(function(r) {
      return Object.getOwnPropertyDescriptor(n, r).enumerable;
    })), t.push.apply(t, a);
  }
  return t;
}
function M(n) {
  for (var e = 1; e < arguments.length; e++) {
    var t = arguments[e] != null ? arguments[e] : {};
    e % 2 ? J(Object(t), !0).forEach(function(a) {
      be(n, a, t[a]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(n, Object.getOwnPropertyDescriptors(t)) : J(Object(t)).forEach(function(a) {
      Object.defineProperty(n, a, Object.getOwnPropertyDescriptor(t, a));
    });
  }
  return n;
}
function Te(n, e) {
  if (!(n instanceof e))
    throw new TypeError("Cannot call a class as a function");
}
function Q(n, e) {
  for (var t = 0; t < e.length; t++) {
    var a = e[t];
    a.enumerable = a.enumerable || !1, a.configurable = !0, "value" in a && (a.writable = !0), Object.defineProperty(n, a.key, a);
  }
}
function Oe(n, e, t) {
  return e && Q(n.prototype, e), t && Q(n, t), n;
}
function be(n, e, t) {
  return e in n ? Object.defineProperty(n, e, {
    value: t,
    enumerable: !0,
    configurable: !0,
    writable: !0
  }) : n[e] = t, n;
}
function Ie(n) {
  return xe(n) || Ce(n) || Re(n) || Ee();
}
function xe(n) {
  if (Array.isArray(n))
    return K(n);
}
function Ce(n) {
  if (typeof Symbol < "u" && n[Symbol.iterator] != null || n["@@iterator"] != null)
    return Array.from(n);
}
function Re(n, e) {
  if (n) {
    if (typeof n == "string")
      return K(n, e);
    var t = Object.prototype.toString.call(n).slice(8, -1);
    if (t === "Object" && n.constructor && (t = n.constructor.name), t === "Map" || t === "Set")
      return Array.from(n);
    if (t === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))
      return K(n, e);
  }
}
function K(n, e) {
  (e == null || e > n.length) && (e = n.length);
  for (var t = 0, a = new Array(e); t < e; t++)
    a[t] = n[t];
  return a;
}
function Ee() {
  throw new TypeError(`Invalid attempt to spread non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.`);
}
var b = {
  FRONT: "FRONT",
  // scroll up or left
  BEHIND: "BEHIND"
  // scroll down or right
}, z = {
  INIT: "INIT",
  FIXED: "FIXED",
  DYNAMIC: "DYNAMIC"
}, Z = 2, ke = /* @__PURE__ */ function() {
  function n(e, t) {
    Te(this, n), this.init(e, t);
  }
  return Oe(n, [{
    key: "init",
    value: function(t, a) {
      this.param = t, this.callUpdate = a, this.sizes = /* @__PURE__ */ new Map(), this.firstRangeTotalSize = 0, this.firstRangeAverageSize = 0, this.lastCalcIndex = 0, this.fixedSizeValue = 0, this.calcType = z.INIT, this.offset = 0, this.direction = "", this.range = /* @__PURE__ */ Object.create(null), t && this.checkRange(0, t.keeps - 1);
    }
  }, {
    key: "destroy",
    value: function() {
      this.init(null, null);
    }
    // return current render range
  }, {
    key: "getRange",
    value: function() {
      var t = /* @__PURE__ */ Object.create(null);
      return t.start = this.range.start, t.end = this.range.end, t.padFront = this.range.padFront, t.padBehind = this.range.padBehind, t;
    }
  }, {
    key: "isBehind",
    value: function() {
      return this.direction === b.BEHIND;
    }
  }, {
    key: "isFront",
    value: function() {
      return this.direction === b.FRONT;
    }
    // return start index offset
  }, {
    key: "getOffset",
    value: function(t) {
      return (t < 1 ? 0 : this.getIndexOffset(t)) + this.param.slotHeaderSize;
    }
  }, {
    key: "updateParam",
    value: function(t, a) {
      var r = this;
      this.param && t in this.param && (t === "uniqueIds" && this.sizes.forEach(function(f, u) {
        a.includes(u) || r.sizes.delete(u);
      }), this.param[t] = a);
    }
    // save each size map by id
  }, {
    key: "saveSize",
    value: function(t, a) {
      this.sizes.set(t, a), this.calcType === z.INIT ? (this.fixedSizeValue = a, this.calcType = z.FIXED) : this.calcType === z.FIXED && this.fixedSizeValue !== a && (this.calcType = z.DYNAMIC, delete this.fixedSizeValue), this.calcType !== z.FIXED && typeof this.firstRangeTotalSize < "u" && (this.sizes.size < Math.min(this.param.keeps, this.param.uniqueIds.length) ? (this.firstRangeTotalSize = Ie(this.sizes.values()).reduce(function(r, f) {
        return r + f;
      }, 0), this.firstRangeAverageSize = Math.round(this.firstRangeTotalSize / this.sizes.size)) : delete this.firstRangeTotalSize);
    }
    // in some special situation (e.g. length change) we need to update in a row
    // try goiong to render next range by a leading buffer according to current direction
  }, {
    key: "handleDataSourcesChange",
    value: function() {
      var t = this.range.start;
      this.isFront() ? t = t - Z : this.isBehind() && (t = t + Z), t = Math.max(t, 0), this.updateRange(this.range.start, this.getEndByStart(t));
    }
    // when slot size change, we also need force update
  }, {
    key: "handleSlotSizeChange",
    value: function() {
      this.handleDataSourcesChange();
    }
    // calculating range on scroll
  }, {
    key: "handleScroll",
    value: function(t) {
      this.direction = t < this.offset ? b.FRONT : b.BEHIND, this.offset = t, this.param && (this.direction === b.FRONT ? this.handleFront() : this.direction === b.BEHIND && this.handleBehind());
    }
    // ----------- public method end -----------
  }, {
    key: "handleFront",
    value: function() {
      var t = this.getScrollOvers();
      if (!(t > this.range.start)) {
        var a = Math.max(t - this.param.buffer, 0);
        this.checkRange(a, this.getEndByStart(a));
      }
    }
  }, {
    key: "handleBehind",
    value: function() {
      var t = this.getScrollOvers();
      t < this.range.start + this.param.buffer || this.checkRange(t, this.getEndByStart(t));
    }
    // return the pass overs according to current scroll offset
  }, {
    key: "getScrollOvers",
    value: function() {
      var t = this.offset - this.param.slotHeaderSize;
      if (t <= 0)
        return 0;
      if (this.isFixedType())
        return Math.floor(t / this.fixedSizeValue);
      for (var a = 0, r = 0, f = 0, u = this.param.uniqueIds.length; a <= u; ) {
        if (r = a + Math.floor((u - a) / 2), f = this.getIndexOffset(r), f === t)
          return r;
        f < t ? a = r + 1 : f > t && (u = r - 1);
      }
      return a > 0 ? --a : 0;
    }
    // return a scroll offset from given index, can efficiency be improved more here?
    // although the call frequency is very high, its only a superposition of numbers
  }, {
    key: "getIndexOffset",
    value: function(t) {
      if (!t)
        return 0;
      for (var a = 0, r = 0, f = 0; f < t; f++)
        r = this.sizes.get(this.param.uniqueIds[f]), a = a + (typeof r == "number" ? r : this.getEstimateSize());
      return this.lastCalcIndex = Math.max(this.lastCalcIndex, t - 1), this.lastCalcIndex = Math.min(this.lastCalcIndex, this.getLastIndex()), a;
    }
    // is fixed size type
  }, {
    key: "isFixedType",
    value: function() {
      return this.calcType === z.FIXED;
    }
    // return the real last index
  }, {
    key: "getLastIndex",
    value: function() {
      return this.param.uniqueIds.length - 1;
    }
    // in some conditions range is broke, we need correct it
    // and then decide whether need update to next range
  }, {
    key: "checkRange",
    value: function(t, a) {
      var r = this.param.keeps, f = this.param.uniqueIds.length;
      f <= r ? (t = 0, a = this.getLastIndex()) : a - t < r - 1 && (t = a - r + 1), this.range.start !== t && this.updateRange(t, a);
    }
    // setting to a new range and rerender
  }, {
    key: "updateRange",
    value: function(t, a) {
      this.range.start = t, this.range.end = a, this.range.padFront = this.getPadFront(), this.range.padBehind = this.getPadBehind(), this.callUpdate(this.getRange());
    }
    // return end base on start
  }, {
    key: "getEndByStart",
    value: function(t) {
      var a = t + this.param.keeps - 1, r = Math.min(a, this.getLastIndex());
      return r;
    }
    // return total front offset
  }, {
    key: "getPadFront",
    value: function() {
      return this.isFixedType() ? this.fixedSizeValue * this.range.start : this.getIndexOffset(this.range.start);
    }
    // return total behind offset
  }, {
    key: "getPadBehind",
    value: function() {
      var t = this.range.end, a = this.getLastIndex();
      return this.isFixedType() ? (a - t) * this.fixedSizeValue : this.lastCalcIndex === a ? this.getIndexOffset(a) - this.getIndexOffset(t) : (a - t) * this.getEstimateSize();
    }
    // get the item estimate size
  }, {
    key: "getEstimateSize",
    value: function() {
      return this.isFixedType() ? this.fixedSizeValue : this.firstRangeAverageSize || this.param.estimateSize;
    }
  }]), n;
}(), Fe = {
  dataKey: {
    type: [String, Function],
    required: !0
  },
  dataSources: {
    type: Array,
    required: !0,
    default: function() {
      return [];
    }
  },
  dataComponent: {
    type: [Object, Function],
    required: !0
  },
  keeps: {
    type: Number,
    default: 30
  },
  extraProps: {
    type: Object
  },
  estimateSize: {
    type: Number,
    default: 50
  },
  direction: {
    type: String,
    default: "vertical"
    // the other value is horizontal
  },
  start: {
    type: Number,
    default: 0
  },
  offset: {
    type: Number,
    default: 0
  },
  topThreshold: {
    type: Number,
    default: 0
  },
  bottomThreshold: {
    type: Number,
    default: 0
  },
  pageMode: {
    type: Boolean,
    default: !1
  },
  rootTag: {
    type: String,
    default: "div"
  },
  wrapTag: {
    type: String,
    default: "div"
  },
  wrapClass: {
    type: String,
    default: "wrap"
  },
  wrapStyle: {
    type: Object
  },
  itemTag: {
    type: String,
    default: "div"
  },
  itemClass: {
    type: String,
    default: ""
  },
  itemClassAdd: {
    type: Function
  },
  itemStyle: {
    type: Object
  },
  headerTag: {
    type: String,
    default: "div"
  },
  headerClass: {
    type: String,
    default: ""
  },
  headerStyle: {
    type: Object
  },
  footerTag: {
    type: String,
    default: "div"
  },
  footerClass: {
    type: String,
    default: ""
  },
  footerStyle: {
    type: Object
  },
  itemScopedSlots: {
    type: Object
  }
}, Pe = {
  index: {
    type: Number
  },
  event: {
    type: String
  },
  tag: {
    type: String
  },
  horizontal: {
    type: Boolean
  },
  source: {
    type: Object
  },
  component: {
    type: [Object, Function]
  },
  uniqueKey: {
    type: [String, Number]
  },
  extraProps: {
    type: Object
  },
  scopedSlots: {
    type: Object
  }
}, Be = {
  event: {
    type: String
  },
  uniqueKey: {
    type: String
  },
  tag: {
    type: String
  },
  horizontal: {
    type: Boolean
  }
}, ne = function(e, t, a) {
  var r = null, f = he(function() {
    return e.horizontal ? "offsetWidth" : "offsetHeight";
  }), u = function() {
    return t.value ? t.value[f.value] : 0;
  }, c = function() {
    var d = e.event, S = e.uniqueKey, s = e.hasInitial;
    a(d, S, u(), s);
  };
  te(function() {
    typeof ResizeObserver < "u" && (r = new ResizeObserver(function() {
      c();
    }), t.value && r.observe(t.value));
  }), ge(function() {
    c();
  }), ae(function() {
    r && (r.disconnect(), r = null);
  });
}, we = D({
  name: "VirtualListItem",
  props: Pe,
  emits: ["itemResize"],
  setup: function(e, t) {
    var a = t.emit, r = x(null);
    return ne(e, r, a), function() {
      var f = e.tag, u = e.component, c = e.extraProps, v = c === void 0 ? {} : c, d = e.index, S = e.source, s = e.scopedSlots, _ = s === void 0 ? {} : s, C = e.uniqueKey, R = M(M({}, v), {}, {
        source: S,
        index: d
      });
      return m(f, {
        key: C,
        ref: r
      }, {
        default: function() {
          return [m(u, M(M({}, R), {}, {
            scopedSlots: _
          }), null)];
        }
      });
    };
  }
}), ee = D({
  name: "VirtualListSlot",
  props: Be,
  emits: ["slotResize"],
  setup: function(e, t) {
    var a = t.slots, r = t.emit, f = x(null);
    return ne(e, f, r), function() {
      var u, c = e.tag, v = e.uniqueKey;
      return m(c, {
        ref: f,
        key: v
      }, {
        default: function() {
          return [(u = a.default) === null || u === void 0 ? void 0 : u.call(a)];
        }
      });
    };
  }
}), k;
(function(n) {
  n.ITEM = "itemResize", n.SLOT = "slotResize";
})(k || (k = {}));
var I;
(function(n) {
  n.HEADER = "thead", n.FOOTER = "tfoot";
})(I || (I = {}));
var Me = D({
  name: "VirtualList",
  props: Fe,
  setup: function(e, t) {
    var a = t.emit, r = t.slots, f = t.expose, u = e.direction === "horizontal", c = u ? "scrollLeft" : "scrollTop", v = x(null), d = x(), S = x(null), s;
    w(function() {
      return e.dataSources.length;
    }, function() {
      s.updateParam("uniqueIds", U()), s.handleDataSourcesChange();
    }), w(function() {
      return e.keeps;
    }, function(o) {
      s.updateParam("keeps", o), s.handleSlotSizeChange();
    }), w(function() {
      return e.start;
    }, function(o) {
      A(o);
    }), w(function() {
      return e.offset;
    }, function(o) {
      return T(o);
    });
    var _ = function(i) {
      return s.sizes.get(i);
    }, C = function() {
      return e.pageMode ? document.documentElement[c] || document.body[c] : d.value ? Math.ceil(d.value[c]) : 0;
    }, R = function() {
      var i = u ? "clientWidth" : "clientHeight";
      return e.pageMode ? document.documentElement[i] || document.body[i] : d.value ? Math.ceil(d.value[i]) : 0;
    }, F = function() {
      var i = u ? "scrollWidth" : "scrollHeight";
      return e.pageMode ? document.documentElement[i] || document.body[i] : d.value ? Math.ceil(d.value[i]) : 0;
    }, ie = function(i, l, h, g) {
      a("scroll", g, s.getRange()), s.isFront() && e.dataSources.length && i - e.topThreshold <= 0 ? a("totop") : s.isBehind() && i + l + e.bottomThreshold >= h && a("tobottom");
    }, N = function(i) {
      var l = C(), h = R(), g = F();
      l < 0 || l + h > g + 1 || !g || (s.handleScroll(l), ie(l, h, g, i));
    }, U = function() {
      var i = e.dataKey, l = e.dataSources, h = l === void 0 ? [] : l;
      return h.map(function(g) {
        return typeof i == "function" ? i(g) : g[i];
      });
    }, re = function(i) {
      v.value = i;
    }, se = function() {
      s = new ke({
        slotHeaderSize: 0,
        slotFooterSize: 0,
        keeps: e.keeps,
        estimateSize: e.estimateSize,
        buffer: Math.round(e.keeps / 3),
        // recommend for a third of keeps
        uniqueIds: U()
      }, re), v.value = s.getRange();
    }, A = function(i) {
      if (i >= e.dataSources.length - 1)
        Y();
      else {
        var l = s.getOffset(i);
        T(l);
      }
    }, T = function(i) {
      e.pageMode ? (document.body[c] = i, document.documentElement[c] = i) : d.value && (d.value[c] = i);
    }, oe = function() {
      for (var i = [], l = v.value, h = l.start, g = l.end, j = e.dataSources, O = e.dataKey, q = e.itemClass, L = e.itemTag, H = e.itemStyle, V = e.extraProps, P = e.dataComponent, B = e.itemScopedSlots, y = h; y <= g; y++) {
        var p = j[y];
        if (p) {
          var E = typeof O == "function" ? O(p) : p[O];
          typeof E == "string" || typeof E == "number" ? i.push(m(we, {
            index: y,
            tag: L,
            event: k.ITEM,
            horizontal: u,
            uniqueKey: E,
            source: p,
            extraProps: V,
            component: P,
            scopedSlots: B,
            style: H,
            class: "".concat(q).concat(e.itemClassAdd ? " " + e.itemClassAdd(y) : ""),
            onItemResize: le
          }, null)) : console.warn("Cannot get the data-key '".concat(O, "' from data-sources."));
        } else
          console.warn("Cannot get the index '".concat(y, "' from data-sources."));
      }
      return i;
    }, le = function(i, l) {
      s.saveSize(i, l), a("resized", i, l);
    }, X = function(i, l, h) {
      i === I.HEADER ? s.updateParam("slotHeaderSize", l) : i === I.FOOTER && s.updateParam("slotFooterSize", l), h && s.handleSlotSizeChange();
    }, Y = function o() {
      if (S.value) {
        var i = S.value[u ? "offsetLeft" : "offsetTop"];
        T(i), setTimeout(function() {
          C() + R() < F() && o();
        }, 3);
      }
    }, ue = function() {
      if (d.value) {
        var i = d.value.getBoundingClientRect(), l = d.value.ownerDocument.defaultView, h = u ? i.left + l.pageXOffset : i.top + l.pageYOffset;
        s.updateParam("slotHeaderSize", h);
      }
    }, fe = function() {
      return s.sizes.size;
    };
    return de(function() {
      se();
    }), ce(function() {
      T(s.offset);
    }), te(function() {
      e.start ? A(e.start) : e.offset && T(e.offset), e.pageMode && (ue(), document.addEventListener("scroll", N, {
        passive: !1
      }));
    }), ae(function() {
      s.destroy(), e.pageMode && document.removeEventListener("scroll", N);
    }), f({
      scrollToBottom: Y,
      getSizes: fe,
      getSize: _,
      getOffset: C,
      getScrollSize: F,
      getClientSize: R,
      scrollToOffset: T,
      scrollToIndex: A
    }), function() {
      var o = e.pageMode, i = e.rootTag, l = e.wrapTag, h = e.wrapClass, g = e.wrapStyle, j = e.headerTag, O = e.headerClass, q = e.headerStyle, L = e.footerTag, H = e.footerClass, V = e.footerStyle, P = v.value, B = P.padFront, y = P.padBehind, p = {
        padding: u ? "0px ".concat(y, "px 0px ").concat(B, "px") : "".concat(B, "px 0px ").concat(y, "px")
      }, E = g ? Object.assign({}, g, p) : p, $ = r.header, W = r.footer;
      return m(i, {
        ref: d,
        onScroll: !o && N
      }, {
        default: function() {
          return [$ && m(ee, {
            class: O,
            style: q,
            tag: j,
            event: k.SLOT,
            uniqueKey: I.HEADER,
            onSlotResize: X
          }, {
            default: function() {
              return [$()];
            }
          }), m(l, {
            class: h,
            style: E
          }, {
            default: function() {
              return [oe()];
            }
          }), W && m(ee, {
            class: H,
            style: V,
            tag: L,
            event: k.SLOT,
            uniqueKey: I.FOOTER,
            onSlotResize: X
          }, {
            default: function() {
              return [W()];
            }
          }), m("div", {
            ref: S,
            style: {
              width: u ? "0px" : "100%",
              height: u ? "100%" : "0px"
            }
          }, null)];
        }
      });
    };
  }
});
const qe = /* @__PURE__ */ D({
  __name: "ChatMessages",
  props: {
    messages: null
  },
  setup(n) {
    const e = n, t = x();
    return ze(() => e.messages, () => {
      Se(() => {
        t.value.scrollToBottom();
      });
    }, { deep: !0, debounce: 300 }), (a, r) => (ve(), ye(me(Me), {
      class: "flex flex-col bg-opacity-80 p-4 gap-2 overflow-y-scroll scroll-smooth flex-auto",
      ref_key: "list",
      ref: t,
      "data-key": "timestamp",
      "data-sources": n.messages,
      "data-component": pe
    }, null, 8, ["data-sources"]));
  }
});
export {
  qe as default
};
//# sourceMappingURL=ChatMessages-d89c2789.js.map
