"use client";
import { jsx as o, jsxs as W } from "react/jsx-runtime";
import l from "../../../../../../_virtual/dayjs.min.js";
import { forwardRef as y, useContext as N, useRef as S, useCallback as f, useEffect as Y } from "react";
import { VariableSizeList as E } from "../../../../../../node_modules/react-window/dist/index.esm.js";
import { GanttContext as T } from "../../Gantt/GanttContext.js";
import { useForwardRef as A } from "../../../hooks/useForwardRef.js";
import { getScaleItems as C } from "../../../utils/getScaleItems.js";
import { GanttDimensions as u } from "../../../enums/GanttDimensions.js";
import { GanttDimensionsSettings as O } from "../../../constants/DimensionsSettings.js";
import { GanttConsts as c } from "../../../constants/GanttConsts.js";
const _ = y(
  ({ width: I, wrapRef: p }, g) => {
    const { scaleDates: e, setScaleDates: m, setScaleRenderState: h, settings: t } = N(T), i = A(g), d = S(null), x = S(null), D = f(
      (s) => {
        const n = l.unix(e[s]);
        return t.dimension === u.DAY ? n.daysInMonth() * t.stepWidth : t.scaleStepItems * t.stepWidth;
      },
      [
        e,
        t.dimension,
        t.scaleStepItems,
        t.stepWidth
      ]
    ), M = f(
      ({ scrollOffset: s }) => {
        const n = O[t.dimension].unitOfTime;
        if (s < c.MIN_SCROLL_OFFSET) {
          const r = l.unix(e[0]).subtract(1, n);
          let a = t.scaleStepItems * t.stepWidth;
          t.dimension === u.DAY && (a = r.daysInMonth() * t.stepWidth), m([r.unix(), ...e]), p.current?.scrollTo({ left: s + a }), i?.current?.resetAfterIndex(0);
        }
        (d.current?.scrollWidth || 0) - (d.current?.clientWidth || 0) - s - c.TREE_WIDTH < c.MIN_SCROLL_OFFSET && (m([
          ...e,
          l.unix(e[e.length - 1]).add(1, n).unix()
        ]), i?.current?.resetAfterIndex(0));
      },
      [
        i,
        e,
        m,
        t.dimension,
        t.scaleStepItems,
        t.stepWidth,
        p
      ]
    ), R = f(
      (s) => {
        h(s);
      },
      [h]
    );
    return Y(() => {
      i?.current?.resetAfterIndex(0);
    }, [i, t.dimension]), /* @__PURE__ */ o("div", { className: "gantt-scale-wrap", children: /* @__PURE__ */ o(
      E,
      {
        className: "gantt-scale-list",
        layout: "horizontal",
        width: I,
        height: c.HEADER_HEIGHT,
        itemCount: e.length,
        itemSize: D,
        itemData: e,
        ref: i,
        outerRef: d,
        innerRef: x,
        initialScrollOffset: t.initialScrollOffset,
        onScroll: M,
        onItemsRendered: R,
        children: ({ style: s, index: n, data: r }) => /* @__PURE__ */ W("div", { className: "gantt-scale-item", style: s, children: [
          /* @__PURE__ */ o("div", { className: "gantt-scale-title", children: l.unix(r[n]).format(
            t.dimension === u.DAY ? "MMMM, YYYY" : "ddd, D MMMM, YY"
          ) }),
          /* @__PURE__ */ o("div", { className: "gantt-scale-steps", children: C(t.dimension, r[n]).map(
            (a) => /* @__PURE__ */ o(
              "div",
              {
                className: "gantt-scale-step",
                style: { width: t.stepWidth },
                children: a
              },
              a
            )
          ) })
        ] })
      }
    ) });
  }
);
_.displayName = "Scale";
export {
  _ as default
};
