import { jsx as l, jsxs as v } from "react/jsx-runtime";
import { useTranslateProvider as S } from "@/common/languageTranslator/TranslationContext";
import { showToast as M } from "@/common/toaster/toaster";
import U from "@/hooks/useTrackEvent";
import { faXmark as W } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon as z } from "@fortawesome/react-fontawesome";
import { Modal as H, Typography as C, IconButton as Q, TextField as V, Stack as $, Button as h } from "@mui/material";
import y from "@mui/material/Grid2";
import E from "prop-types";
import "react";
import { useSelector as D } from "react-redux";
function P({
  open: F,
  handleClose: a,
  data: i,
  roleData: t
}) {
  var b, g, w, T, A, x;
  const { user: n } = D((r) => r.Authenticate), { translate: m } = S();
  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 += `
`, i.code !== null || i.code !== void 0) {
    e += "DELETE FROM notification_type_master WHERE code = '" + i.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 += (n == null ? void 0 : n.id) + ",", e += "now(),", e += (n == null ? void 0 : n.id) + ",", e += "now(),", e += i.code ? "'" + i.code + "'," : "null,", e += i.notificationName ? "'" + i.notificationName + "'," : "null,", e += i.notificationType ? "'" + i.notificationType + "'," : "null,", i.roleId !== null ? e += i.roleId + "," : e += "null,", e += i.state ? "'" + i.state + "'," : "null,", e += i.notificationFor ? "'" + i.notificationFor + "'," : "null,", e += "null,", i.dataQuery !== null ? e += "'" + ((b = i == null ? void 0 : i.dataQuery) == null ? void 0 : b.replace(/'/g, "''")) + "'," : e += "null,", i.actionQuery !== null ? e += "'" + ((g = i == null ? void 0 : i.actionQuery) == null ? void 0 : g.replace(/'/g, "''")) + "'," : e += "null,", i.orderNo !== null ? e += i.orderNo + "," : e += "null,", i.colorCode !== null ? e += "'" + i.colorCode + "'," : e += "null,", i.dataFor !== null ? e += "'" + i.dataFor + "'," : e += "null,", e += i.urlBasedAction ? i.urlBasedAction + "," : (() => {
      let o;
      return i.urlBasedAction === !1 ? o = "false," : o = "null,", o;
    })(), i.url !== null ? e += "'" + i.url + "'," : e += "null,", e += i.modalBasedAction ? i.modalBasedAction + "," : (() => {
      let o;
      return i.modalBasedAction === !1 ? o = "false," : o = "null,", o;
    })(), i.modalName !== null ? e += "'" + i.modalName + "'," : e += "null,", e += i.isLocationFilterRequired ? i.isLocationFilterRequired + "," : "null,", i.fetchUptoLevel !== null ? e += i.fetchUptoLevel + "," : e += "null,", i.requiredUptoLevel !== null ? e += i.requiredUptoLevel + "," : e += "null,", e += i.isFetchAccordingAOI ? i.isFetchAccordingAOI + "," : "null,", i.uuid !== null ? e += "'" + i.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 r = (w = i.roles) == null ? void 0 : w.length, _ = 0;
    if (((T = i.roles) == null ? void 0 : T.length) > 0)
      i.roles.forEach((o) => {
        var c;
        _ += 1, ((c = t.find((u) => u.id === o)) == null ? void 0 : c.name) != null && (e += "'" + t.find((u) => u.id === o).name + "' "), _ === r ? e += ");" : e += ",", e += `
`;
      });
    else {
      _ += 1;
      const o = ((A = t == null ? void 0 : t.find((c) => c.id === (i == null ? void 0 : i.roleId))) == null ? void 0 : A.name) ?? null;
      e += o ? `'${o}'` : "'null'", e += ");", e += `
`;
    }
    ((x = i.escalationLevels) == null ? void 0 : x.length) > 0 && i.escalationLevels.forEach((o) => {
      var u, N, R, L, I, k;
      let c = !1;
      if (e += `
`, e += "DELETE FROM escalation_level_master WHERE uuid = '" + o.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 += "'" + o.name + "',", e += "var_notification_type_id,", e += (n == null ? void 0 : n.id) + ",", e += "now(),", e += (n == null ? void 0 : n.id) + ",", e += "now(),", e += "'" + o.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 += `
`, o.performAction != null && ((u = Object.keys(o.performAction)) == null ? void 0 : u.length) > 0) {
        for (const p in o.performAction)
          if (((N = t.find((d) => d.id === p)) == null ? void 0 : N.name) != null) {
            c = !0;
            break;
          }
      }
      if (c) {
        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 = (R = Object.keys(o.performAction)) == null ? void 0 : R.length;
        e += `
`, e += "CASE", e += `
`;
        let d = 0;
        for (const f in o.performAction)
          d++, ((L = t.find((s) => s.id === f)) == null ? void 0 : L.name) != null && (e += "WHEN um_role_master.name = '" + ((I = t.find((s) => s.id === f)) == null ? void 0 : I.name) + "' THEN " + o.performAction[f]), d === p && (e += `
`, e += "ELSE false", e += `
`, e += "END"), e += `
`;
        d = 0, e += "FROM um_role_master", e += `
`, e += "WHERE name IN (";
        for (const f in o.performAction)
          d++, ((k = t.find((s) => s.id === f)) == null ? void 0 : k.name) !== void 0 && (e += "'" + t.find((s) => s.id === f).name + "' "), d === p ? e += ");" : e += ",", e += `
`;
      }
    }), e += "END $$;", e += `
`, e += "commit;";
  }
  const O = U(), q = () => {
    const r = new Blob([e], { type: "text/plain" }), _ = (r.size / 1024).toFixed(2), o = document.createElement("a");
    o.href = URL.createObjectURL(r), o.download = "V" + (/* @__PURE__ */ new Date()).getTime() + "__CREATED_OR_UPDATED_feature_.sql", document.body.appendChild(o), o.click(), O("file_download", {
      file_type: "sql",
      file_name: o.download,
      file_size: `${_} KB`,
      download_status: "success"
    }), document.body.removeChild(o), a();
  }, B = () => {
    navigator.clipboard.writeText(e).then(
      () => {
        M({
          message: "Query copied",
          type: "success"
        });
      },
      (r) => {
        console.error("Could not copy text: ", r);
      }
    ), a();
  };
  return /* @__PURE__ */ l(
    H,
    {
      "data-testid": "notification-flywayquery-modal",
      open: F,
      onClose: a,
      sx: {
        "& .MuiBackdrop-root": {
          backgroundColor: "rgba(0, 0, 0, 0.1)"
        }
      },
      children: /* @__PURE__ */ v(
        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__ */ v(y, { className: "p-2.5 px-5 flex  justify-between border-b", children: [
              /* @__PURE__ */ l(C, { variant: "body1", className: "font-medium text-lg text-customBlue", children: m("Flyway Query") }),
              /* @__PURE__ */ l(
                Q,
                {
                  size: "small",
                  onClick: a,
                  "data-testid": "notification-flywayquery-close-button",
                  children: /* @__PURE__ */ l(z, { icon: W })
                }
              )
            ] }),
            /* @__PURE__ */ l(y, { className: "border-b border-gray-300 p-[10px]", children: /* @__PURE__ */ l(
              V,
              {
                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__ */ l(y, { className: "border-b border-gray-300 pr-[10px] pb-[1px]", children: /* @__PURE__ */ v(
              $,
              {
                direction: "row",
                spacing: 2,
                justifyContent: "flex-end",
                mt: 1,
                mb: 1,
                children: [
                  /* @__PURE__ */ l(
                    h,
                    {
                      "data-testid": "notification-flywayquery-download-button",
                      variant: "contained",
                      color: "primary",
                      size: "small",
                      onClick: q,
                      children: m("Download")
                    }
                  ),
                  /* @__PURE__ */ l(
                    h,
                    {
                      "data-testid": "notification-flywayquery-copy-button",
                      variant: "contained",
                      color: "primary",
                      size: "small",
                      onClick: B,
                      children: m("Copy Query")
                    }
                  ),
                  /* @__PURE__ */ l(
                    h,
                    {
                      "data-testid": "notification-flywayquery-close-button-action",
                      variant: "contained",
                      color: "secondary",
                      size: "small",
                      onClick: a,
                      children: /* @__PURE__ */ l(C, { variant: "body2", children: m("Close") })
                    }
                  )
                ]
              }
            ) })
          ]
        }
      )
    }
  );
}
P.propTypes = {
  open: E.any,
  handleClose: E.any,
  data: E.any,
  roleData: E.any
};
export {
  P as default
};
//# sourceMappingURL=NotificationFlywayQuery-BY8Zzml_.js.map
