import { jsx as n, jsxs as E } from "react/jsx-runtime";
import { useTranslateProvider as k } from "@/common/languageTranslator/TranslationContext";
import { showToast as S } from "@/common/toaster/toaster";
import M from "@/hooks/useTrackEvent";
import { faXmark as U } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon as W } from "@fortawesome/react-fontawesome";
import { Modal as H, Typography as O, IconButton as j, TextField as z, Stack as Q, Button as b } from "@mui/material";
import y from "@mui/material/Grid2";
import v from "prop-types";
import "react";
import { useSelector as $ } from "react-redux";
function P({
  open: q,
  handleClose: _,
  data: o,
  roleData: t
}) {
  var h, g, w, T, x, A;
  const { user: i } = $((a) => a.Authenticate), { translate: m } = k();
  let e = "";
  if (e = "", e += "begin;", e += `
`, e += "DO $$", e += `
`, e += "DECLARE", e += `
`, e += "var_notification_type_id integer;", e += `
`, e += "var_escalation_level_id integer;", e += `
`, e += "old_notification_type_id integer;", e += `
`, e += "old_escalation_level_id integer;", e += `
`, e += "BEGIN", e += `
`, o.code !== null || o.code !== void 0) {
    e += "DELETE FROM notification_type_master WHERE code = '" + o.code + "'returning id into old_notification_type_id;", e += `
`, e += `
`, e += "INSERT INTO notification_type_master (", e += 'created_by,created_on,modified_by,modified_on,code,"name","type",role_id,state,notification_for,action_on_role_id,data_query,action_query,order_no,color_code,data_for,url_based_action,url,modal_based_action,modal_name,is_location_filter_required,fetch_up_to_level,required_up_to_level,is_fetch_according_aoi,"uuid"', e += ")", e += `
`, e += "VALUES (", e += (i == null ? void 0 : i.id) + ",", e += "now(),", e += (i == null ? void 0 : i.id) + ",", e += "now(),", e += o.code ? "'" + o.code + "'," : "null,", e += o.notificationName ? "'" + o.notificationName + "'," : "null,", e += o.notificationType ? "'" + o.notificationType + "'," : "null,", o.roleId !== null ? e += o.roleId + "," : e += "null,", e += o.state ? "'" + o.state + "'," : "null,", e += o.notificationFor ? "'" + o.notificationFor + "'," : "null,", e += "null,", o.dataQuery !== null ? e += "'" + ((h = o == null ? void 0 : o.dataQuery) == null ? void 0 : h.replace(/'/g, "''")) + "'," : e += "null,", o.actionQuery !== null ? e += "'" + ((g = o == null ? void 0 : o.actionQuery) == null ? void 0 : g.replace(/'/g, "''")) + "'," : e += "null,", o.orderNo !== null ? e += o.orderNo + "," : e += "null,", o.colorCode !== null ? e += "'" + o.colorCode + "'," : e += "null,", o.dataFor !== null ? e += "'" + o.dataFor + "'," : e += "null,", e += o.urlBasedAction ? o.urlBasedAction + "," : (() => {
      let l;
      return o.urlBasedAction === !1 ? l = "false," : l = "null,", l;
    })(), o.url !== null ? e += "'" + o.url + "'," : e += "null,", e += o.modalBasedAction ? o.modalBasedAction + "," : (() => {
      let l;
      return o.modalBasedAction === !1 ? l = "false," : l = "null,", l;
    })(), o.modalName !== null ? e += "'" + o.modalName + "'," : e += "null,", e += o.isLocationFilterRequired ? o.isLocationFilterRequired + "," : "null,", o.fetchUptoLevel !== null ? e += o.fetchUptoLevel + "," : e += "null,", o.requiredUptoLevel !== null ? e += o.requiredUptoLevel + "," : e += "null,", e += o.isFetchAccordingAOI ? o.isFetchAccordingAOI + "," : "null,", o.uuid !== null ? e += "'" + o.uuid + "'" : e += "null", e += ")", e += "returning id into var_notification_type_id;", e += `
`, e += `
`, e += "DELETE FROM notification_type_role_rel WHERE notification_type_id = old_notification_type_id;", e += `
`, e += `
`, e += "INSERT INTO notification_type_role_rel (notification_type_id, role_id)", e += `
`, e += "SELECT var_notification_type_id, id", e += `
`, e += "FROM um_role_master", e += `
`, e += "WHERE name IN (";
    let a = (w = o.roles) == null ? void 0 : w.length, f = 0;
    if (((T = o.roles) == null ? void 0 : T.length) > 0)
      o.roles.forEach((l) => {
        var r;
        f += 1, ((r = t.find((u) => u.id === l)) == null ? void 0 : r.name) != null && (e += "'" + t.find((u) => u.id === l).name + "' "), f === a ? e += ");" : e += ",", e += `
`;
      });
    else {
      f += 1;
      const l = ((x = t == null ? void 0 : t.find((r) => r.id === (o == null ? void 0 : o.roleId))) == null ? void 0 : x.name) ?? null;
      e += l ? `'${l}'` : "'null'", e += ");", e += `
`;
    }
    ((A = o.escalationLevels) == null ? void 0 : A.length) > 0 && o.escalationLevels.forEach((l) => {
      var r, u, N, R, C, I;
      let L = !1;
      if (e += `
`, e += "DELETE FROM escalation_level_master WHERE uuid = '" + l.uuid + "' returning id into old_escalation_level_id;", e += `
`, e += `
`, e += 'INSERT INTO escalation_level_master ("name", notification_type_id, created_by, created_on, modified_by, modified_on,uuid)', e += "VALUES (", e += "'" + l.name + "',", e += "var_notification_type_id,", e += (i == null ? void 0 : i.id) + ",", e += "now(),", e += (i == null ? void 0 : i.id) + ",", e += "now(),", e += "'" + l.uuid + "'", e += ")", e += "returning id into var_escalation_level_id;", e += `
`, e += `
`, e += "DELETE FROM escalation_level_role_rel WHERE escalation_level_id = old_escalation_level_id ;", e += `
`, e += `
`, l.performAction != null && ((r = Object.keys(l.performAction)) == null ? void 0 : r.length) > 0) {
        for (const p in l.performAction)
          if (((u = t.find((d) => d.id === p)) == null ? void 0 : u.name) != null) {
            L = !0;
            break;
          }
      }
      if (L) {
        e += "INSERT INTO escalation_level_role_rel (escalation_level_id, role_id, can_perform_action)", e += `
`, e += "SELECT var_escalation_level_id, id, ";
        let p = (N = Object.keys(l.performAction)) == null ? void 0 : N.length;
        e += `
`, e += "CASE", e += `
`;
        let d = 0;
        for (const s in l.performAction)
          d++, ((R = t.find((c) => c.id === s)) == null ? void 0 : R.name) != null && (e += "WHEN um_role_master.name = '" + ((C = t.find((c) => c.id === s)) == null ? void 0 : C.name) + "' THEN " + l.performAction[s]), d === p && (e += `
`, e += "ELSE false", e += `
`, e += "END"), e += `
`;
        d = 0, e += "FROM um_role_master", e += `
`, e += "WHERE name IN (";
        for (const s in l.performAction)
          d++, ((I = t.find((c) => c.id === s)) == null ? void 0 : I.name) !== void 0 && (e += "'" + t.find((c) => c.id === s).name + "' "), d === p ? e += ");" : e += ",", e += `
`;
      }
    }), e += "END $$;", e += `
`, e += "commit;";
  }
  const F = M(), D = () => {
    const a = new Blob([e], { type: "text/plain" }), f = (a.size / 1024).toFixed(2), l = document.createElement("a");
    l.href = URL.createObjectURL(a), l.download = "V" + (/* @__PURE__ */ new Date()).getTime() + "__CREATED_OR_UPDATED_feature_.sql", document.body.appendChild(l), l.click(), F("file_download", {
      file_type: "sql",
      file_name: l.download,
      file_size: `${f} KB`,
      download_status: "success"
    }), document.body.removeChild(l), _();
  }, B = () => {
    navigator.clipboard.writeText(e).then(
      () => {
        S({
          message: "Query copied",
          type: "success"
        });
      },
      (a) => {
        console.error("Could not copy text: ", a);
      }
    ), _();
  };
  return /* @__PURE__ */ n(
    H,
    {
      "data-testid": "notification-flywayquery-modal",
      open: q,
      onClose: _,
      sx: {
        "& .MuiBackdrop-root": {
          backgroundColor: "rgba(0, 0, 0, 0.1)"
        }
      },
      children: /* @__PURE__ */ E(
        y,
        {
          container: !0,
          sx: {
            position: "absolute",
            top: "10%",
            left: "50%",
            transform: "translateX(-50%)",
            width: "100%",
            bgcolor: "background.paper",
            boxShadow: 24,
            maxWidth: 900,
            display: "flex",
            flexDirection: "column"
          },
          children: [
            /* @__PURE__ */ E(y, { className: "p-2.5 px-5 flex  justify-between border-b", children: [
              /* @__PURE__ */ n(O, { variant: "body1", className: "font-medium text-lg text-customBlue", children: m("Flyway Query") }),
              /* @__PURE__ */ n(
                j,
                {
                  size: "small",
                  onClick: _,
                  "data-testid": "notification-flywayquery-close-button",
                  children: /* @__PURE__ */ n(W, { icon: U })
                }
              )
            ] }),
            /* @__PURE__ */ n(y, { className: "border-b border-gray-300 p-[10px]", children: /* @__PURE__ */ n(
              z,
              {
                multiline: !0,
                rows: 10,
                variant: "outlined",
                fullWidth: !0,
                value: e,
                sx: {
                  "& .MuiInputBase-input": {
                    fontSize: "14px"
                  },
                  "& .MuiOutlinedInput-root": {
                    borderRadius: "5px"
                  }
                },
                slotProps: {
                  input: {
                    readOnly: !0
                  }
                },
                "data-testid": "shownotification-flywayquery-textfield-query-display"
              }
            ) }),
            /* @__PURE__ */ n(y, { className: "border-b border-gray-300 pr-[10px] pb-[1px]", children: /* @__PURE__ */ E(
              Q,
              {
                direction: "row",
                spacing: 2,
                justifyContent: "flex-end",
                mt: 1,
                mb: 1,
                children: [
                  /* @__PURE__ */ n(
                    b,
                    {
                      "data-testid": "notification-flywayquery-download-button",
                      variant: "contained",
                      color: "primary",
                      size: "small",
                      onClick: D,
                      children: m("Download")
                    }
                  ),
                  /* @__PURE__ */ n(
                    b,
                    {
                      "data-testid": "notification-flywayquery-copy-button",
                      variant: "contained",
                      color: "primary",
                      size: "small",
                      onClick: B,
                      children: m("Copy Query")
                    }
                  ),
                  /* @__PURE__ */ n(
                    b,
                    {
                      "data-testid": "notification-flywayquery-close-button-action",
                      variant: "contained",
                      color: "secondary",
                      size: "small",
                      onClick: _,
                      children: /* @__PURE__ */ n(O, { variant: "body2", children: m("Close") })
                    }
                  )
                ]
              }
            ) })
          ]
        }
      )
    }
  );
}
P.propTypes = {
  open: v.any,
  handleClose: v.any,
  data: v.any,
  roleData: v.any
};
export {
  P as default
};
//# sourceMappingURL=NotificationFlywayQuery-BY8Zzml_-C7G-28dx.js.map
