import { jsx as o, Fragment as U, jsxs as G } from "react/jsx-runtime";
import { useState as l, useRef as R, useEffect as k, useCallback as n } from "react";
import { isValid as V, format as J, parse as Q } from "date-fns";
import S from "../../../date-range-picker/DateRangePicker.js";
import W from "../../../date-picker/DatePicker.js";
import X from "../../../input/Input.js";
import Y from "../../../button/Button.js";
import { c as Z } from "../../../../index-BtlgVD4m.js";
const $ = "dd.MM.yyyy", rt = ({
  value: e,
  isEditable: I,
  editorType: s = "text",
  dateFormat: f = $,
  record: M,
  dataIndex: L,
  renderEditor: D,
  onSave: a,
  children: x
}) => {
  const [u, r] = l(!1), [i, m] = l(typeof e == "string" ? e : ""), [N, P] = l(
    typeof e == "object" && e !== null && "from" in e ? e : void 0
  ), [h, y] = l(e), [d, b] = l(!1), [K, C] = l(!1), g = R(null), p = R(null);
  k(() => {
    typeof e == "string" ? m(e) : typeof e == "object" && e !== null && "from" in e && P(e), y(e);
  }, [e]), k(() => {
    u && s === "text" && g.current && (g.current.focus(), g.current.select());
  }, [u, s]), k(() => {
    if (!u || s !== "date" && s !== "dateRange")
      return;
    const t = (c) => {
      p.current && !p.current.contains(c.target) && r(!1);
    };
    return document.addEventListener("mousedown", t), () => document.removeEventListener("mousedown", t);
  }, [u, s]);
  const E = n(() => {
    typeof e == "string" && i !== e && a(i), r(!1);
  }, [i, e, a]), j = n(
    (t) => {
      t.key === "Enter" ? (t.preventDefault(), E()) : t.key === "Escape" && (typeof e == "string" && m(e), r(!1));
    },
    [E, e]
  ), v = n((t) => {
    t.stopPropagation(), r(!0), C(!1);
  }, []), A = n(() => {
    C(!0);
  }, []), B = n(() => {
    C(!1);
  }, []), H = n(
    (t) => {
      if (t && V(t)) {
        const c = J(t, f);
        m(c), a(t);
      }
      r(!1);
    },
    [f, a]
  ), O = n(() => {
    if (!(!i || typeof i != "string"))
      try {
        const t = Q(i, f, /* @__PURE__ */ new Date());
        return V(t) ? t : void 0;
      } catch {
        return;
      }
  }, [i, f]), _ = n(
    (t) => {
      if (t) {
        P(t);
        const c = { from: t.from, to: t.to };
        a(c);
      }
      r(!1);
    },
    [a]
  ), q = n((t) => {
    y(t);
  }, []), z = n(
    async (t) => {
      if (d)
        return;
      const c = t !== void 0 ? t : h;
      b(!0);
      try {
        await a(c), r(!1);
      } catch {
      } finally {
        b(!1);
      }
    },
    [d, h, a]
  ), w = n(() => {
    d || (y(e), r(!1));
  }, [d, e]);
  return I ? u ? s === "custom" && D ? /* @__PURE__ */ o(
    "div",
    {
      ref: p,
      className: "relative",
      onClick: (t) => t.stopPropagation(),
      onKeyDown: (t) => {
        t.key === "Escape" && (t.stopPropagation(), w());
      },
      children: D({
        value: h,
        record: M,
        dataIndex: L ?? "",
        onChange: q,
        onSave: z,
        onCancel: w,
        isLoading: d
      })
    }
  ) : s === "dateRange" ? /* @__PURE__ */ o(
    "div",
    {
      ref: p,
      className: "relative",
      onClick: (t) => t.stopPropagation(),
      onKeyDown: (t) => {
        (t.key === "Enter" || t.key === "Escape") && t.stopPropagation();
      },
      role: "button",
      tabIndex: 0,
      children: /* @__PURE__ */ o(S, { value: N, onChange: _, format: f })
    }
  ) : s === "date" ? /* @__PURE__ */ o(
    "div",
    {
      ref: p,
      className: "relative",
      onClick: (t) => t.stopPropagation(),
      onKeyDown: (t) => {
        (t.key === "Enter" || t.key === "Escape") && t.stopPropagation();
      },
      role: "button",
      tabIndex: 0,
      children: /* @__PURE__ */ o(W, { value: O(), onChange: H, format: f })
    }
  ) : /* @__PURE__ */ o(
    X,
    {
      ref: g,
      type: "text",
      value: i,
      onChange: m,
      onBlur: E,
      onKeyDown: j,
      onClick: (t) => t.stopPropagation()
    }
  ) : /* @__PURE__ */ G("div", { className: "flex items-center w-full gap-2 group/editable", onMouseEnter: A, onMouseLeave: B, children: [
    /* @__PURE__ */ o("span", { className: "flex-1 truncate", children: x }),
    /* @__PURE__ */ o("div", { className: Z("transition-opacity", K ? "opacity-100" : "opacity-0"), children: /* @__PURE__ */ o(Y, { variant: "ghost", onClick: v, icon: "SquarePen", size: "xs", "aria-label": "Edit cell" }) })
  ] }) : /* @__PURE__ */ o(U, { children: x });
};
export {
  rt as default
};
//# sourceMappingURL=EditableCell.js.map
