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

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