import { jsx as r, jsxs as s } from "react/jsx-runtime";
import { useTranslateProvider as B } from "@/common/languageTranslator/TranslationContext";
import { s as T } from "./QueryBuilder-BPfavHH9-M3wgeHB3.js";
import d from "@/common/services/util/GeneralUtil";
import { showToast as g } from "@/common/toaster/toaster";
import L from "@/hooks/useTrackEvent";
import N from "@/views/custom-components/CustomTextArea";
import _ from "@/views/custom-components/CustomTextField";
import { yupResolver as j } from "@hookform/resolvers/yup";
import { Drawer as D, Box as z, Typography as q, FormControlLabel as n, Checkbox as c, Button as S } from "@mui/material";
import l from "@mui/material/Grid2";
import u from "prop-types";
import { useRef as Q, useEffect as M } from "react";
import { useForm as G, Controller as m } from "react-hook-form";
import * as p from "yup";
import './AddEditQueryBuilder-7JRplV5J.css';const O = p.object().shape({
  code: p.string().required("Enter code").trim().lowercase(),
  description: p.string().required("Enter Description").trim().lowercase(),
  query: p.string().required("Enter query").trim().lowercase()
});
function U({
  isEditMode: o,
  open: b,
  close: E,
  editRow: t,
  loadInitialState: R
}) {
  const C = Q(null), k = Q(!1), { translate: i } = B(), {
    control: a,
    handleSubmit: F,
    setValue: f,
    getValues: I,
    formState: { errors: y },
    reset: v
  } = G({
    resolver: j(O),
    mode: "onChange",
    defaultValues: {
      code: "",
      description: "",
      query: "",
      returnsResultSet: !1,
      isPublic: !1,
      isMobileQuery: !1,
      isClickhouseQuery: !1,
      ...d.isTecho && { isCached: !1 }
    }
  }), w = L(), h = () => {
    w("conversion_event", {
      form: C.current,
      form_name: "Add/Edit Query Builder",
      submit: k.current
    }), E();
  }, P = async () => {
    try {
      const e = I();
      w("button_click", {
        button_name: o && t ? "querybuilder-update-button" : "querybuilder-add-button",
        button_type: "submit",
        button_id: "addeditquerybuilder-save-button"
      }), o && t ? (t.query = e.query, t.code = e.code, t.description = e.description, t.returnsResultSet = e.returnsResultSet, t.isPublic = e.isPublic, t.isMobileQuery = e.isMobileQuery, t.isClickhouseQuery = e.isClickhouseQuery, d.isTecho && (t.isCached = e.isCached), await T(t), g({ message: "Query Updated SuccessFully", type: "success" })) : (await T(e), g({ message: "Query Created SuccessFully", type: "success" })), k.current = !0, h(), await R();
    } catch (e) {
      g({
        message: `${e.status === 400 ? "This code is already registered. Please enter a different code" : "Error in Query Generation"}`,
        type: "error"
      });
    }
  };
  return M(() => {
    o && Object.keys(t).forEach((e) => f(e, t == null ? void 0 : t[e]));
  }, [o, t, f]), M(() => {
    b || v();
  }, [b, v]), /* @__PURE__ */ r(
    D,
    {
      anchor: "right",
      open: b,
      onClose: h,
      className: "z-[1300]",
      "data-testid": "addEdit-queryBuilder",
      children: /* @__PURE__ */ s(l, { container: !0, className: "w-full md:w-[650px] max-w-full", ref: C, children: [
        /* @__PURE__ */ r(z, { className: "p-2.5 px-5 flex border-b w-full", children: /* @__PURE__ */ s(l, { className: "font-medium text-lg w-full text-customBlue", children: [
          i(o ? "Edit Configured" : "Configure New"),
          " ",
          i("Query")
        ] }) }),
        /* @__PURE__ */ s(l, { className: "p-3 grow", children: [
          /* @__PURE__ */ s(l, { size: { xs: 12 }, className: "pb-2", children: [
            /* @__PURE__ */ s(q, { variant: "caption", className: "pl-0", children: [
              i("Code"),
              /* @__PURE__ */ r("span", { className: "text-red-500 ml-1", children: "*" })
            ] }),
            /* @__PURE__ */ r(
              _,
              {
                control: a,
                id: "code",
                name: "code",
                errors: y,
                className: "m-0",
                label: "",
                size: "small",
                onBlur: (e) => {
                  const x = e.target.value;
                  if (x) {
                    const V = o ? x.trim() : x.trim().toUpperCase();
                    f("code", V);
                  }
                },
                dataTestId: "addeditquerybuilder-code-textfield"
              }
            )
          ] }),
          /* @__PURE__ */ s(l, { size: { xs: 12 }, className: "pb-2", children: [
            /* @__PURE__ */ s(
              q,
              {
                variant: "caption",
                className: "p-3 pl-0",
                "data-testid": "addeditquerybuilder-description-label",
                children: [
                  i("Description"),
                  /* @__PURE__ */ r("span", { className: "text-red-500 ml-1", children: "*" })
                ]
              }
            ),
            /* @__PURE__ */ r(
              N,
              {
                control: a,
                minRows: 1,
                id: "description",
                name: "description",
                errors: y,
                className: "pl-2.5 pt-2.5",
                "data-testid": "addeditquerybuilder-description-textarea"
              }
            )
          ] }),
          /* @__PURE__ */ s(l, { size: { xs: 12 }, className: "pb-2", children: [
            /* @__PURE__ */ s(q, { variant: "caption", className: "p-3 pl-0", children: [
              i("Query"),
              /* @__PURE__ */ r("span", { className: "text-red-500 ml-1", children: "*" })
            ] }),
            /* @__PURE__ */ r(
              N,
              {
                control: a,
                minRows: 5,
                id: "query",
                name: "query",
                errors: y,
                "data-testid": "addeditquerybuilder-query-textarea"
              }
            )
          ] }),
          d.isTecho && /* @__PURE__ */ r(l, { size: { xs: 12 }, children: /* @__PURE__ */ r(
            m,
            {
              name: "isCached",
              control: a,
              defaultValue: !1,
              render: ({ field: e }) => /* @__PURE__ */ r(
                n,
                {
                  control: /* @__PURE__ */ r(c, { ...e, checked: e.value }),
                  label: i("Is Cached"),
                  sx: {
                    "& .MuiFormControlLabel-label": {
                      fontSize: "14px"
                    },
                    marginTop: 0,
                    paddingTop: 0
                  },
                  "data-testid": "addeditquerybuilder-is-cached-checkbox"
                }
              )
            }
          ) }),
          /* @__PURE__ */ r(l, { size: { xs: 12 }, children: /* @__PURE__ */ r(
            m,
            {
              name: "returnsResultSet",
              control: a,
              defaultValue: !1,
              render: ({ field: e }) => /* @__PURE__ */ r(
                n,
                {
                  control: /* @__PURE__ */ r(c, { ...e, checked: e.value }),
                  label: i("Returns Result Set"),
                  sx: {
                    "& .MuiFormControlLabel-label": {
                      fontSize: "14px"
                    },
                    marginTop: 0,
                    paddingTop: 0
                  },
                  "data-testid": "addeditquerybuilder-returns-result-set-checkbox"
                }
              )
            }
          ) }),
          /* @__PURE__ */ r(l, { size: { xs: 12 }, children: /* @__PURE__ */ r(
            m,
            {
              name: "isPublic",
              control: a,
              defaultValue: !1,
              render: ({ field: e }) => /* @__PURE__ */ r(
                n,
                {
                  control: /* @__PURE__ */ r(c, { ...e, checked: e.value }),
                  label: i("Is Public"),
                  sx: {
                    "& .MuiFormControlLabel-label": {
                      fontSize: "14px"
                    },
                    marginTop: 0,
                    paddingTop: 0
                  },
                  "data-testid": "addeditquerybuilder-is-public-checkbox"
                }
              )
            }
          ) }),
          /* @__PURE__ */ r(l, { size: { xs: 12 }, children: /* @__PURE__ */ r(
            m,
            {
              name: "isMobileQuery",
              control: a,
              defaultValue: !1,
              render: ({ field: e }) => /* @__PURE__ */ r(
                n,
                {
                  control: /* @__PURE__ */ r(c, { ...e, checked: e.value }),
                  label: i("Is Mobile Query"),
                  sx: {
                    "& .MuiFormControlLabel-label": {
                      fontSize: "14px"
                    },
                    marginTop: 0,
                    paddingTop: 0
                  },
                  "data-testid": "addeditquerybuilder-is-mobilequery-checkbox"
                }
              )
            }
          ) }),
          (d.isEkavach || d.isPhmp) && /* @__PURE__ */ r(l, { size: { xs: 12 }, children: /* @__PURE__ */ r(
            m,
            {
              name: "isClickhouseQuery",
              control: a,
              defaultValue: !1,
              render: ({ field: e }) => /* @__PURE__ */ r(
                n,
                {
                  control: /* @__PURE__ */ r(c, { ...e, checked: e.value }),
                  label: i("Is Clickhouse Query"),
                  sx: {
                    "& .MuiFormControlLabel-label": {
                      fontSize: "14px"
                    },
                    marginTop: 0,
                    paddingTop: 0
                  },
                  "data-testid": "addeditquerybuilder-is-clickhousequery-checkbox"
                }
              )
            }
          ) })
        ] }),
        /* @__PURE__ */ s(z, { className: "p-3 w-full flex justify-end", children: [
          /* @__PURE__ */ r(
            S,
            {
              size: "small",
              variant: "contained",
              color: "secondary",
              onClick: h,
              "data-testid": "addeditquerybuilder-close-button",
              children: i("Cancel")
            }
          ),
          /* @__PURE__ */ r(
            S,
            {
              variant: "contained",
              size: "small",
              color: "primary",
              type: "submit",
              onClick: F(P),
              className: "ml-2.5",
              "data-testid": "addeditquerybuilder-save-button",
              children: i("Save")
            }
          )
        ] })
      ] })
    }
  );
}
U.propTypes = {
  isEditMode: u.any,
  open: u.any,
  close: u.any,
  editRow: u.any,
  loadInitialState: u.any
};
export {
  U as default
};
//# sourceMappingURL=AddEditQueryBuilder-7JRplV5J-DHgS6TYG.js.map
