import { jsx as e, jsxs as l, Fragment as Qe } from "react/jsx-runtime";
import { useState as w, useEffect as pe, Suspense as We, lazy as Ye } from "react";
import { Card as He, CardHeader as Z, Box as _, Typography as m, CardContent as Ze, FormControl as fe, Select as he, MenuItem as B, Chip as D, Button as M, Divider as K, Tooltip as Ke, IconButton as Xe, Autocomplete as ea, TextField as aa } from "@mui/material";
import { showToast as E } from "@/common/toaster/toaster";
import { execute as L } from "@/common/services/GlobalApis";
import u from "@mui/material/Grid2";
import { useParams as sa, useNavigate as ra } from "react-router";
import { useForm as la, useFieldArray as be, FormProvider as na, Controller as X, get as j } from "react-hook-form";
import ta from "@/views/custom-components/CustomCheckBox";
import { yupResolver as ia } from "@hookform/resolvers/yup";
import { useTranslateProvider as oa } from "@/common/languageTranslator/TranslationContext";
import ca from "@/views/custom-components/CustomAutoComplete";
import * as b from "yup";
import k from "@/views/custom-components/CustomTextField";
import { u as da, M as ma } from "./ImmunizationSkeleton-D2tdv5k-.js";
import { faTrash as ge, faInfoCircle as ua } from "@fortawesome/free-solid-svg-icons";
import { useSelector as pa } from "react-redux";
import { validateAOI as fa } from "@/common/services/Users";
import { FontAwesomeIcon as ee } from "@fortawesome/react-fontawesome";
import './manageImmunization.css';const ha = Ye(
  () => import("@/views/feature-components/LocationDirective/LocationDirectiveV2")
), Fa = () => {
  var de;
  const { id: R } = sa(), { translate: o } = oa(), { user: P } = pa((a) => a.Authenticate), G = ra(), { t: ye } = da(), [A, ve] = w("days"), [U, xe] = w([]), [F, Ne] = w("days"), [O, we] = w("days"), [Ce, _e] = w("Add Immunization"), [ke, Ie] = w([]), ze = o("Requirement"), De = o("Min Gap (In Days)"), qe = o("Max Gap  (In Days)"), Se = o("Message"), [Re, Ae] = w([]), Le = o("Select Vaccines"), [q, ae] = w({});
  let Fe = 10;
  const [C, V] = w([]), [se, Q] = w(!1), [re, Te] = w([]), I = (a, s) => {
    const n = Ee();
    if (s === "fromDate") {
      const c = n == null ? void 0 : n.fromDate, d = JSON.parse(JSON.stringify(A)), f = z(c, d, a);
      v("fromDate", parseFloat(f.toFixed(1))), ve(a);
    }
    if (s === "toDate") {
      const c = n == null ? void 0 : n.toDate, d = JSON.parse(JSON.stringify(F)), f = z(c, d, a);
      v("toDate", parseFloat(f.toFixed(2))), Ne(a);
    }
    if (s === "gapRange") {
      const c = n == null ? void 0 : n.gapRange, d = JSON.parse(JSON.stringify(O)), f = z(c, d, a);
      v("gapRange", parseFloat(f.toFixed(2))), we(a);
    }
  }, z = (a, s, n) => {
    if (!a)
      return 0;
    let c;
    switch (s) {
      case "months":
        c = a * 30;
        break;
      case "years":
        c = a * 365;
        break;
      default:
        c = Math.round(a);
    }
    let d;
    switch (n) {
      case "months":
        d = c / 30;
        break;
      case "years":
        d = c / 365;
        break;
      default:
        d = Math.round(c);
    }
    return d;
  }, Oe = async (a) => {
    var s, n, c;
    try {
      const f = {
        dependencies: ((s = a == null ? void 0 : a.dependencies) == null ? void 0 : s.map(
          ({ min_gap_days: N, max_gap_days: y, required: r, vaccine: S, ...T }) => ({
            ...T,
            vaccine: S.map((h) => h.code),
            ...r !== "false" && { min_gap_days: N, max_gap_days: y },
            required: r
          })
        )) || []
      }, i = (a.filters || []).map((N) => ({
        filterName: N.filterName,
        queryCode: N.queryCode
      })), t = {
        ...(a == null ? void 0 : a.isRecursive) && {
          gapRange: a == null ? void 0 : a.gapRange,
          maxDoses: a.maxDoses
        }
      }, x = {
        code: a == null ? void 0 : a.code,
        name: a == null ? void 0 : a.name,
        isrecursive: a == null ? void 0 : a.isRecursive,
        fromAgeDays: parseInt(
          z(a == null ? void 0 : a.fromDate, A, "days")
        ),
        toAgeDays: parseInt(z(a == null ? void 0 : a.toDate, F, "days")),
        validations: JSON.stringify(f),
        filters: JSON.stringify(i),
        basedate: a == null ? void 0 : a.basedate,
        recursiveobject: JSON.stringify(t),
        locations: C ? JSON.stringify(C) : null,
        active: !0
      };
      R ? (await L({
        code: "update_immunization",
        parameters: { ...x, id: parseInt(R) }
      }), E({
        message: "Immunization Updated Successfully!",
        type: "success"
      }), G(-1)) : ((c = (n = (await L({
        code: "check_existing_code",
        parameters: { code: a == null ? void 0 : a.code }
      })).data) == null ? void 0 : n.result) == null ? void 0 : c.length) > 0 ? E({ message: "Code Already Exist", type: "error" }) : (await L({ code: "add_immunization", parameters: x }), E({
        message: "Immunization Added Successfully",
        type: "success"
      }), G(-1));
    } catch (d) {
      console.error(d), E({ message: "Error in adding Immunization", type: "error" });
    }
  }, $e = b.object().shape({
    code: b.string().required("Code is required"),
    // Code is required
    name: b.string().required("Name is required"),
    // Name is required
    basedate: b.string().required("Base date (name) is required"),
    // Basedate is required
    fromDate: b.number().transform(
      (a, s) => s === "" ? void 0 : a
    ).nullable().min(0, "From date must be 0 or a positive number"),
    toDate: b.number().transform(
      (a, s) => s === "" ? void 0 : a
    ).nullable().min(0, "To date must be 0 or a positive number").when("fromDate", (a, s) => s.test(
      "check-to-date",
      "To date must be greater than From date",
      (n) => n === 0 ? !0 : !(z(a, A, "days") !== void 0 && a > 0 && z(n, F, "days") <= z(a, A, "days"))
    )),
    gapRange: b.number().transform(
      (a, s) => s === "" ? void 0 : a
    ).nullable().min(0, "Must be 0 or a positive number").test(
      "is-less-than-6-months",
      "Gap range must be less than 6 months",
      function(a) {
        return a == null ? !0 : z(a, O, "days") < 182;
      }
    ),
    maxDoses: b.number().transform(
      (a, s) => s === "" ? void 0 : a
    ).nullable().min(0, "Must be 0 or a positive number"),
    dependencies: b.array().of(
      b.object().shape({
        vaccine: b.array().min(1, "At least one vaccine is required").required("Vaccine is required"),
        required: b.string().required("Please Select a vaccine"),
        min_gap_days: b.number().min(0, "No negative number").transform(
          (a, s) => s === "" ? void 0 : a
        ).nullable(),
        max_gap_days: b.number().min(0, "No negative number").transform(
          (a, s) => s === "" ? void 0 : a
        ).nullable().when("min_gap_days", (a, s) => s.test(
          "check-to-date",
          "Max days must be greater",
          (n) => n === 0 ? !0 : !(a > 0 && n <= a)
        )),
        message: b.string().nullable()
      })
    ),
    filters: b.array().of(
      b.object().shape({
        filterName: b.string().required("Filter name is required"),
        queryCode: b.string().required("Please select a query")
      })
    )
  }), le = la({
    defaultValues: {
      code: "",
      name: "",
      basedate: "",
      fromDate: 0,
      toDate: 0,
      isRecursive: !1,
      gapRange: 0,
      maxDoses: 0,
      dependencies: [],
      filters: []
    },
    resolver: ia($e),
    mode: "onChange"
  }), {
    control: g,
    handleSubmit: Me,
    setValue: v,
    getValues: Ee,
    clearErrors: je,
    watch: W,
    formState: { errors: p }
  } = le, { fields: ne, append: te, remove: Je } = be({
    control: g,
    name: "dependencies"
  }), {
    fields: ie,
    append: oe,
    remove: Be
  } = be({
    control: g,
    name: "filters"
  }), $ = W("dependencies"), Y = W("isRecursive"), ce = W("name");
  pe(() => {
    if (!R) {
      const a = ce.replace(/\s+/g, "_").toUpperCase();
      v("code", a);
    }
  }, [ce, R]), pe(() => {
    Pe();
  }, []);
  const Pe = async () => {
    var a, s, n, c, d;
    try {
      Q((y) => !y);
      const f = await L({
        code: "imt_member_date_column"
      });
      xe((a = f == null ? void 0 : f.data) == null ? void 0 : a.result);
      const i = await L({
        code: "get_code_immunization",
        parameters: {}
      });
      let t = (s = i == null ? void 0 : i.data) == null ? void 0 : s.result;
      Ie(t);
      const x = await L({
        code: "GET_MANAGE_IMMUNIZATION_FILTER_QUERIES",
        parameters: {}
      }), N = Object.values(
        (((n = x == null ? void 0 : x.data) == null ? void 0 : n.result) || []).reduce((y, r) => (y[r.queryCode] = r, y), {})
      );
      if (Te(N), R) {
        _e("Edit Immunization");
        const y = await L({
          code: "get_immunization_by_id",
          parameters: {
            id: parseInt(R)
          }
        }), r = (c = y == null ? void 0 : y.data) == null ? void 0 : c.result[0];
        if (v("code", r == null ? void 0 : r.code), v("name", r == null ? void 0 : r.name), v("basedate", r == null ? void 0 : r.base_date), v("fromDate", r == null ? void 0 : r.from_age_days), v("toDate", r == null ? void 0 : r.to_age_days), v("isRecursive", r == null ? void 0 : r.is_recursive), V(r != null && r.locations ? JSON.parse(r == null ? void 0 : r.locations) : []), r.is_recursive) {
          const h = JSON.parse(r == null ? void 0 : r.recursive_object);
          v("gapRange", h == null ? void 0 : h.gapRange), v("maxDoses", h == null ? void 0 : h.maxDoses);
        }
        const S = JSON.parse(r == null ? void 0 : r.validations);
        S && ((d = S == null ? void 0 : S.dependencies) == null || d.forEach((h) => {
          const J = ((h == null ? void 0 : h.vaccine) || []).map((ue) => {
            const H = t.find((Ve) => Ve.code === ue);
            return {
              code: ue,
              name: (H == null ? void 0 : H.name) || "Unknown"
              // Fallback to 'Unknown' if no match is found
            };
          });
          h.vaccine = J, te(h);
        }));
        const T = r != null && r.filters ? JSON.parse(r.filters) : [];
        T && T.length > 0 && T.forEach((h) => {
          oe({
            filterName: h.filterName || "",
            queryCode: h.queryCode || ""
          });
        });
      }
      Q((y) => !y);
    } catch (f) {
      console.error(f), Q((i) => !i);
    }
  }, Ge = async () => {
    var a, s, n, c, d;
    try {
      const f = (s = (a = q == null ? void 0 : q.finalSelected) == null ? void 0 : a.optionSelected) == null ? void 0 : s.id, i = q.finalSelected.level, { id: t } = q.finalSelected.optionSelected;
      let x = !1, N = [], y = {
        ids: C.map((r) => r.locationId),
        idArr: C.map((r) => r.locationIdArr)
      };
      for (let r = 1; r <= i; r++)
        (c = y.ids) != null && c.includes(
          (n = q[`level${r}`]) == null ? void 0 : n.id
        ) ? x = !0 : N.push((d = q[`level${r}`]) == null ? void 0 : d.id);
      if (x)
        E({
          message: "Please select different parent location!",
          type: "info"
        });
      else {
        const { data: r } = await fa({
          roleId: P == null ? void 0 : P.roleId,
          toBeAdded: t
        });
        V((S) => {
          var h;
          return [
            ...S.filter(
              (me) => {
                var J;
                return !((J = me.locationIdArr) != null && J.includes(f));
              }
            ),
            {
              locationId: f,
              locationIdArr: N,
              locationList: (h = r == null ? void 0 : r.message) == null ? void 0 : h.replaceAll(",", " => ")
            }
          ];
        }), ae({
          finalSelected: null,
          optionsList: []
        });
      }
    } catch (f) {
      console.error(f);
    }
  }, Ue = (a) => {
    V((s) => s.filter((n) => n.locationId !== a));
  };
  return /* @__PURE__ */ e(na, { ...le, children: /* @__PURE__ */ e(
    He,
    {
      className: "mt-6 mr-3 mb-3",
      elevation: 5,
      component: "form",
      onSubmit: Me(Oe),
      noValidate: !0,
      children: /* @__PURE__ */ l(ma, { isLoading: se, children: [
        /* @__PURE__ */ e(u, { item: !0, container: !0, direction: "row", children: /* @__PURE__ */ e(u, { item: !0, size: { xs: 12 }, children: /* @__PURE__ */ e(
          Z,
          {
            className: "p-2.5 px-5 flex border-b",
            subheader: /* @__PURE__ */ e(
              _,
              {
                display: "flex",
                alignItems: "center",
                justifyContent: "space-between",
                children: /* @__PURE__ */ e(
                  m,
                  {
                    variant: "body2",
                    className: "font-bold text-lg text-customBlue",
                    "data-testid": "form-header",
                    children: ye(Ce)
                  }
                )
              }
            ),
            slotProps: {
              subheader: { className: "subheader-style" }
            }
          }
        ) }) }),
        /* @__PURE__ */ l(Ze, { children: [
          /* @__PURE__ */ l(u, { container: !0, spacing: 2, children: [
            /* @__PURE__ */ e(u, { className: "w-full", children: /* @__PURE__ */ l(
              u,
              {
                container: !0,
                spacing: 2,
                className: "flex flex-col justify-between  w-full",
                children: [
                  /* @__PURE__ */ l("div", { className: "flex md:flex-row flex-col space-x-10 w-[100%]", children: [
                    /* @__PURE__ */ l("div", { className: "flex flex-col space-y-2 w-full md:w-1/2", children: [
                      /* @__PURE__ */ l(u, { className: "mb-3 mr-[5rem] w-full", children: [
                        /* @__PURE__ */ l(
                          m,
                          {
                            variant: "caption",
                            className: "p-3 pl-0 text-base",
                            children: [
                              o("Immunization Name"),
                              /* @__PURE__ */ e("span", { className: "text-red-500 ml-1", children: "*" })
                            ]
                          }
                        ),
                        /* @__PURE__ */ e(
                          k,
                          {
                            control: g,
                            id: "name",
                            name: "name",
                            className: "w-full",
                            errors: p,
                            label: "",
                            size: "small"
                          }
                        )
                      ] }),
                      /* @__PURE__ */ l(u, { className: "mb-3 mr-[5rem] w-full", children: [
                        /* @__PURE__ */ l(
                          m,
                          {
                            variant: "caption",
                            className: "p-3 pl-0 text-base",
                            children: [
                              o("Immunization Code"),
                              /* @__PURE__ */ e("span", { className: "text-red-500 ml-1", children: "*" })
                            ]
                          }
                        ),
                        /* @__PURE__ */ e(
                          k,
                          {
                            control: g,
                            id: "code",
                            name: "code",
                            className: "w-full",
                            errors: p,
                            label: "",
                            size: "small",
                            disabled: !!R
                          }
                        )
                      ] }),
                      /* @__PURE__ */ l(u, { className: "mb-3 mr-[5rem] w-full", children: [
                        /* @__PURE__ */ e("div", { className: "flex gap-2 items-center", children: /* @__PURE__ */ l(
                          m,
                          {
                            variant: "caption",
                            className: "p-3 pl-0 text-base",
                            children: [
                              o("Base Date"),
                              /* @__PURE__ */ e("span", { className: "text-red-500 ml-1", children: "*" })
                            ]
                          }
                        ) }),
                        /* @__PURE__ */ e(_, { className: "w-full", children: /* @__PURE__ */ l(fe, { fullWidth: !0, children: [
                          /* @__PURE__ */ e(
                            X,
                            {
                              className: "w-full",
                              name: "basedate",
                              control: g,
                              defaultValue: "",
                              render: ({ field: a }) => /* @__PURE__ */ e(
                                he,
                                {
                                  ...a,
                                  className: "w-full",
                                  sx: {
                                    height: 38
                                  },
                                  error: p.basedate,
                                  "data-testid": "mobiletypeeventconfigmodal-notification-select",
                                  children: U == null ? void 0 : U.map((s, n) => /* @__PURE__ */ e(
                                    B,
                                    {
                                      value: s.column_name,
                                      children: /* @__PURE__ */ e(m, { variant: "body2", children: s.column_name })
                                    },
                                    n + 1
                                  ))
                                }
                              )
                            }
                          ),
                          (p == null ? void 0 : p.basedate) && /* @__PURE__ */ e("p", { className: "error-helper-text-immunization", children: (de = p == null ? void 0 : p.basedate) == null ? void 0 : de.message })
                        ] }) })
                      ] }),
                      /* @__PURE__ */ l(u, { className: "mb-3 mr-[5rem] w-full", children: [
                        /* @__PURE__ */ l("div", { className: "flex flex-col md:flex-row md:items-center gap-2 w-full", children: [
                          /* @__PURE__ */ e(
                            m,
                            {
                              variant: "caption",
                              className: "text-base md:whitespace-nowrap",
                              children: o("Eligible From ")
                            }
                          ),
                          /* @__PURE__ */ l(u, { className: "flex gap-2 flex-wrap", children: [
                            /* @__PURE__ */ e(
                              D,
                              {
                                size: "small",
                                label: "Days",
                                clickable: !0,
                                color: A === "days" ? "primary" : "default",
                                onClick: () => I("days", "fromDate")
                              }
                            ),
                            /* @__PURE__ */ e(
                              D,
                              {
                                size: "small",
                                label: "Months",
                                clickable: !0,
                                color: A === "months" ? "primary" : "default",
                                onClick: () => I("months", "fromDate")
                              }
                            ),
                            /* @__PURE__ */ e(
                              D,
                              {
                                size: "small",
                                label: "Years",
                                clickable: !0,
                                color: A === "years" ? "primary" : "default",
                                onClick: () => I("years", "fromDate")
                              }
                            )
                          ] })
                        ] }),
                        /* @__PURE__ */ e(
                          k,
                          {
                            control: g,
                            id: "fromDate",
                            name: "fromDate",
                            type: "number",
                            errors: p,
                            label: "",
                            size: "small"
                          }
                        )
                      ] }),
                      /* @__PURE__ */ l(u, { className: "mb-3 mr-[5rem] w-full", children: [
                        /* @__PURE__ */ l("div", { className: "flex flex-col md:flex-row md:items-center gap-2 w-full", children: [
                          /* @__PURE__ */ e(
                            m,
                            {
                              variant: "caption",
                              className: "text-base md:whitespace-nowrap",
                              children: o("Eligible To")
                            }
                          ),
                          /* @__PURE__ */ l("div", { className: "flex gap-2 flex-wrap", children: [
                            /* @__PURE__ */ e(
                              D,
                              {
                                size: "small",
                                label: "Days",
                                clickable: !0,
                                color: F === "days" ? "primary" : "default",
                                onClick: () => I("days", "toDate")
                              }
                            ),
                            /* @__PURE__ */ e(
                              D,
                              {
                                size: "small",
                                label: "Months",
                                clickable: !0,
                                color: F === "months" ? "primary" : "default",
                                onClick: () => I("months", "toDate")
                              }
                            ),
                            /* @__PURE__ */ e(
                              D,
                              {
                                size: "small",
                                label: "Years",
                                clickable: !0,
                                color: F === "years" ? "primary" : "default",
                                onClick: () => I("years", "toDate")
                              }
                            )
                          ] })
                        ] }),
                        /* @__PURE__ */ e(
                          k,
                          {
                            control: g,
                            id: "toDate",
                            name: "toDate",
                            type: "number",
                            errors: p,
                            label: "",
                            size: "small"
                          }
                        )
                      ] })
                    ] }),
                    /* @__PURE__ */ l(u, { className: "w-full md:w-1/2", children: [
                      /* @__PURE__ */ e(We, { children: /* @__PURE__ */ e(
                        ha,
                        {
                          selectedLocation: q,
                          setSelectedLocation: ae,
                          locations: Re,
                          setLocations: Ae,
                          fetchUptoLevel: Fe,
                          templateType: "inline",
                          fetchAccordingToUserAoi: !0,
                          demographicFilterRequired: !1,
                          requiredUptoLevel: 1,
                          childForm: !0,
                          disabled: se
                        }
                      ) }),
                      /* @__PURE__ */ e(
                        M,
                        {
                          variant: "contained",
                          color: "primary",
                          size: "small",
                          onClick: Ge,
                          className: "mt-[1rem]",
                          children: o("Add Location")
                        }
                      ),
                      /* @__PURE__ */ l("div", { className: "w-full mt-8 p-6 bg-background2 border border-gray-300 rounded-2xl shadow-lg", children: [
                        /* @__PURE__ */ e("h2", { className: "text-xl font-semibold mb-4 text-customBlue", children: "📍 Selected Locations" }),
                        /* @__PURE__ */ l("ul", { className: "", children: [
                          C == null ? void 0 : C.map((a) => /* @__PURE__ */ l("li", { children: [
                            /* @__PURE__ */ e("span", { className: "text-xl leading-none", children: "• " }),
                            /* @__PURE__ */ e("span", { children: a != null && a.locationList ? a == null ? void 0 : a.locationList : "" }),
                            /* @__PURE__ */ e(
                              "button",
                              {
                                onClick: () => Ue(a == null ? void 0 : a.locationId),
                                className: "mx-3 cursor-pointer text-red-500 hover:text-red-700 font-bold text-xl",
                                children: "×"
                              }
                            )
                          ] }, a == null ? void 0 : a.locationId)),
                          (C == null ? void 0 : C.length) === 0 && /* @__PURE__ */ e("li", { className: "text-gray-400 italic text-center", children: "No locations selected." })
                        ] })
                      ] })
                    ] })
                  ] }),
                  /* @__PURE__ */ l(u, { className: "flex flex-col md:flex-row space-x-10 w-full", children: [
                    /* @__PURE__ */ e(u, { className: "mb-3 mt-8", children: /* @__PURE__ */ e(
                      ta,
                      {
                        name: "isRecursive",
                        sx: { "& .MuiSvgIcon-root": { fontSize: 20 } },
                        label: o("Repeats"),
                        control: g,
                        labelSizeInRem: "1rem",
                        checked: !!Y
                      }
                    ) }),
                    Y && /* @__PURE__ */ l(u, { className: "mb-3", children: [
                      /* @__PURE__ */ l("div", { className: "flex flex-col md:flex-row md:items-center gap-2 w-full", children: [
                        /* @__PURE__ */ e(
                          m,
                          {
                            variant: "caption",
                            className: "text-base md:whitespace-nowrap",
                            children: o("Interval")
                          }
                        ),
                        /* @__PURE__ */ l("div", { className: "flex gap-2 flex-wrap", children: [
                          /* @__PURE__ */ e(
                            D,
                            {
                              size: "small",
                              label: "Days",
                              clickable: !0,
                              color: O === "days" ? "primary" : "default",
                              onClick: () => I("days", "gapRange")
                            }
                          ),
                          /* @__PURE__ */ e(
                            D,
                            {
                              size: "small",
                              label: "Months",
                              clickable: !0,
                              color: O === "months" ? "primary" : "default",
                              onClick: () => I("months", "gapRange")
                            }
                          ),
                          /* @__PURE__ */ e(
                            D,
                            {
                              size: "small",
                              label: "Years",
                              clickable: !0,
                              color: O === "years" ? "primary" : "default",
                              onClick: () => I("years", "gapRange")
                            }
                          )
                        ] })
                      ] }),
                      /* @__PURE__ */ e(
                        k,
                        {
                          control: g,
                          id: "gapRange",
                          name: "gapRange",
                          type: "number",
                          className: "w-full",
                          errors: p,
                          label: "",
                          size: "small"
                        }
                      )
                    ] }),
                    Y && /* @__PURE__ */ l(u, { className: "mb-3", children: [
                      /* @__PURE__ */ e("div", { className: "flex flex-col md:flex-row md:items-center gap-2 w-full", children: /* @__PURE__ */ l(
                        m,
                        {
                          variant: "caption",
                          className: " pl-0 text-base",
                          children: [
                            o("Max Doses"),
                            /* @__PURE__ */ e("span", { className: "text-red-500 ml-1", children: "*" })
                          ]
                        }
                      ) }),
                      /* @__PURE__ */ e(
                        k,
                        {
                          control: g,
                          id: "maxDoses",
                          name: "maxDoses",
                          type: "number",
                          className: "w-full",
                          errors: p,
                          label: "",
                          size: "small"
                        }
                      )
                    ] })
                  ] }),
                  /* @__PURE__ */ l(u, { size: { xs: 12 }, className: "mb-3", children: [
                    /* @__PURE__ */ e(K, {}),
                    /* @__PURE__ */ l(_, { className: "flex flex-row items-center justify-between", children: [
                      /* @__PURE__ */ e(
                        Z,
                        {
                          subheader: /* @__PURE__ */ e(
                            _,
                            {
                              display: "flex",
                              alignItems: "center",
                              justifyContent: "space-between",
                              children: /* @__PURE__ */ e(
                                m,
                                {
                                  variant: "body2",
                                  className: "font-bold text-lg text-customBlue",
                                  "data-testid": "form-header",
                                  children: o("Add Validations")
                                }
                              )
                            }
                          ),
                          slotProps: {
                            subheader: {
                              variant: "p",
                              sx: { fontWeight: "bold" }
                            }
                          }
                        }
                      ),
                      /* @__PURE__ */ e(
                        M,
                        {
                          variant: "contained",
                          size: "small",
                          className: "mr-2",
                          onClick: () => te({
                            vaccine: [],
                            min_gap_days: 0,
                            max_gap_days: 0,
                            message: "",
                            required: ""
                          }),
                          children: o("Add")
                        }
                      )
                    ] }),
                    ne.map((a, s) => {
                      var n, c, d, f, i;
                      return /* @__PURE__ */ e(
                        _,
                        {
                          sx: {
                            display: "flex",
                            justifyContent: "space-between",
                            mb: 2,
                            alignItems: "flex-start",
                            border: "1px solid var(--color-gray3)",
                            backgroundColor: "var(--color-background)",
                            padding: 2,
                            borderRadius: 2,
                            position: "relative",
                            marginLeft: "15px"
                          },
                          children: /* @__PURE__ */ e("div", { className: "w-full", children: /* @__PURE__ */ l("div", { className: "flex flex-wrap gap-4 items-center w-full", children: [
                            /* @__PURE__ */ l("div", { className: "w-full md:w-[20%]", children: [
                              /* @__PURE__ */ e(
                                m,
                                {
                                  variant: "caption",
                                  className: "pb-1 text-base block",
                                  children: Le
                                }
                              ),
                              /* @__PURE__ */ e(
                                ca,
                                {
                                  name: `dependencies.${s}.vaccine`,
                                  options: ke,
                                  checkEquality: "code",
                                  className: "w-full",
                                  errors: p,
                                  getOptionLabel: (t) => t.name,
                                  control: g,
                                  value: (c = (n = $[s]) == null ? void 0 : n.vaccine) == null ? void 0 : c.map(
                                    (t) => t || []
                                  )
                                }
                              )
                            ] }),
                            /* @__PURE__ */ l("div", { className: "w-full md:w-[20%]", children: [
                              /* @__PURE__ */ e(
                                m,
                                {
                                  variant: "caption",
                                  className: "pb-1 text-base block",
                                  children: ze
                                }
                              ),
                              /* @__PURE__ */ e(fe, { fullWidth: !0, children: /* @__PURE__ */ e(
                                X,
                                {
                                  name: `dependencies.${s}.required`,
                                  control: g,
                                  defaultValue: "",
                                  render: ({ field: t }) => /* @__PURE__ */ l(
                                    he,
                                    {
                                      ...t,
                                      className: "w-full",
                                      sx: { height: 38 },
                                      error: j(
                                        p,
                                        `dependencies.${s}.required`
                                      ),
                                      onChange: (x) => {
                                        var N, y;
                                        t.onChange(x.target.value), x.target.value === "false" ? (v(
                                          `dependencies.${s}.min_gap_days`,
                                          0
                                        ), v(
                                          `dependencies.${s}.max_gap_days`,
                                          0
                                        ), je([
                                          `dependencies.${s}.min_gap_days`,
                                          `dependencies.${s}.max_gap_days`
                                        ])) : (v(
                                          `dependencies.${s}.min_gap_days`,
                                          ((N = $[s]) == null ? void 0 : N.min_gap_days) || 0
                                        ), v(
                                          `dependencies.${s}.max_gap_days`,
                                          ((y = $[s]) == null ? void 0 : y.max_gap_days) || 0
                                        ));
                                      },
                                      children: [
                                        /* @__PURE__ */ e(B, { value: "true", children: /* @__PURE__ */ e(m, { variant: "body2", children: o("Should be given") }) }),
                                        /* @__PURE__ */ e(B, { value: "false", children: /* @__PURE__ */ e(m, { variant: "body2", children: o("Should not be given") }) }),
                                        /* @__PURE__ */ e(B, { value: "Optional", children: /* @__PURE__ */ e(m, { variant: "body2", children: o("Optional") }) })
                                      ]
                                    }
                                  )
                                }
                              ) })
                            ] }),
                            ((d = $[s]) == null ? void 0 : d.required) !== "false" && /* @__PURE__ */ l("div", { className: "w-full md:w-[15%]", children: [
                              /* @__PURE__ */ e(
                                m,
                                {
                                  variant: "caption",
                                  className: "pb-1 text-base block",
                                  children: De
                                }
                              ),
                              /* @__PURE__ */ e(
                                k,
                                {
                                  control: g,
                                  id: `dependencies.${s}.min_gap_days`,
                                  name: `dependencies.${s}.min_gap_days`,
                                  type: "number",
                                  errors: p,
                                  label: "",
                                  size: "small"
                                }
                              )
                            ] }),
                            ((f = $[s]) == null ? void 0 : f.required) !== "false" && /* @__PURE__ */ l("div", { className: "w-full md:w-[15%]", children: [
                              /* @__PURE__ */ e(
                                m,
                                {
                                  variant: "caption",
                                  className: "pb-1 text-base block",
                                  children: qe
                                }
                              ),
                              /* @__PURE__ */ e(
                                k,
                                {
                                  control: g,
                                  id: `dependencies.${s}.max_gap_days`,
                                  name: `dependencies.${s}.max_gap_days`,
                                  type: "number",
                                  errors: p,
                                  label: "",
                                  size: "small"
                                }
                              )
                            ] }),
                            /* @__PURE__ */ l(u, { className: "w-full md:w-[24%]", children: [
                              /* @__PURE__ */ e(
                                m,
                                {
                                  variant: "caption",
                                  className: "pb-1 text-base block",
                                  children: Se
                                }
                              ),
                              /* @__PURE__ */ e(
                                k,
                                {
                                  control: g,
                                  id: `dependencies.${s}.message`,
                                  name: `dependencies.${s}.message`,
                                  errors: p,
                                  label: "",
                                  size: "small"
                                }
                              ),
                              j(
                                p,
                                `dependencies.${s}.vaccine`
                              ) && /* @__PURE__ */ e(m, { variant: "caption", color: "error", children: (i = j(
                                p,
                                `dependencies.${s}.vaccine`
                              )) == null ? void 0 : i.message })
                            ] }),
                            /* @__PURE__ */ e(u, { className: "mt-8", children: /* @__PURE__ */ e(
                              ee,
                              {
                                icon: ge,
                                color: "red",
                                className: "cursor-pointer p-1 text-xl",
                                onClick: () => Je(s)
                              }
                            ) })
                          ] }) })
                        },
                        a.id
                      );
                    }),
                    ne.length === 0 && /* @__PURE__ */ e(
                      _,
                      {
                        sx: {
                          padding: 3,
                          marginLeft: "15px",
                          marginTop: 2,
                          textAlign: "center",
                          color: "text.secondary",
                          backgroundColor: "var(--color-background)",
                          borderRadius: 2,
                          border: "1px dashed var(--color-gray3)"
                        },
                        children: /* @__PURE__ */ e(m, { variant: "body2", color: "text.secondary", children: o("No dependencies added yet.") })
                      }
                    )
                  ] }),
                  /* @__PURE__ */ l(u, { size: { xs: 12 }, className: "mb-3", children: [
                    /* @__PURE__ */ e(K, {}),
                    /* @__PURE__ */ l(_, { className: "flex flex-row items-center justify-between", children: [
                      /* @__PURE__ */ e(
                        Z,
                        {
                          subheader: /* @__PURE__ */ l(
                            _,
                            {
                              display: "flex",
                              alignItems: "center",
                              justifyContent: "space-between",
                              children: [
                                /* @__PURE__ */ e(
                                  m,
                                  {
                                    variant: "body2",
                                    className: "font-bold text-lg text-customBlue",
                                    "data-testid": "form-header",
                                    children: o("Add Filters")
                                  }
                                ),
                                /* @__PURE__ */ e(
                                  Ke,
                                  {
                                    arrow: !0,
                                    title: /* @__PURE__ */ l(Qe, { children: [
                                      "Queries with ",
                                      /* @__PURE__ */ e("b", { children: "params" }),
                                      " ",
                                      /* @__PURE__ */ e("i", { children: "{{member_id}}" }),
                                      " are shown here.",
                                      /* @__PURE__ */ e("br", {}),
                                      "The query must return a single boolean column named ",
                                      /* @__PURE__ */ e("code", { children: "filter_passed" }),
                                      ", e.g.",
                                      " ",
                                      /* @__PURE__ */ e("code", { children: "{ filter_passed: true }" }),
                                      "."
                                    ] }),
                                    children: /* @__PURE__ */ e(Xe, { size: "small", color: "info", children: /* @__PURE__ */ e(ee, { icon: ua }) })
                                  }
                                )
                              ]
                            }
                          ),
                          slotProps: {
                            subheader: {
                              variant: "p",
                              sx: { fontWeight: "bold" }
                            }
                          }
                        }
                      ),
                      /* @__PURE__ */ e(
                        M,
                        {
                          variant: "contained",
                          size: "small",
                          onClick: () => oe({
                            filterName: "",
                            queryCode: ""
                          }),
                          children: o("Add")
                        }
                      )
                    ] }),
                    ie.map((a, s) => /* @__PURE__ */ e(
                      _,
                      {
                        sx: {
                          display: "flex",
                          justifyContent: "space-between",
                          mb: 2,
                          alignItems: "flex-start",
                          border: "1px solid var(--color-gray3)",
                          backgroundColor: "var(--color-background)",
                          padding: 2,
                          borderRadius: 2,
                          position: "relative",
                          marginLeft: "15px"
                        },
                        children: /* @__PURE__ */ e("div", { className: "w-full", children: /* @__PURE__ */ l("div", { className: "flex flex-wrap gap-4 items-center w-full", children: [
                          /* @__PURE__ */ l("div", { className: "w-full md:w-[45%]", children: [
                            /* @__PURE__ */ e(
                              m,
                              {
                                variant: "caption",
                                className: "pb-1 text-base block",
                                children: o("Filter Name")
                              }
                            ),
                            /* @__PURE__ */ e(
                              k,
                              {
                                control: g,
                                name: `filters.${s}.filterName`,
                                errors: p,
                                label: "",
                                size: "small"
                              }
                            )
                          ] }),
                          /* @__PURE__ */ l("div", { className: "w-full md:w-[50%]", children: [
                            /* @__PURE__ */ e(
                              m,
                              {
                                variant: "caption",
                                className: "pb-1 text-base block",
                                children: o("Query")
                              }
                            ),
                            /* @__PURE__ */ e(
                              X,
                              {
                                name: `filters.${s}.queryCode`,
                                control: g,
                                render: ({ field: n }) => {
                                  const [c, d] = w(""), f = re.find(
                                    (i) => i.queryCode === n.value
                                  ) || null;
                                  return /* @__PURE__ */ e(
                                    ea,
                                    {
                                      options: re,
                                      getOptionLabel: (i) => i.queryCode || "",
                                      isOptionEqualToValue: (i, t) => (i == null ? void 0 : i.queryCode) === t,
                                      value: f,
                                      inputValue: c,
                                      onInputChange: (i, t) => {
                                        d(t);
                                      },
                                      onChange: (i, t) => {
                                        n.onChange(
                                          (t == null ? void 0 : t.queryCode) || ""
                                        ), d(
                                          (t == null ? void 0 : t.queryCode) || ""
                                        );
                                      },
                                      filterOptions: (i, { inputValue: t }) => i.filter(
                                        (x) => x.queryCode.toLowerCase().includes(
                                          t.toLowerCase()
                                        )
                                      ),
                                      renderOption: (i, t) => /* @__PURE__ */ e("li", { ...i, children: t.queryCode }),
                                      renderInput: (i) => {
                                        var t;
                                        return /* @__PURE__ */ e(
                                          aa,
                                          {
                                            ...i,
                                            size: "small",
                                            error: !!j(
                                              p,
                                              `filters.${s}.queryCode`
                                            ),
                                            helperText: (t = j(
                                              p,
                                              `filters.${s}.queryCode`
                                            )) == null ? void 0 : t.message
                                          }
                                        );
                                      }
                                    }
                                  );
                                }
                              }
                            )
                          ] }),
                          /* @__PURE__ */ e(u, { className: "mt-8", children: /* @__PURE__ */ e(
                            ee,
                            {
                              icon: ge,
                              color: "red",
                              className: "cursor-pointer p-1 text-xl",
                              onClick: () => Be(s)
                            }
                          ) })
                        ] }) })
                      },
                      a.id
                    )),
                    ie.length === 0 && /* @__PURE__ */ e(
                      _,
                      {
                        sx: {
                          padding: 3,
                          marginLeft: "15px",
                          marginTop: 2,
                          textAlign: "center",
                          color: "text.secondary",
                          backgroundColor: "var(--color-background)",
                          borderRadius: 2,
                          border: "1px dashed var(--color-gray3)"
                        },
                        children: /* @__PURE__ */ e(m, { variant: "body2", color: "text.secondary", children: o("No filters added yet.") })
                      }
                    )
                  ] })
                ]
              }
            ) }),
            /* @__PURE__ */ l(u, { size: { xs: 12 }, className: "mb-3", children: [
              /* @__PURE__ */ e(K, { variant: "middle", className: "h-[0.625rem]" }),
              /* @__PURE__ */ e(u, { container: !0, spacing: 2 })
            ] })
          ] }),
          /* @__PURE__ */ l(u, { item: !0, container: !0, size: { xs: 12 }, justifyContent: "flex-end", children: [
            /* @__PURE__ */ e(
              M,
              {
                variant: "contained",
                color: "secondary",
                size: "small",
                onClick: () => {
                  G("/ui/medplat/manage/immunizationConfiguratorList");
                },
                "data-testid": "manageImmunization-close-button",
                children: /* @__PURE__ */ e(m, { variant: "caption", children: o("Back") })
              }
            ),
            /* @__PURE__ */ e(
              M,
              {
                className: "ml-2 mr-4",
                size: "small",
                variant: "contained",
                color: "primary",
                type: "submit",
                "data-testid": "submit-button",
                children: o("Submit")
              }
            )
          ] })
        ] })
      ] })
    }
  ) });
};
export {
  Fa as default
};
//# sourceMappingURL=manageImmunization-B4swS745.js.map
