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