import { last as T, flattenDeep as A, memoize as N, filter as h, isEmpty as S, omit as P, isString as _, get as u, each as R, has as $, keys as J, includes as j, replace as W, startsWith as I } from "lodash-es";
import { a as c, S as C } from "./STRINGS-rPGH65PW.js";
import { j as p } from "./jsx-runtime-Sp0orL4X.js";
import y from "react";
import { twMerge as K } from "tailwind-merge";
import { getBlockComponent as Y } from "@chaibuilder/runtime";
import { createTailwindcss as v } from "@mhsdesign/jit-browser-tailwindcss";
import F from "@tailwindcss/forms";
import L from "@tailwindcss/typography";
import U from "@tailwindcss/aspect-ratio";
import { g as z, c as M } from "./plugin-f5uJtdXY.js";
import "tailwindcss-palette-generator";
import "./plugin-sJ3Hzvpv.js";
const E = (s, r = "") => {
  const e = s.replace(c, "").split(",").map((n) => n.split(" ").map((a) => {
    if (a === "")
      return "";
    if (a.includes(":")) {
      const i = a.split(":");
      return i[i.length - 1] = r + T(i), i.join(":");
    }
    return `${r}${a}`;
  }).join(" "));
  return A(e).join(" ");
}, ft = (s) => {
  if (!s)
    return [];
  try {
    return JSON.parse(q(s)).filter((t) => !t._type.startsWith("@chai"));
  } catch {
    return [{ _type: "Paragraph", _id: "error", content: "Invalid JSON. Please check the JSON string." }];
  }
};
function q(s) {
  const r = /(asset:\/\/|https:\/\/asset\.localhost\/)(?:localhost\/)?[^"']+/g;
  return s.replace(r, (t) => {
    const e = decodeURIComponent(t), n = e.indexOf("public");
    return n !== -1 ? e.substring(n + 6) : e;
  });
}
const G = (s) => {
  const r = {};
  return Object.keys(s).forEach((t) => {
    _(s[t]) && s[t].startsWith(C) && (r[t] = s[t].replace(C, "").split(","));
  }), r;
}, H = N((s, r) => {
  const t = s.replace(c, "").split(","), e = K(t[0], t[1]);
  return r === "" ? e.replace(c, "").trim() : E(e, r).replace(c, "").trim();
});
function Q(s, r) {
  const t = u(s, `${r}_attrs`, {});
  $(t, "data-ai-key") && delete t["data-ai-key"];
  const e = J(t).join(" ");
  return j(e, "x-show") && !j(e, "x-transition") && (t["x-transition"] = ""), t;
}
function V(s, r) {
  const t = {};
  return Object.keys(s).forEach((e) => {
    if (_(s[e]) && s[e].startsWith(c)) {
      const n = H(s[e], r);
      t[e] = {
        className: n,
        ...Q(s, e)
      };
    }
  }), t;
}
function X(s, r) {
  const t = u(s, "_bindings", {});
  return S(t) ? { ...s } : (R(t, (e, n) => {
    _(e) && u(r, e, null) && (s[n] = u(r, e, null));
  }), s);
}
function B({
  blocks: s,
  parent: r,
  classPrefix: t = "",
  externalData: e = {},
  blockModifierCallback: n
}) {
  const l = s, w = (i) => V(i, t), a = r ? h(s, { _parent: r }) : h(s, (i) => S(i._parent));
  return /* @__PURE__ */ p.jsx(p.Fragment, { children: y.Children.toArray(
    a.map((i, O) => {
      const f = G(i), m = {};
      S(f) || Object.keys(f).forEach((o) => {
        m[o] = y.Children.toArray(
          f[o].map((g) => /* @__PURE__ */ p.jsx(
            B,
            {
              externalData: e,
              classPrefix: t,
              blocks: l,
              parent: g
            }
          ))
        );
      });
      const x = h(l, { _parent: i._id });
      m.children = x.length > 0 ? /* @__PURE__ */ p.jsx(
        B,
        {
          externalData: e,
          classPrefix: t,
          parent: i._id,
          blocks: l
        }
      ) : null;
      const d = Y(i._type);
      if (d !== null) {
        let o = i;
        const g = d.component;
        return o = { ...d.defaults, ...i }, n && (o = n(o)), y.createElement(
          g,
          P(
            {
              blockProps: {},
              inBuilder: !1,
              ...o,
              index: O,
              ...X(i, e),
              ...w(o),
              ...m
            },
            ["_parent"]
          )
        );
      }
      return /* @__PURE__ */ p.jsxs("noscript", { children: [
        i._type,
        " not found"
      ] });
    })
  ) });
}
async function Z(s, r, t = [], e = "", n = !1) {
  return await v({
    tailwindConfig: {
      darkMode: "class",
      safelist: t,
      theme: z(s),
      plugins: [F, L, U, M],
      corePlugins: { preflight: n },
      ...e ? { prefix: `${e}` } : {}
    }
  }).generateStylesFromContent(
    ` ${n ? "@tailwind base;" : ""}
      @tailwind components;
      @tailwind utilities;`,
    r
  );
}
const D = (s, r) => s.map((t) => {
  const e = {};
  return Object.keys(t).forEach((n) => {
    I(t[n], c) && (e[n] = E(t[n], r));
  }), e;
}), b = (s, r, t = "", e = !1) => Z(
  r,
  [W(JSON.stringify(D(s, t)), /#styles:/g, "")],
  [],
  t,
  e
), mt = async (s, r = {}, t = "", e = !0) => await b(s, r, t, e);
export {
  B as RenderChaiBlocks,
  ft as convertToBlocks,
  mt as getStylesForBlocks
};
