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