"use client";
import { jsx as r, jsxs as u } from "react/jsx-runtime";
import { useDraggable as W } from "../../../../../../../../node_modules/@dnd-kit/core/dist/core.esm.js";
import D from "../../../../../../../../_virtual/dayjs.min.js";
import { memo as H, useContext as G, useMemo as l, useCallback as T } from "react";
import { TooltipProvider as X, Tooltip as B, TooltipTrigger as M, TooltipContent as O } from "../../../../../../tooltip.js";
import { cn as v } from "../../../../../../../../lib/utils.js";
import { GanttContext as U } from "../../../../Gantt/GanttContext.js";
import { BarsRowContext as q } from "../BarsRowContext.js";
import { DragTypes as t } from "../../../../../enums/DragTypes.js";
let b;
const J = H(
  ({
    title: L,
    barKey: A,
    startDate: a,
    endDate: m,
    repetead: y,
    onBarDoubleClick: w,
    leftRender: p
  }) => {
    const { scaleDates: P, settings: n } = G(U), { barData: x } = G(q), e = n.draggable === !0, {
      attributes: I,
      listeners: c,
      transform: h,
      activatorEvent: R,
      setNodeRef: k,
      setActivatorNodeRef: N
    } = W({
      id: A,
      disabled: !e
    }), d = l(() => (m - a) / n.secondsInPixel, [m, n.secondsInPixel, a]), E = l(() => y ? (24 * 3600 - 1) / n.secondsInPixel : 0, [y, n.secondsInPixel]), C = l(() => (a - P[0]) / n.secondsInPixel, [P, n.secondsInPixel, a]), s = l(() => +(R?.target?.dataset?.dragtype ?? 0), [R?.target]), i = l(() => {
      if (!h)
        return 0;
      const o = E ? E - d : null, S = n.gridSize - d;
      let f = s === t.RESIZE_LEFT ? -h.x : h.x;
      return f !== 0 && s !== t.DRAG && (o !== null && f > o && (f = o), f < S && (f = S)), f;
    }, [s, E, n.gridSize, d, h]), g = l(() => s === t.DRAG ? d : d + i, [s, i, d]), Z = l(() => s === t.DRAG ? `translateX(${i}px)` : `translateX(${s === t.RESIZE_LEFT ? -i : 0}px)`, [s, i]), _ = l(() => {
      const o = [t.RESIZE_LEFT, t.DRAG].includes(
        s
      ) ? i * n.secondsInPixel : 0;
      return D.unix(
        a + (s === t.RESIZE_LEFT ? -o : o)
      ).format("L LTS");
    }, [s, i, n.secondsInPixel, a]), j = l(() => {
      const o = [t.RESIZE_RIGHT, t.DRAG].includes(
        s
      ) ? i * n.secondsInPixel : 0;
      return D.unix(m + o).format("L LTS");
    }, [s, i, m, n.secondsInPixel]), z = T(() => {
      w && w(x);
    }, [x, w]), F = T(
      (o) => {
        e && (b = setTimeout(() => {
          c?.onPointerDown && c.onPointerDown(o);
        }, 200));
      },
      [c, e]
    ), $ = T(() => {
      b && clearTimeout(b);
    }, []);
    return /* @__PURE__ */ r(X, { children: /* @__PURE__ */ u(B, { children: [
      /* @__PURE__ */ r(M, { asChild: !0, children: /* @__PURE__ */ r(
        "div",
        {
          className: "absolute z-10 flex items-center",
          style: {
            height: "40px",
            top: 0,
            left: C,
            width: g,
            transform: Z
          },
          ref: k,
          children: /* @__PURE__ */ u(
            "div",
            {
              className: "flex h-6 w-full items-center justify-between rounded px-0.5 text-white",
              "data-dragtype": t.DRAG,
              ...e ? c : {},
              ...e ? I : {},
              onPointerDown: F,
              onPointerUp: $,
              onDoubleClick: z,
              style: {
                backgroundColor: x.color,
                ...e ? {} : { cursor: "default" }
              },
              children: [
                e && /* @__PURE__ */ u(
                  "div",
                  {
                    className: v(
                      "flex h-full w-2.5 flex-shrink-0 cursor-w-resize justify-between rounded bg-black/25",
                      g < 25 && "hidden group-hover:flex"
                    ),
                    ref: e ? N : void 0,
                    "data-dragtype": t.RESIZE_LEFT,
                    ...e ? c : {},
                    ...e ? I : {},
                    style: {
                      ...e ? {} : { cursor: "default", pointerEvents: "none" }
                    },
                    children: [
                      /* @__PURE__ */ r("span", { className: "h-full w-0.5 bg-white/60" }),
                      /* @__PURE__ */ r("span", { className: "h-full w-0.5 bg-white/60" })
                    ]
                  }
                ),
                /* @__PURE__ */ u(
                  "div",
                  {
                    className: v(
                      "flex items-center gap-2 overflow-hidden px-2 text-ellipsis whitespace-nowrap select-none",
                      g < 100 && "hidden"
                    ),
                    "data-dragtype": t.DRAG,
                    children: [
                      p && /* @__PURE__ */ r(
                        "div",
                        {
                          className: "flex-shrink-0",
                          onClick: (o) => {
                            o.stopPropagation();
                          },
                          children: typeof p == "function" ? p(x) : p
                        }
                      ),
                      /* @__PURE__ */ r("div", { className: "overflow-hidden text-ellipsis whitespace-nowrap", children: L })
                    ]
                  }
                ),
                e && /* @__PURE__ */ u(
                  "div",
                  {
                    className: v(
                      "flex h-full w-2.5 flex-shrink-0 cursor-e-resize justify-between rounded bg-black/25",
                      g < 25 && "hidden group-hover:flex"
                    ),
                    ref: e ? N : void 0,
                    "data-dragtype": t.RESIZE_RIGHT,
                    ...e ? c : {},
                    ...e ? I : {},
                    style: {
                      ...e ? {} : { cursor: "default", pointerEvents: "none" }
                    },
                    children: [
                      /* @__PURE__ */ r("span", { className: "h-full w-0.5 bg-white/60" }),
                      /* @__PURE__ */ r("span", { className: "h-full w-0.5 bg-white/60" })
                    ]
                  }
                )
              ]
            }
          )
        }
      ) }),
      /* @__PURE__ */ r(
        O,
        {
          side: "top",
          className: "min-w-max text-xs",
          sideOffset: 5,
          children: `${_} → ${j}`
        }
      )
    ] }) });
  }
);
J.displayName = "BarItem";
export {
  J as default
};
