import { jsx as i, jsxs as n } from "react/jsx-runtime";
import { useRef as z, useEffect as f, useState as S, Suspense as $, lazy as K } from "react";
import { Drawer as W, FormGroup as Z, FormControlLabel as ii, Checkbox as ti, FormControl as R, Typography as p, Box as q, Divider as ei, Stack as oi, Button as E } from "@mui/material";
import c from "@mui/material/Grid2";
import { useForm as ni, Controller as ai } from "react-hook-form";
import * as x from "yup";
import { yupResolver as ri } from "@hookform/resolvers/yup";
import si from "@/views/custom-components/CustomTextField";
import ci from "@/views/custom-components/CustomTextArea";
import o from "prop-types";
import { showToast as di } from "@/common/toaster/toaster";
import li from "@/hooks/useTrackEvent";
import { useTranslateProvider as mi } from "@/common/languageTranslator/TranslationContext";
import { faPen as fi, faTrash as pi } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon as k } from "@fortawesome/react-fontawesome";
import ui from "@/views/custom-components/CustomizedTable/CustomizedTableV2";
import './ConditionConfig.css';const gi = K(() => import("./TypeConfiguration-DYbBG_hZ.js"));
function hi({
  selectedConfig: u,
  newCondition: d,
  setNewConditionData: D,
  overlayOpen: C,
  handleOverlayState: I,
  addTypeConfig: M,
  setNewTypeData: O,
  typeConfigurationData: m,
  handleTypeConfigurationDelete: j,
  handleTypeConfigurationEdit: A,
  notificationtypeMap: v
}) {
  const T = z(null), { translate: r } = mi(), N = z(!1), F = li();
  f(() => {
    var t;
    d && ((t = Object.keys(d)) == null || t.forEach((a) => {
      d[a] && l(a, d[a]);
    }));
  }, [d]);
  const [P, _] = S([]), B = [
    { field: "type", label: "Type" },
    { field: "template", label: "Template" },
    { field: "Actions", label: "Actions" }
  ], L = [
    { label: "", icon: /* @__PURE__ */ i(k, { icon: fi }), onClick: (t, a) => {
      A(a);
    } },
    {
      label: "",
      icon: /* @__PURE__ */ i(k, { icon: pi }),
      onClick: (t, a) => {
        j(a);
      }
    }
  ];
  f(() => {
    const t = JSON.parse(JSON.stringify(m));
    let a = t == null ? void 0 : t.map((Y) => {
      const e = { ...Y };
      return (e == null ? void 0 : e.type) === "EXCEPTION" && e.exceptionMessage && (e.template = e.exceptionMessage), (e == null ? void 0 : e.type) === "QUERY" && e.queryCode && (e.template = e.queryCode), (e == null ? void 0 : e.type) === "MOBILE" && v[e.mobileNotificationType] && (e.template = v[e.mobileNotificationType]), e;
    });
    _(a || []);
  }, [m]);
  const [g, V] = S(null), G = x.object().shape({
    condition: x.string().test("is-required", "Condition is required", function(t) {
      return s ? !!t : !0;
    }).test("max-length", "Character limit exceeds", function(t) {
      return s ? (t == null ? void 0 : t.length) <= 500 : !0;
    }),
    description: x.string().max(500, "Max 500 characters allowed")
  }), {
    control: h,
    handleSubmit: J,
    watch: H,
    reset: y,
    setValue: l,
    resetField: Q,
    formState: { errors: w }
  } = ni({
    defaultValues: { newCondition: d, isConditionReq: !1 },
    resolver: ri(G),
    mode: "onChange"
  }), s = H("isConditionReq");
  f(() => {
    s || Q("condition");
  }, [s]);
  const b = () => {
    F("conversion_event", {
      form: T.current,
      form_name: "Condition Config Events",
      submit: N.current
    }), I();
  }, U = (t) => {
    t.condition || m.length > 0 ? (D({
      ...t,
      notificaitonConfigsType: m
    }), b(), N.current = !0, y()) : di({
      message: "To save please add atleast a condition or type for configuration.",
      type: "error"
    });
  }, X = () => {
    M();
  };
  return f(() => {
    g && O({ type: g });
  }, [g]), /* @__PURE__ */ i("div", { className: "side-overlay", "data-testid": "conditionconfig-side-overlay", children: /* @__PURE__ */ n(
    W,
    {
      classes: {
        paper: `overlay-condition ${C ? "w-full md:w-[650px] max-w-full" : ""}`
      },
      anchor: "right",
      open: C,
      className: "z-[1300]",
      onClose: () => {
        b(), y();
      },
      "data-testid": "conditionconfig-drawer-overlay",
      children: [
        /* @__PURE__ */ n(
          "div",
          {
            className: "p-2.5 px-5 flex border-b font-medium text-lg text-customBlue",
            "data-testid": "conditionconfig-overlay-header",
            children: [
              r("Add"),
              " ",
              r("Field")
            ]
          }
        ),
        /* @__PURE__ */ i(
          "form",
          {
            ref: T,
            onSubmit: J(U),
            "data-testid": "conditionconfig-form",
            children: /* @__PURE__ */ n(c, { container: !0, spacing: 2, className: "p-2", children: [
              /* @__PURE__ */ i(c, { item: !0, size: { xs: 12 }, children: /* @__PURE__ */ i(
                Z,
                {
                  className: "form-group",
                  "data-testid": "conditionconfig-form-group",
                  children: /* @__PURE__ */ i(
                    ai,
                    {
                      name: "isConditionReq",
                      control: h,
                      defaultValue: !1,
                      render: ({ field: t }) => /* @__PURE__ */ i(
                        ii,
                        {
                          control: /* @__PURE__ */ i(
                            ti,
                            {
                              ...t,
                              checked: t.value,
                              onChange: () => (l("condition", ""), l("isConditionReq", !s)),
                              color: "primary"
                            }
                          ),
                          label: "Is Condition Required",
                          "data-testid": "conditionconfig-checkbox-is-condition-required",
                          sx: {
                            "& .MuiFormControlLabel-label": {
                              fontSize: "13px"
                            }
                          },
                          onChange: () => {
                            if (t.value)
                              return l("condition", ""), l("isConditionReq", !s);
                          }
                        }
                      )
                    }
                  )
                }
              ) }),
              s && /* @__PURE__ */ i(
                c,
                {
                  size: {
                    xs: 12
                  },
                  className: "flex flex-col items-start",
                  "data-testid": "conditionconfig-condition-grid",
                  children: /* @__PURE__ */ n(
                    R,
                    {
                      variant: "outlined",
                      className: "w-full",
                      "data-testid": "conditionconfig-form-control-condition",
                      children: [
                        /* @__PURE__ */ n(p, { variant: "body2", children: [
                          r("Condition"),
                          /* @__PURE__ */ i("span", { className: "text-red-500 ml-1", children: "*" })
                        ] }),
                        /* @__PURE__ */ i(
                          q,
                          {
                            className: "w-full",
                            "data-testid": "conditionconfig-condition-box",
                            children: /* @__PURE__ */ i(
                              si,
                              {
                                control: h,
                                id: "condition",
                                name: "condition",
                                errors: w,
                                label: "",
                                size: "small",
                                dataTestId: "conditionconfig-custom-text-field-condition"
                              }
                            )
                          }
                        ),
                        /* @__PURE__ */ i(
                          "div",
                          {
                            className: "whitespace-normal w-full break-words",
                            "data-testid": "conditionconfig-implicit-param",
                            children: /* @__PURE__ */ i("b", { children: u == null ? void 0 : u.implicitParam })
                          }
                        )
                      ]
                    }
                  )
                }
              ),
              /* @__PURE__ */ i(
                c,
                {
                  item: !0,
                  size: { xs: 12 },
                  "data-testid": "conditionconfig-description-grid",
                  children: /* @__PURE__ */ n(R, { variant: "outlined", className: "w-full", children: [
                    /* @__PURE__ */ n(
                      p,
                      {
                        "data-testid": "conditionconfig-description-label",
                        variant: "body2",
                        children: [
                          " ",
                          r("Description")
                        ]
                      }
                    ),
                    /* @__PURE__ */ i(
                      q,
                      {
                        className: "w-full",
                        "data-testid": "conditionconfig-description-box",
                        children: /* @__PURE__ */ i(
                          ci,
                          {
                            control: h,
                            id: "description",
                            name: "description",
                            errors: w,
                            label: "",
                            minRows: 5,
                            "data-testid": "conditionconfig-custom-text-area-description"
                          }
                        )
                      }
                    )
                  ] })
                }
              ),
              /* @__PURE__ */ n(
                c,
                {
                  item: !0,
                  size: { xs: 12 },
                  "data-testid": "conditionconfig-type-configuration-grid",
                  children: [
                    /* @__PURE__ */ i(ei, { className: "m-[20px_3px]" }),
                    /* @__PURE__ */ n(
                      p,
                      {
                        className: "mb-5",
                        "data-testid": "conditionconfig-type-configuration-label",
                        variant: "body2",
                        children: [
                          r("Type"),
                          " ",
                          r("Configuration")
                        ]
                      }
                    ),
                    /* @__PURE__ */ i($, { children: /* @__PURE__ */ i(
                      gi,
                      {
                        setType: V,
                        onSubmit: X,
                        "data-testid": "conditionconfig-type-configuration-form"
                      }
                    ) })
                  ]
                }
              ),
              /* @__PURE__ */ i(
                c,
                {
                  item: !0,
                  size: { xs: 12 },
                  "data-testid": "conditionconfig-table-grid",
                  children: /* @__PURE__ */ i(
                    ui,
                    {
                      columns: B,
                      tableData: P,
                      disableHtml: !0,
                      actions: L,
                      paginationConfig: { pagination: !1 },
                      emptyMessage: "No Configuration added",
                      dataTestId: "conditionconfig-customized-table"
                    }
                  )
                }
              ),
              /* @__PURE__ */ i(
                c,
                {
                  item: !0,
                  size: { xs: 12 },
                  "data-testid": "conditionconfig-buttons-grid",
                  children: /* @__PURE__ */ n(
                    oi,
                    {
                      direction: "row",
                      className: "mr-5",
                      spacing: 2,
                      justifyContent: "flex-end",
                      mt: 1,
                      mb: 1,
                      children: [
                        /* @__PURE__ */ i(
                          E,
                          {
                            variant: "contained",
                            color: "secondary",
                            size: "small",
                            onClick: () => {
                              b(), y();
                            },
                            "data-testid": "conditionconfig-button-close",
                            children: /* @__PURE__ */ i(
                              p,
                              {
                                variant: "body2",
                                "data-testid": "conditionconfig-typography-close",
                                children: r("Cancel")
                              }
                            )
                          }
                        ),
                        /* @__PURE__ */ i(
                          E,
                          {
                            size: "small",
                            type: "submit",
                            variant: "contained",
                            color: "primary",
                            "data-testid": "conditionconfig-button-save",
                            children: r("Save")
                          }
                        )
                      ]
                    }
                  )
                }
              )
            ] })
          }
        )
      ]
    }
  ) });
}
hi.propTypes = {
  selectedConfig: o.any,
  newCondition: o.any,
  setNewConditionData: o.any,
  overlayOpen: o.any,
  handleOverlayState: o.any,
  addTypeConfig: o.any,
  setNewTypeData: o.any,
  typeConfigurationData: o.any,
  handleTypeConfigurationDelete: o.any,
  handleTypeConfigurationEdit: o.any,
  notificationtypeMap: o.any
};
export {
  hi as default
};
//# sourceMappingURL=ConditionConfig-DB0FqwgG.js.map
