import { jsxs as u, jsx as r } from "react/jsx-runtime";
import { cF as M, cG as k, aE as x, cH as E, cI as Q, cJ as q, cK as m, S as p, aU as f, cw as T, cL as c, aR as l, B as j, cM as A, cN as N } from "./index-BJZQSU1O.mjs";
import { useMemo as d, useCallback as y } from "react";
import { f as b, g as w } from "./sqlRunnerSlice-C3O32avm.mjs";
const z = {
  ...N,
  readOnly: !0
}, P = ({
  selectedView: n = "query"
}) => {
  const { colorScheme: v } = M(), { projectUuid: C } = k(), { data: i } = x(C), s = d(
    () => E(i?.warehouseConnection?.type),
    [i]
  ), { data: o, error: a, isInitialLoading: S } = Q(), L = y(
    (e) => {
      q(e, s), e.editor.defineTheme("lightdash-light", {
        base: "vs",
        inherit: !0,
        ...m("light")
      }), e.editor.defineTheme("lightdash-dark", {
        base: "vs-dark",
        inherit: !0,
        ...m("dark")
      });
    },
    [s]
  ), g = y(
    (e) => {
      if (!e) return "";
      try {
        return b(e, {
          language: w(i?.warehouseConnection?.type)
        });
      } catch (t) {
        return console.error(
          "Error rendering SQL:",
          t instanceof Error ? t.message : "Unknown error occurred"
        ), e;
      }
    },
    [i?.warehouseConnection?.type]
  ), h = d(
    () => n === "pivotQuery" && !o?.pivotQuery ? "query" : n,
    [n, o?.pivotQuery]
  ), O = d(() => {
    const e = h === "pivotQuery" ? o?.pivotQuery : o?.query;
    return g(e);
  }, [o?.query, o?.pivotQuery, h, g]);
  return S ? /* @__PURE__ */ u(p, { my: "xs", align: "center", children: [
    /* @__PURE__ */ r(f, { size: "lg", color: "gray", mt: "xs" }),
    /* @__PURE__ */ r(T, { order: 4, fw: 500, c: "ldGray.7", children: "Compiling SQL" })
  ] }) : a?.error.message ? /* @__PURE__ */ r("div", { style: { margin: 10 }, children: /* @__PURE__ */ r(
    c,
    {
      icon: /* @__PURE__ */ r(l, { size: "1rem" }),
      title: "Compilation error",
      color: "red",
      variant: "filled",
      children: /* @__PURE__ */ r("p", { children: a.error.message })
    }
  ) }) : a?.error.data ? /* @__PURE__ */ r("div", { style: { margin: 10 }, children: /* @__PURE__ */ r(
    c,
    {
      icon: /* @__PURE__ */ r(l, { size: "1rem" }),
      title: "Compilation error",
      color: "red",
      variant: "filled",
      children: Object.entries(a.error.data).map(
        ([e, t]) => /* @__PURE__ */ r("p", { children: JSON.stringify(t) }, e)
      )
    }
  ) }) : /* @__PURE__ */ u(p, { gap: 0, h: "100%", children: [
    o?.compilationErrors && o.compilationErrors.length > 0 && /* @__PURE__ */ r("div", { style: { margin: 10 }, children: /* @__PURE__ */ r(
      c,
      {
        icon: /* @__PURE__ */ r(l, { size: "1rem" }),
        title: "Compilation error",
        color: "red",
        variant: "filled",
        children: o.compilationErrors.map(
          (e, t) => /* @__PURE__ */ r("p", { children: e }, t)
        )
      }
    ) }),
    /* @__PURE__ */ r(j, { flex: 1, children: /* @__PURE__ */ r(
      A,
      {
        loading: /* @__PURE__ */ r(f, { color: "gray", size: "xs" }),
        language: s,
        beforeMount: L,
        value: O,
        options: z,
        theme: v === "dark" ? "lightdash-dark" : "lightdash-light"
      }
    ) })
  ] });
};
export {
  P as RenderedSql
};
//# sourceMappingURL=RenderedSql-CI3sP0G4.mjs.map
