import d from "react";
import { r as T, a as r, M as $, H as w, B as x, s as z, o as S } from "./App-f49e4ede.mjs";
import { S as C } from "./chakra-ui-skeleton.esm-18eff055.mjs";
import "@emotion/react";
import "react-dom";
import "@emotion/styled";
import "@feedloop/formula-editor";
import "@feedloop/expression-parser";
import "crypto-js";
import "react-mentions";
function H() {
  var u, E;
  const f = T(), [h, v] = d.useState("<svg></svg>"), s = d.useMemo(() => {
    var c, l, m;
    if (!((l = (c = f.data) == null ? void 0 : c.data) != null && l.tables))
      return;
    const n = (m = f.data) == null ? void 0 : m.data.tables, a = /* @__PURE__ */ new Set();
    let e = "erDiagram";
    const p = n.concat().sort((o, t) => t.name === "users" ? 0 : 1);
    let i = "";
    return p.forEach((o) => {
      e += `
${o.name} {
`, o.columns.forEach((t) => {
        if (t.references || (e += `${t.type} ${t.name}
`), t.references) {
          const b = t.name.split("_")[0], g = [o.name, t.references.targetTable].sort().concat(t.references.type || "ManyToMany").join("_");
          a.has(g) || (a.add(g), t.references.type === "OneToMany" ? i += `${o.name} }o--|| ${t.references.targetTable} : ${b}
` : i += `${o.name} }o--o{ ${t.references.targetTable} : ${b}
`);
        }
      }), e += `}
`;
    }), `${e}${i}`;
  }, [(E = (u = f.data) == null ? void 0 : u.data) == null ? void 0 : E.tables]), y = d.useCallback((n) => {
    const a = new Blob([n]), e = document.createElement("a");
    e.download = "ERD.svg", e.href = URL.createObjectURL(a), document.body.appendChild(e), e.click(), document.body.removeChild(e), URL.revokeObjectURL(e.href);
  }, []);
  return d.useEffect(() => {
    const n = setTimeout(async () => {
      if (!s)
        return;
      const {
        default: a
      } = await import("./mermaid.esm.min-ac303610.mjs"), e = await import("./index-644e2bc8.mjs");
      a.initialize({
        theme: "forest",
        // @ts-ignore
        themeCSS: `
          .entityBox {
            fill: #F6F6F6;
            stroke: #E9E9E9;
          }
          .attributeBoxOdd {
            fill: #ffffff;
            stroke: #E9E9E9;
          }
          .attributeBoxEven {
            fill: #F6F6F6;
            stroke: #E9E9E9;
          }
          .relationshipLabelBox {
            fill: #E9E9E9;
            opacity: 0.7;
          }`,
        maxTextSize: 9e4
      }), a.mermaidAPI.render("graph", s, (p) => {
        const i = p.replace(/ height="[^"]*"/, "");
        v(i), setTimeout(() => {
          const c = e.zoom().on("zoom", m), l = e.select("#erd").call(
            // @ts-ignore
            c
          );
          function m({
            transform: o
          }) {
            l.select("#graph").attr("transform", o);
          }
        }, 500);
      });
    }, 300);
    return () => clearTimeout(n);
  }, [s]), s ? /* @__PURE__ */ r($, {
    Header: /* @__PURE__ */ r(w, {
      children: /* @__PURE__ */ r(x, {
        size: "sm",
        variant: "outline",
        fontSize: "sm",
        fontWeight: "400",
        onClick: () => y(h),
        leftIcon: /* @__PURE__ */ r(z, {
          fontSize: "16px"
        }),
        children: "Download"
      })
    }),
    MainComponent: /* @__PURE__ */ r(S, {
      position: "relative",
      width: "100%",
      id: "zoom-container",
      overflow: "hidden",
      children: /* @__PURE__ */ r(S, {
        id: "erd",
        dangerouslySetInnerHTML: {
          __html: h
        }
      })
    })
  }) : /* @__PURE__ */ r(C, {});
}
export {
  H as default
};
