import { jsxs as d, Fragment as v, jsx as r } from "react/jsx-runtime";
import { useTranslateProvider as g } from "@/common/languageTranslator/TranslationContext";
import { showToast as k } from "@/common/toaster/toaster";
import I from "@/hooks/useTrackEvent";
import O from "@/views/custom-components/CustomConfirmationModal";
import { Dialog as Q, DialogTitle as A, DialogContent as D, TextField as N, DialogActions as S, Button as u } from "@mui/material";
import c from "prop-types";
import { useState as y, useCallback as F, useEffect as M } from "react";
import w from "@/common/services/util/GeneralUtil";
const U = ({ configuredQuery: e, open: _, onClose: l }) => {
  const [n, C] = y(""), [b, $] = y(!1), [E, s] = y(!1), T = I(), { translate: a } = g(), p = w.isEkavach || w.isPhmp, m = F(() => {
    const i = e.query.includes("public");
    $(i);
    let o = `DELETE FROM QUERY_MASTER WHERE CODE='${e.code}';

`;
    o += `INSERT INTO QUERY_MASTER (uuid, created_by, created_on, modified_by, modified_on, code, params, query, description, returns_result_set, state, is_mobile_query, is_public ${p ? ", is_clickhouse_query" : ""}) 
VALUES ( 
`, o += `'${e.uuid}', ${e.createdBy}, current_date, ${e.createdBy}, current_date, `, o += `'${e.code}', 
`, o += e.params ? `'${e.params}', 
` : `null, 
`, o += `'${e.query.replace(/'/g, "''")}', 
`, o += e.description ? `'${e.description}', 
` : `null, 
`, o += e.returnsResultSet !== void 0 ? `${e.returnsResultSet}, ` : "false, ", o += e.state ? `'${e.state}', ` : "'ACTIVE', ", o += ` ${e == null ? void 0 : e.isMobileQuery},`, o += `${e == null ? void 0 : e.isPublic}`, p && (o += `,${e.isClickhouseQuery || !1}`), o += ");", C(o);
  }, [e]), x = () => {
    navigator.clipboard.writeText(n).then(() => {
      k({ message: "Query Copied", type: "success" }), l();
    });
  }, R = () => {
    f(), s((i) => !i);
  }, f = () => {
    const i = new Blob([n], { type: "text/plain" }), o = (i.size / 1024).toFixed(2), h = URL.createObjectURL(i), t = document.createElement("a");
    t.href = h, t.download = `V${(/* @__PURE__ */ new Date()).getTime()}__CHANGE_IN_${e.code}.sql`, document.body.appendChild(t), t.click(), T("file_download", {
      file_type: "sql",
      file_name: t.download,
      file_size: `${o} KB`,
      download_status: "success"
    }), document.body.removeChild(t), URL.revokeObjectURL(h), l();
  }, q = () => {
    b ? s((i) => !i) : f();
  };
  return M(() => {
    e && m();
  }, [e, m]), /* @__PURE__ */ d(v, { children: [
    /* @__PURE__ */ r(
      O,
      {
        description: "This Flyway contains keyword 'public'. Are you sure you want to download?",
        title: "CONFIRM ACTION",
        handleClick: R,
        open: E,
        handleClose: () => s((i) => !i),
        dataTestId: "showflywayquery-customconfirmationmodal"
      }
    ),
    /* @__PURE__ */ d(Q, { open: _, onClose: l, fullWidth: !0, maxWidth: "md", children: [
      /* @__PURE__ */ r(A, { className: "p-2.5 px-5 flex border-b font-medium text-lg", children: a("Flyway Query") }),
      /* @__PURE__ */ r(D, { children: /* @__PURE__ */ r(
        N,
        {
          multiline: !0,
          rows: 10,
          variant: "outlined",
          fullWidth: !0,
          value: n,
          sx: {
            "& .MuiInputBase-input": {
              fontSize: "14px"
            },
            "& .MuiOutlinedInput-root": {
              // fontSize: '14px'
              borderRadius: "5px"
            }
          },
          slotProps: {
            input: {
              readOnly: !0
            }
          },
          "data-testid": "showflywayquery-textfield"
        }
      ) }),
      /* @__PURE__ */ d(S, { children: [
        /* @__PURE__ */ r(
          u,
          {
            onClick: q,
            variant: "contained",
            size: "small",
            color: "primary",
            "data-testid": "showflywayquery-button-download",
            children: a("Download")
          }
        ),
        /* @__PURE__ */ r(
          u,
          {
            onClick: x,
            variant: "contained",
            size: "small",
            color: "primary",
            "data-testid": "showflywayquery-button-copyquery",
            children: a("Copy Query")
          }
        ),
        /* @__PURE__ */ r(
          u,
          {
            onClick: l,
            variant: "contained",
            color: "secondary",
            size: "small",
            "data-testid": "showflywayquery-button-cancel",
            children: a("Cancel")
          }
        )
      ] })
    ] })
  ] });
};
U.propTypes = {
  configuredQuery: c.any,
  open: c.any,
  onClose: c.any
};
export {
  U as default
};
//# sourceMappingURL=ShowFlywayQuery-BkyA6QaA-2t9pZjcx.js.map
