import { jsx as r, jsxs as k } from "react/jsx-runtime";
import { bn as g } from "./use-page-data.BYLN5MSa.js";
import { a as c } from "./twind.Cg2g8xBZ.js";
import { b as j } from "./use-section-style.CqWLdjop.js";
import { B as p } from "./BrickRoot.CQgjTuOI.js";
import { u as C } from "./use-brick-props.C-zGB4NN.js";
function z(h) {
  const { brick: d, editable: i } = h, a = C(h), u = j(d), l = a.columns || [], o = a.rows || [], b = a.showHeader !== !1, y = a.striped !== !1, N = a.hover !== !1, x = a.fontSize || "text-sm", w = (e, n) => {
    if (e == null || e === "")
      return "-";
    switch (n) {
      case "currency": {
        const t = parseFloat(String(e).replace(/[^0-9.-]/g, ""));
        return Number.isNaN(t) ? String(e) : new Intl.NumberFormat("en-US", {
          style: "currency",
          currency: "USD"
        }).format(t);
      }
      case "date": {
        const t = new Date(e);
        return Number.isNaN(t.getTime()) ? String(e) : t.toLocaleDateString();
      }
      case "link":
      case "image":
        return String(e);
      default:
        return String(e);
    }
  }, S = (e, n) => {
    const t = w(e, n.type);
    switch (n.type) {
      case "link": {
        if (t && t !== "-") {
          const s = t.startsWith("http");
          return /* @__PURE__ */ r(
            "a",
            {
              href: t,
              target: s ? "_blank" : "_self",
              rel: s ? "noopener noreferrer" : void 0,
              className: "text-blue-600 hover:text-blue-800 underline",
              children: "View"
            }
          );
        }
        return /* @__PURE__ */ r("span", { className: "text-gray-400", children: "-" });
      }
      case "image":
        return t && t !== "-" ? /* @__PURE__ */ r(
          "img",
          {
            src: t,
            alt: "",
            className: "w-12 h-12 object-cover rounded",
            onError: (s) => {
              s.currentTarget.style.display = "none";
            }
          }
        ) : /* @__PURE__ */ r("span", { className: "text-gray-400", children: "-" });
      default:
        return /* @__PURE__ */ r("span", { children: t });
    }
  }, m = (e) => {
    switch (e) {
      case "center":
        return "text-center";
      case "right":
        return "text-right";
      default:
        return "text-left";
    }
  }, f = (e) => {
    switch (e) {
      case "min":
        return { width: "min-content" };
      case "1fr":
        return { flex: "1" };
      case "2fr":
        return { flex: "2" };
      case "3fr":
        return { flex: "3" };
      default:
        return { width: "auto" };
    }
  };
  return l.length === 0 ? /* @__PURE__ */ r(
    p,
    {
      brick: d,
      editable: i,
      manifest: g,
      className: c("flex flex-col grow items-center justify-center text-center", Object.values(u)),
      children: /* @__PURE__ */ r("div", { className: "p-8", children: i ? "Add columns to this table in the panel" : "No columns configured" })
    }
  ) : /* @__PURE__ */ r(
    p,
    {
      brick: d,
      editable: i,
      manifest: g,
      className: c("overflow-x-auto", Object.values(u)),
      children: /* @__PURE__ */ k("table", { className: c("w-full border-collapse", x), children: [
        b && /* @__PURE__ */ r("thead", { children: /* @__PURE__ */ r("tr", { className: "border-b-2 border-gray-200", children: l.map((e, n) => /* @__PURE__ */ r(
          "th",
          {
            className: c(
              "py-3 px-4 font-semibold text-gray-700 bg-gray-50",
              m(e.align),
              n === 0 && "rounded-tl-lg",
              n === l.length - 1 && "rounded-tr-lg"
            ),
            style: f(e.width),
            children: e.header
          },
          n
        )) }) }),
        /* @__PURE__ */ r("tbody", { children: o.length === 0 ? /* @__PURE__ */ r("tr", { children: /* @__PURE__ */ r("td", { colSpan: l.length, className: "py-8 px-4 text-center text-gray-500", children: i ? "Add data to this table in the panel" : "No data to display" }) }) : o.map((e, n) => /* @__PURE__ */ r(
          "tr",
          {
            className: c(
              "border-b border-gray-100",
              y && n % 2 === 0 && "bg-gray-50/50",
              N && "hover:bg-gray-100/50 transition-colors duration-200"
            ),
            children: l.map((t, s) => /* @__PURE__ */ r(
              "td",
              {
                className: c(
                  "py-3 px-4",
                  m(t.align),
                  n === o.length - 1 && s === 0 && "rounded-bl-lg",
                  n === o.length - 1 && s === l.length - 1 && "rounded-br-lg"
                ),
                style: f(t.width),
                children: S(e[t.field], t)
              },
              s
            ))
          },
          n
        )) })
      ] })
    }
  );
}
export {
  z as default
};
