"use client";
import { jsx as u } from "react/jsx-runtime";
import { DndContext as P } from "../../../../../../node_modules/@dnd-kit/core/dist/core.esm.js";
import { createSnapModifier as W, restrictToHorizontalAxis as v } from "../../../../../../node_modules/@dnd-kit/modifiers/dist/modifiers.esm.js";
import f from "../../../../../../_virtual/dayjs.min.js";
import { forwardRef as H, useContext as _, useMemo as E, useCallback as g } from "react";
import { VariableSizeList as N } from "../../../../../../node_modules/react-window/dist/index.esm.js";
import { GanttContext as Z } from "../../Gantt/GanttContext.js";
import { getWholeWidth as j } from "../../../utils/getWholeWidth.js";
import A from "./BarsRow/BarsRow.js";
import { GanttConsts as F } from "../../../constants/GanttConsts.js";
import { DragTypes as p } from "../../../enums/DragTypes.js";
const L = ({
  scaleWidth: D,
  style: x,
  children: l,
  ...T
}) => /* @__PURE__ */ u(
  "div",
  {
    style: { ...x, width: D, position: "relative" },
    ...T,
    children: l
  }
), O = H(
  ({ width: D, height: x, data: l, setData: T, onBarDoubleClick: b, onBarChange: y }, R) => {
    const { settings: o, scaleDates: S } = _(Z), G = E(() => j(
      S,
      o.dimension,
      o.scaleStepItems,
      o.stepWidth
    ), [
      S,
      o.dimension,
      o.scaleStepItems,
      o.stepWidth
    ]), M = E(
      () => W(o.gridSize),
      [o.gridSize]
    ), I = E(() => {
      const d = [];
      for (const s of l)
        Object.prototype.hasOwnProperty.call(s, "expanded") && !s.expanded && d.push(s.key);
      return l.filter((s) => !s.parentsKeys.some(
        (m) => d.includes(m)
      ));
    }, [l]), z = g(() => F.ROW_HEIGHT, []), k = g(
      (d) => {
        const s = d.delta.x * o.secondsInPixel, i = 3600 * 24, m = o.gridSize * o.secondsInPixel, w = [...l], r = w.find((t) => t.key === d.active.id), C = d.activatorEvent.target, h = Number(C?.dataset?.dragtype ?? -1);
        if (r?.data?.repeatType && h === p.RESIZE_RIGHT) {
          const { fromTime: t, toTime: a } = r.data;
          let e = a + s;
          a > t && e - t >= i || a < t && e > t ? e = t - 1 : a > t && e < t || a < t && t - e >= i ? e = t + m : e < 0 ? e = e + i : e >= i && (e = e - i), r.data.toTime = Math.round(e);
        }
        if (r?.data?.repeatType && h === p.RESIZE_LEFT) {
          const { fromTime: t, toTime: a } = r.data;
          let e = t + s;
          t < a && a - e >= i || t > a && e < a ? e = a + 1 : t < a && e > a || t > a && e - a >= i ? e = a - m : e < 0 ? e = e + i : e >= i && (e = e - i), r.data.fromTime = Math.round(e);
        }
        if (r?.data?.repeatType && h === p.DRAG) {
          const { fromTime: t, toTime: a } = r.data;
          let e = t + s, n = a + s;
          for (; e < 0; )
            e = e + i;
          for (; n < 0; )
            n = n + i;
          for (; e >= i; )
            e = e - i;
          for (; n >= i; )
            n = n - i;
          r.data.fromTime = Math.round(e), r.data.toTime = Math.round(n);
        }
        if (r?.data && !r?.data?.repeatType) {
          const { startDate: t, endDate: a } = r.data;
          if (t && a) {
            const e = f(t).unix(), n = f(a).unix();
            switch (h) {
              case p.RESIZE_LEFT: {
                let c = e + s;
                n - c < m && (c = n - m), r.data.startDate = f.unix(c).format();
                break;
              }
              case p.RESIZE_RIGHT: {
                let c = n + s;
                c - e < m && (c = e + m), r.data.endDate = f.unix(c).format();
                break;
              }
              case p.DRAG: {
                r.data.startDate = f.unix(Math.round(e + s)).format(), r.data.endDate = f.unix(Math.round(n + s)).format();
                break;
              }
            }
          }
        }
        y && r && y(r, w), T(w);
      },
      [l, y, T, o.gridSize, o.secondsInPixel]
    );
    return /* @__PURE__ */ u("div", { className: "gantt-bars-wrap", children: /* @__PURE__ */ u(
      P,
      {
        modifiers: [v, M],
        onDragEnd: k,
        autoScroll: !1,
        children: /* @__PURE__ */ u(
          N,
          {
            ref: R,
            className: "gantt-bars-list",
            layout: "vertical",
            width: D,
            height: x,
            itemCount: I.length,
            itemSize: z,
            itemData: I,
            innerElementType: (d) => /* @__PURE__ */ u(L, { scaleWidth: G, ...d }),
            children: ({ style: d, data: s, index: i }) => /* @__PURE__ */ u(
              A,
              {
                barData: s[i],
                index: i,
                style: d,
                onBarDoubleClick: b
              }
            )
          }
        )
      }
    ) });
  }
);
O.displayName = "Bars";
export {
  O as default
};
