import { jsx as r, jsxs as i } from "react/jsx-runtime";
import { useTranslateProvider as w } from "@/common/languageTranslator/TranslationContext";
import { showToast as h } from "@/common/toaster/toaster";
import U from "@/hooks/useTrackEvent";
import { generateUUID as N } from "@/utils/UUIDgenerator";
import { faXmark as T } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon as k } from "@fortawesome/react-fontawesome";
import { Modal as C, Typography as c, IconButton as D, TextField as q, Stack as I, Button as p } from "@mui/material";
import d from "@mui/material/Grid2";
import s from "prop-types";
import { useState as j, useEffect as z } from "react";
function B({ open: y, handleClose: l, data: t, type: x }) {
  const [a, f] = j({}), m = N(), { translate: u } = w(), b = U();
  z(() => {
    let e = {};
    t != null && t.featureJson && (t == null ? void 0 : t.featureJson) != null ? e.featureJson = t == null ? void 0 : t.featureJson : e.featureJson = null;
    let n = "";
    e.groupName = t == null ? void 0 : t.groupName, e.menuName = t == null ? void 0 : t.name, e.subgroupName = t == null ? void 0 : t.subgroupName, e.navigationState = t == null ? void 0 : t.navigationState, e.menuType = x, e.uuid = m, e.groupName && e.groupName != null ? (n = "select id from menu_group mg where group_name = '" + e.groupName + "'", e.groupUUID = m) : (n = null, e.groupUUID = null);
    let o;
    e.subgroupName && e.subgroupName != null ? (o = "select sub_group_id from menu_config mc where menu_name = '" + e.subgroupName + "'", e.subgroupUUID = m) : (o = null, e.subgroupUUID = null), e.text = "insert into menu_config (feature_json, group_id, active, is_dynamic_report, menu_name, navigation_state, sub_group_id,", e.text += `
`, e.text += 'menu_type,"uuid", group_name_uuid, sub_group_uuid)', e.text += `
`, e.text += "values(", e.featureJson != null ? e.text += "'" + e.featureJson + "'" : e.text += "null", e.text += `
`, e.text += ",", n != null ? e.text += "(" + n + ")" : e.text += "null", e.text += ",", e.text += "true, false,", e.text += "'" + e.menuName + "'", e.text += ",", e.text += `
`, e.text += "'" + e.navigationState + "'", e.text += ",", o != null ? e.text += "(" + o + ")" : e.text += "null", e.text += ",", e.text += "'" + e.menuType + "'", e.text += ",", e.text += "'" + e.uuid + "'", e.text += ",", e.groupUUID != null ? e.text += "'" + e.groupUUID + "'" : e.text += "null", e.text += ",", e.subgroupUUID != null ? e.text += "'" + e.subgroupUUID + "'" : e.text += "null", e.text += ")", e.text += `
`, e.text += "on conflict(navigation_state,active) do update", e.text += `
`, e.text += "SET feature_json = excluded.feature_json,", e.text += `
`, e.text += "	group_id= excluded.group_id,", e.text += `
`, e.text += " 	menu_name = excluded.menu_name,", e.text += `
`, e.text += "	sub_group_id= excluded.sub_group_id,", e.text += `
`, e.text += " 	menu_type= excluded.menu_type;", f((v) => ({ ...v, queryText: e.text }));
  }, []);
  const g = () => {
    const e = new Blob([a == null ? void 0 : a.queryText], { type: "text/plain" }), n = (e.size / 1024).toFixed(2), o = document.createElement("a");
    o.href = URL.createObjectURL(e), o.download = "V" + (/* @__PURE__ */ new Date()).getTime() + "__CREATED_OR_UPDATED_feature_" + x + ".sql", document.body.appendChild(o), o.click(), document.body.removeChild(o), b("file_download", {
      file_type: "sql",
      file_name: o.download,
      file_size: `${n} KB`,
      download_status: "success"
    }), l();
  }, _ = () => {
    navigator.clipboard.writeText(a == null ? void 0 : a.queryText).then(
      () => {
        h({
          message: "Query copied",
          type: "success"
        });
      },
      (e) => {
        console.error("Could not copy text: ", e);
      }
    ), l();
  };
  return /* @__PURE__ */ r(
    C,
    {
      "data-testid": "flywayquery-modal",
      open: y,
      onClose: l,
      sx: {
        "& .MuiBackdrop-root": {
          backgroundColor: "rgba(0, 0, 0, 0.1)"
        }
      },
      children: /* @__PURE__ */ i(
        d,
        {
          container: !0,
          className: "flex flex-col absolute top-[5%] left-1/2 transform -translate-x-1/2 w-[99%] max-w-full sm:max-w-[600px] md:max-w-[750px] lg:max-w-[900px] bg-[var(--color-background2)] shadow-md border border-slate-300 dark:border-slate-700 overflow-y-auto max-h-[90vh]",
          children: [
            /* @__PURE__ */ i(d, { className: "flex justify-between items-center p-[5px_10px] bg-slate-200 dark:bg-slate-800 border-b border-slate-300 dark:border-slate-700", children: [
              /* @__PURE__ */ r(c, { variant: "body1", className: "font-bold text-customBlue", children: u("FLYWAY QUERY") }),
              /* @__PURE__ */ r(
                D,
                {
                  size: "small",
                  onClick: l,
                  "data-testid": "flywayquery-close-button",
                  children: /* @__PURE__ */ r(k, { icon: T })
                }
              )
            ] }),
            /* @__PURE__ */ r(d, { className: "border-b border-slate-200 dark:border-slate-800 p-2.5 ", children: /* @__PURE__ */ r(
              q,
              {
                multiline: !0,
                rows: 10,
                variant: "outlined",
                fullWidth: !0,
                value: a == null ? void 0 : a.queryText,
                sx: {
                  "& .MuiInputBase-input": {
                    fontSize: "14px"
                  },
                  "& .MuiOutlinedInput-root": {
                    borderRadius: "5px"
                  }
                },
                slotProps: {
                  input: {
                    readOnly: !0
                  }
                },
                "data-testid": "flywayquery-textfield-query-display"
              }
            ) }),
            /* @__PURE__ */ r(d, { className: "border-b border-slate-200 dark:border-slate-800 pb-[1px] pr-2.5 ", children: /* @__PURE__ */ i(
              I,
              {
                direction: "row",
                spacing: 2,
                justifyContent: "flex-end",
                mt: 1,
                mb: 1,
                children: [
                  /* @__PURE__ */ r(
                    p,
                    {
                      "data-testid": "flywayquery-download-button",
                      variant: "contained",
                      color: "primary",
                      size: "small",
                      onClick: g,
                      children: u("Download")
                    }
                  ),
                  /* @__PURE__ */ r(
                    p,
                    {
                      "data-testid": "flywayquery-copy-button",
                      variant: "contained",
                      size: "small",
                      color: "primary",
                      onClick: _,
                      children: u("Copy")
                    }
                  ),
                  /* @__PURE__ */ r(
                    p,
                    {
                      "data-testid": "flywayquery-close-button-action",
                      variant: "contained",
                      size: "small",
                      color: "secondary",
                      onClick: l,
                      children: /* @__PURE__ */ i(c, { variant: "body2", children: [
                        u("Close"),
                        " "
                      ] })
                    }
                  )
                ]
              }
            ) })
          ]
        }
      )
    }
  );
}
B.propTypes = {
  open: s.any,
  handleClose: s.any,
  data: s.any,
  type: s.any
};
export {
  B as default
};
//# sourceMappingURL=FlywayQuery-Cx2sly6g-DdhUw6bN.js.map
