import { jsx as c } from "../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { useState as I, useRef as _, useMemo as r, useCallback as g } from "../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { TimelineContext as C } from "./context.js";
/* empty css              */
function R({ children: o, showMore: t = null, timeGapLimit: a = null }) {
  const [n, u] = I([]), [l, y] = I(!1), v = _(0), m = r(() => t && n.length > t.limit ? n.length - t.limit : null, [t, n.length]), f = r(() => {
    if (!t || !n.length)
      return null;
    const e = Array.from(n.keys());
    if (t.placement === "before-last") {
      const i = e[e.length - 1];
      return i === void 0 ? e.slice(0, t.limit) : [...e.slice(0, t.limit - 1), i];
    }
    if (t.placement === "after-first") {
      const i = e[0];
      return i === void 0 ? e.slice(0, t.limit) : [i, ...e.slice(-(t.limit - 1))];
    }
    return e.slice(0, t.limit);
  }, [n, t]), d = r(() => {
    if (!t)
      return null;
    switch (t.placement) {
      case "before-last":
        return t.limit - 2;
      case "after-first":
        return 0;
      case "bottom":
      default:
        return t.limit - 1;
    }
  }, [t]), p = g((e) => {
    const i = v.current++, A = { ...e, _id: i };
    return u((s) => [...s, A]), () => {
      u((s) => s.filter((T) => T._id !== i));
    };
  }, []), x = g(() => {
    y(!l);
  }, [l]), b = r(
    () => ({
      registerTimelineEntry: p,
      entries: n,
      showAll: l,
      showMoreIndex: d,
      hiddenItems: m,
      visibleIndexes: f,
      timeGapLimit: a,
      toggleShowAll: x
    }),
    [p, n, l, d, m, f, a, x]
  );
  return /* @__PURE__ */ c(C.Provider, { value: b, children: /* @__PURE__ */ c("div", { className: "adyen-pe-timeline", children: /* @__PURE__ */ c("ol", { className: "adyen-pe-timeline__items", children: o }) }) });
}
export {
  R as default
};
