import { jsxs as s, jsx as n, Fragment as _ } from "react/jsx-runtime";
import { execute as ie } from "@/common/services/QueryBuilder";
import { showToast as q } from "@/common/toaster/toaster";
import { useDebounceFn as sn } from "@/hooks/useDebounceFn";
import dn from "@/views/custom-components/CustomConfirmationModal";
import cn from "@/views/custom-components/CustomTextField";
import C from "@/common/fontAwesomeIcons/FontAwesomeIcons";
import oe from "@monaco-editor/react";
import { Paper as pe, Typography as u, Tooltip as A, Button as z, FormControl as Z, RadioGroup as ye, FormControlLabel as W, Radio as Q, Autocomplete as se, TextField as V, IconButton as B, InputAdornment as ge, Popover as un, Checkbox as mn, TableContainer as hn, Table as pn, TableHead as yn, TableRow as I, TableCell as M, TableBody as He, Modal as gn, Switch as fn } from "@mui/material";
import p from "@mui/material/Grid2";
import { Box as g } from "@mui/system";
import S from "prop-types";
import { useState as w, useEffect as fe } from "react";
import { useForm as xn, useWatch as Cn, Controller as N } from "react-hook-form";
import { useSelector as vn } from "react-redux";
function bn({
  tableInfo: d,
  table: v,
  isEditMode: m,
  updateEditMode: xe,
  tableCompleteInfo: P,
  reloadComponents: Ue,
  isCreateTableMode: o,
  isSyncTableMode: j,
  updateCreateTableMode: Ce,
  openDeleteModal: Ge,
  setOpenDeleteModal: ve
}) {
  var Ae, We, Be, Fe, $e, _e, Qe, je, Ee;
  const be = ["Document Master", "Form Configurator", "Form Media Record", "LMS"], we = (e) => (e == null ? void 0 : e.toUpperCase().replace(/ /g, "_")) || "", Ne = (e) => be.find((r) => we(r) === e) || "", Ye = (e) => {
    var r;
    return e != null && e.downloadType ? Ne(e.downloadType) || e.downloadType : (r = e == null ? void 0 : e.data) != null && r.downloadType ? Ne(e.data.downloadType) || e.data.downloadType : "";
  }, Je = (e) => {
    var a;
    const r = e == null ? void 0 : e.downloadUrl;
    if (typeof r == "object" && r !== null) return r.apiEndpoint || "";
    const l = (a = e == null ? void 0 : e.data) == null ? void 0 : a.downloadUrl;
    return typeof l == "object" && l !== null && l.apiEndpoint || "";
  }, de = (e) => {
    var a;
    const r = e == null ? void 0 : e.downloadUrl;
    if (typeof r == "object" && r !== null) return r.requestParameters || [];
    const l = (a = e == null ? void 0 : e.data) == null ? void 0 : a.downloadUrl;
    return typeof l == "object" && l !== null ? l.requestParameters || [] : [];
  }, [Se, ce] = w([]), [Pe, ee] = w(!1), [E, ne] = w(""), [Xe, Ze] = w({}), [re, H] = w(""), [b, D] = w([]), [U, R] = w([]), [f, ae] = w(0), [G, ue] = w(0), [ke, te] = w(null), [Y, le] = w(null), [F, Ie] = w({}), [T, O] = w(
    o ? [] : de(d == null ? void 0 : d.mediaConfiguration)
  ), Ve = !!ke, Me = Ve ? "simple-popover" : void 0, De = ["TEXT", "INTEGER"], Ke = vn((e) => e.Authenticate.user.id), K = {
    selectedColumns: o ? [] : [...d.columnInfo],
    modifiedOnQuery: o ? "" : d.modifiedOnQuery,
    dataFetchQuery: o ? "" : d.selectQuery,
    tableName: "",
    createTableQuery: "",
    syncByDefault: o ? null : (Ae = d == null ? void 0 : d.syncByDefault) == null ? void 0 : Ae.toString(),
    primaryKey: o ? null : d.primaryKey ? d.primaryKey : null,
    userData: o ? null : ((We = d == null ? void 0 : d.userData) == null ? void 0 : We.toString()) || null,
    hasMedia: o ? null : ((Be = d == null ? void 0 : d.hasMedia) == null ? void 0 : Be.toString()) || null,
    downloadType: o ? "" : Ye(d.mediaConfiguration),
    apiEndpoint: o ? "" : Je(d.mediaConfiguration),
    requestParameters: o ? [] : de(d.mediaConfiguration),
    directoryPath: o ? "" : (($e = (Fe = d.mediaConfiguration) == null ? void 0 : Fe.directoryPath) == null ? void 0 : $e.split("/")[1]) || ((je = (Qe = (_e = d.mediaConfiguration) == null ? void 0 : _e.data) == null ? void 0 : Qe.directoryPath) == null ? void 0 : je.split("/")[1]) || ""
  }, t = xn({
    defaultValues: {
      ...K
    },
    mode: "onChange"
  }), en = !t.formState.isValid, $ = t.watch("hasMedia"), ze = t.watch("newColumnName"), Re = t.watch("newColumnType");
  t.register("requestParameters", {
    validate: (e) => $ !== "true" ? !0 : !e || e.length === 0 ? "At least one request parameter is required." : e.some((l) => {
      var a, i;
      return !((a = l.key) != null && a.trim()) || !((i = l.value) != null && i.trim());
    }) ? "Each parameter must have both a key and a value filled." : !0
  });
  const nn = async (e) => {
    var l;
    const r = o ? t.getValues("createTableQuery") : await Oe(
      [...d.columnInfo],
      e.selectedColumns,
      d.primaryKey,
      (l = e.primaryKey) == null ? void 0 : l.toString()
    );
    r ? (Ze(() => e), ne(() => r), Le(), ee(!0)) : Te(e, !1);
  }, Te = async (e, r) => {
    var h;
    const l = {};
    let a, i = e || Xe;
    if (r || ne(""), Object.keys(i).length > 0) {
      i.selectedColumns.forEach((y) => {
        l[y.name] = y.type;
      }), i.columns = JSON.stringify(l), i.primaryKey = (h = i.primaryKey) == null ? void 0 : h.toString();
      let c = {
        downloadType: we(i.downloadType),
        downloadUrl: {
          apiEndpoint: i.apiEndpoint || "",
          requestParameters: T || []
        },
        directoryPath: "#directoryPath#/" + i.directoryPath + "/"
      };
      c = JSON.stringify(c), a = (await ie({
        code: "update_mobile_entity",
        parameters: {
          tableName: o ? t.getValues("tableName") : v,
          syncByDefault: i.syncByDefault === "true",
          columnInfo: i.columns,
          primaryKey: i.primaryKey,
          modifiedOnQuery: i.modifiedOnQuery,
          selectQuery: i.dataFetchQuery,
          userData: i.userData === "true",
          hasMedia: i.hasMedia === "true",
          mediaConfiguration: i.hasMedia === "true" ? c : null,
          modifiedBy: Ke,
          migrationScript: E,
          isUpdate: !0,
          generateMigration: E !== ""
        }
      }).catch((y) => {
        q({ message: `Submission failure ${y}`, type: "error" });
      })).data;
    } else
      a = (await ie({
        code: "update_mobile_entity",
        parameters: {
          tableName: o ? t.getValues("tableName") : v,
          syncByDefault: !1,
          columnInfo: "",
          primaryKey: "",
          modifiedOnQuery: "",
          selectQuery: "",
          userData: null,
          hasMedia: null,
          mediaConfiguration: "",
          modifiedBy: Ke,
          migrationScript: E,
          isUpdate: !1,
          generateMigration: !0
        }
      }).catch((c) => {
        q({ message: `Submission failure ${c}`, type: "error" });
      })).data;
    a != null && a.result && (xe(!1), Ce(!1), Ue((c) => !c), q({ message: "Table configuration saved successfully!", type: "success" })), ee(!1);
  }, rn = () => {
    ee(!1);
  }, Oe = async (e, r, l, a) => {
    let i = "";
    if (e.length > 0) {
      let h = e.map((x) => x.name), c = r.map((x) => x.name), y = e.filter((x) => !c.includes(x.name)), L = r.filter((x) => !h.includes(x.name)), J = y.map((x) => `ALTER TABLE ${v} DROP COLUMN ${x.name};`).join(`
`), k = L.map((x) => `ALTER TABLE ${v} ADD COLUMN ${x.name} ${x.type};`).join(`
`);
      return i += y.length > 0 ? `${J}
` : "", i += L.length > 0 ? `${k}` : "", i;
    } else {
      const h = o ? t.getValues("tableName") : v;
      return i = `CREATE TABLE ${h} (
`, i += r.map((c) => `${c.name} ${c.type}`).join(`,
`), a && (i += `,
CONSTRAINT ${h}_pkey PRIMARY KEY (${a})`), i += ");", i;
    }
  }, Le = async () => {
    var r;
    const e = (r = await ie({
      code: "MFA_MIGRATION_MASTER_GET_LATEST_VERSIONS"
    }).catch((l) => {
      q({ message: `Failed to fetch migration versions ${l}`, type: "error" });
    })) == null ? void 0 : r.data;
    Ie(e == null ? void 0 : e.result[0]);
  };
  fe(() => {
    if (H(""), ue(0), ae(0), o && (ce([]), D([]), R([]), O([]), t.reset({
      ...K
    }), setTimeout(() => t.trigger(), 0)), v && d && !o) {
      ce(P !== void 0 ? () => P.columnInfo : () => d.columnInfo);
      let e = P !== void 0 ? P.columnInfo.map((a) => a.name) : d.columnInfo.map((a) => a.name), r = {};
      P !== void 0 ? P.columnInfo.forEach((a) => {
        r[a.name] = a.type;
      }) : d.columnInfo.forEach((a) => {
        r[a.name] = a.type;
      });
      let l = {};
      d.columnInfo.forEach((a) => {
        l[a.name] = a.type;
      }), d.columnInfo.map((a) => {
        e.includes(a.name) || (a.onlyMobileDb = !0), l[a.name] !== r[a.name] && (a.isTypeChanged = !0), a.name === d.primaryKey && (a.isPrimaryKey = !0);
      }), t.reset({
        ...K
      }), setTimeout(() => t.trigger(), 0), O(de(d == null ? void 0 : d.mediaConfiguration)), D(
        () => d.columnInfo.sort((a, i) => a.name === d.primaryKey ? (a.isPrimaryKey = !0, -1) : i.name === d.primaryKey ? (i.isPrimaryKey = !0, 1) : a.name.localeCompare(i.name))
      ), R(P !== void 0 ? () => {
        const a = new Set(d.columnInfo.map((h) => h.name));
        return P.columnInfo.map((h) => ({
          ...h,
          isSynced: a.has(h.name)
        })).sort((h, c) => h.name === d.primaryKey ? -1 : c.name === d.primaryKey ? 1 : h.name.localeCompare(c.name));
      } : []);
    }
  }, [d, P, v, o]), fe(() => {
    const e = re.toLowerCase();
    if (f === 0) {
      const r = t.getValues("selectedColumns"), l = e ? r.filter((a) => a.name.toLowerCase().includes(e)) : r;
      D(l);
    } else {
      const r = new Set(t.getValues("selectedColumns").map((a) => a.name)), l = e ? Se.filter((a) => a.name.toLowerCase().includes(e)).map((a) => ({
        ...a,
        isSynced: r.has(a.name)
      })) : Se.map((a) => ({
        ...a,
        isSynced: r.has(a.name)
      }));
      R(l);
    }
  }, [re, f]);
  const qe = Cn({
    control: t.control,
    name: "tableName"
  });
  fe(() => {
    o && t.getValues("selectedColumns").length > 0 && qe && he(t.getValues("selectedColumns"));
  }, [qe]);
  const an = (e) => {
    ne(e);
  }, tn = () => {
    m && xe(!m), o && Ce(!o), t.reset({
      ...K,
      newColumnName: null,
      newColumnType: null
    }), O(K.requestParameters || []), D(K.selectedColumns || []);
    const e = new Set((K.selectedColumns || []).map((r) => r.name));
    R(
      (K.allColumns || []).map((r) => ({
        ...r,
        isSynced: e.has(r.name)
      }))
    );
  }, ln = async () => {
    var l, a, i;
    ve(() => !1);
    const e = await ie({
      code: "DELETE_MOBILE_ENTITY",
      parameters: {
        tableName: v
      }
    }).catch((h) => {
      q({ message: `Metadata deletion failure ${h}`, type: "error" });
    });
    (i = (a = (l = e == null ? void 0 : e.data) == null ? void 0 : l.result) == null ? void 0 : a[0]) != null && i.table_name && q({ message: `${v} deleted successfully!`, type: "success" });
    let r = `DROP TABLE if exists ${v};`;
    ne(() => r), Le(), ee(!0);
  }, on = sn(async (e) => {
    const r = /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(e);
    if (e.trim().length === 0) {
      t.setError("newColumnName", {
        type: "manual",
        message: "*Column name is required. "
      });
      return;
    }
    if (!r) {
      t.setError("newColumnName", {
        type: "manual",
        message: "Must start with a letter/underscore and contain only letters, numbers, and underscores."
      });
      return;
    }
    U.some((l) => l.name === e) || b.some((l) => l.name === e) ? t.setError("newColumnName", {
      type: "manual",
      message: "Column name already exists in the table."
    }) : t.clearErrors("newColumnName");
  }, 500), me = b.some((e) => e.isPrimaryKey === !0), he = async (e) => {
    var a;
    if (G !== 2 && o && ue(2), e.length === 0) {
      t.setValue("createTableQuery", "", { shouldDirty: !0 });
      return;
    }
    const r = ((a = e.find((i) => i.isPrimaryKey)) == null ? void 0 : a.name) || "", l = await Oe([], e, "", r);
    t.setValue("createTableQuery", l, { shouldDirty: !0 });
  };
  return /* @__PURE__ */ s(
    p,
    {
      size: 9,
      sx: {
        maxHeight: "88vh",
        overflowY: "auto",
        overflowX: "hidden"
      },
      children: [
        /* @__PURE__ */ n(
          pe,
          {
            variant: "outlined",
            sx: {
              p: 2.5,
              borderRadius: 2.5,
              backgroundColor: "background.paper"
            },
            children: /* @__PURE__ */ s("form", { onSubmit: t.handleSubmit(nn), noValidate: !0, className: "w-full", children: [
              /* @__PURE__ */ s(
                g,
                {
                  sx: {
                    display: "flex",
                    justifyContent: "space-between",
                    alignItems: "center",
                    mb: 2.5,
                    pb: 2,
                    borderBottom: "1px solid",
                    borderColor: "divider"
                  },
                  children: [
                    /* @__PURE__ */ s(g, { sx: { flex: 1 }, children: [
                      o ? /* @__PURE__ */ s(g, { sx: { maxWidth: 350 }, children: [
                        /* @__PURE__ */ n(u, { variant: "subtitle2", sx: { fontWeight: 600, mb: 0.5 }, children: "New Table" }),
                        /* @__PURE__ */ n(
                          cn,
                          {
                            control: t.control,
                            id: "tableName",
                            name: "tableName",
                            errors: t.formState.errors,
                            label: "Table Name",
                            size: "small",
                            rules: {
                              required: {
                                value: !0,
                                message: "Please Enter the name"
                              },
                              pattern: {
                                value: /^[a-z0-9_]+$/,
                                message: "Only lowercase letters, numbers, and underscores are allowed"
                              },
                              validate: {}
                            }
                          }
                        )
                      ] }) : /* @__PURE__ */ s(_, { children: [
                        /* @__PURE__ */ n(u, { variant: "h6", sx: { fontWeight: 700, color: "text.primary" }, children: v || "No Table Selected" }),
                        /* @__PURE__ */ n(u, { variant: "caption", color: "text.secondary", children: v ? "View and manage table configuration" : "Select a table from the list" })
                      ] }),
                      /* @__PURE__ */ n(
                        dn,
                        {
                          description: "Do you want to drop this table?",
                          title: "CONFIRMATION",
                          open: Ge,
                          handleClick: ln,
                          handleClose: () => ve((e) => !e)
                        }
                      )
                    ] }),
                    /* @__PURE__ */ s(g, { sx: { display: "flex", gap: 1 }, children: [
                      (m || o) && /* @__PURE__ */ n(A, { title: me ? "" : "A primary key is required to proceed.", arrow: !0, placement: "bottom", children: /* @__PURE__ */ n("span", { children: /* @__PURE__ */ n(
                        z,
                        {
                          type: "submit",
                          variant: "contained",
                          size: "small",
                          sx: {
                            borderRadius: 2,
                            fontWeight: 600,
                            textTransform: "none",
                            boxShadow: "none",
                            px: 2.5
                          },
                          disabled: en || !me,
                          startIcon: /* @__PURE__ */ n(C, { iconName: "faCheck" }),
                          children: "Submit"
                        }
                      ) }) }),
                      (m || o) && /* @__PURE__ */ n(
                        z,
                        {
                          type: "button",
                          variant: "outlined",
                          onClick: () => tn(),
                          size: "small",
                          sx: {
                            borderRadius: 2,
                            fontWeight: 600,
                            textTransform: "none",
                            borderColor: "divider",
                            color: "text.secondary",
                            px: 2.5,
                            "&:hover": { backgroundColor: "action.hover", borderColor: "text.disabled" }
                          },
                          children: "Cancel"
                        }
                      )
                    ] })
                  ]
                }
              ),
              /* @__PURE__ */ s(p, { container: !0, spacing: 2, children: [
                /* @__PURE__ */ s(p, { size: 7, children: [
                  /* @__PURE__ */ n(
                    g,
                    {
                      sx: {
                        display: "inline-flex",
                        gap: 0.75,
                        p: 0.75,
                        backgroundColor: "action.hover",
                        borderRadius: 2.5,
                        border: "1px solid",
                        borderColor: "divider",
                        mb: 2
                      },
                      children: [
                        { value: 0, label: "Modified On Query", icon: "faClock" },
                        { value: 1, label: "Data Fetch Query", icon: "faDatabase" },
                        ...o ? [{ value: 2, label: "Create Table Query", icon: "faCode" }] : []
                      ].map((e) => {
                        const r = G === e.value;
                        return /* @__PURE__ */ n(
                          z,
                          {
                            onClick: () => ue(e.value),
                            size: "small",
                            disableElevation: !0,
                            sx: {
                              px: 2,
                              py: 0.6,
                              borderRadius: 2,
                              textTransform: "none",
                              fontSize: "0.8rem",
                              fontWeight: r ? 700 : 500,
                              color: r ? "primary.dark" : "text.secondary",
                              backgroundColor: r ? "background.paper" : "transparent",
                              boxShadow: r ? "0px 2px 6px rgba(0,0,0,0.06)" : "none",
                              border: "1px solid",
                              borderColor: r ? "divider" : "transparent",
                              transition: "all 0.2s ease",
                              "&:hover": {
                                backgroundColor: r ? "background.paper" : "action.hover"
                              }
                            },
                            startIcon: /* @__PURE__ */ n(C, { iconName: e.icon }),
                            children: e.label
                          },
                          e.value
                        );
                      })
                    }
                  ),
                  G === 0 && /* @__PURE__ */ s(p, { children: [
                    /* @__PURE__ */ n(u, { variant: "subtitle1", children: "Modified On Query:" }),
                    /* @__PURE__ */ n(
                      N,
                      {
                        name: "modifiedOnQuery",
                        control: t.control,
                        render: ({ field: e, fieldState: { error: r } }) => /* @__PURE__ */ s(_, { children: [
                          /* @__PURE__ */ n(
                            oe,
                            {
                              height: "35vh",
                              defaultLanguage: "sql",
                              theme: "vs-dark",
                              options: {
                                fontSize: 12,
                                minimap: { enabled: !1 },
                                automaticLayout: !0,
                                scrollBeyondLastLine: !1,
                                wordWrap: "on",
                                occurrencesHighlight: !1,
                                selectionHighlight: !1,
                                readOnly: !m && !o
                              },
                              value: e.value || "",
                              defaultValue: e.value || "",
                              onChange: e.onChange,
                              editorProps: { $blockScrolling: !0 }
                            }
                          ),
                          (r == null ? void 0 : r.message) && /* @__PURE__ */ n(u, { variant: "subtitle1", color: "error", fontSize: "13px", className: "mt-1 ml-[13px]", children: r.message })
                        ] })
                      }
                    )
                  ] }),
                  G === 1 && /* @__PURE__ */ s(p, { children: [
                    /* @__PURE__ */ n(u, { variant: "subtitle1", children: "Data Fetch Query:" }),
                    /* @__PURE__ */ n(
                      N,
                      {
                        name: "dataFetchQuery",
                        control: t.control,
                        render: ({ field: e, fieldState: { error: r } }) => /* @__PURE__ */ s(_, { children: [
                          /* @__PURE__ */ n(
                            oe,
                            {
                              height: "35vh",
                              defaultLanguage: "sql",
                              theme: "vs-dark",
                              options: {
                                fontSize: 12,
                                minimap: { enabled: !1 },
                                automaticLayout: !0,
                                scrollBeyondLastLine: !1,
                                wordWrap: "on",
                                occurrencesHighlight: !1,
                                selectionHighlight: !1,
                                readOnly: !m && !o
                              },
                              value: e.value || "",
                              defaultValue: e.value || "",
                              onChange: e.onChange,
                              editorProps: { $blockScrolling: !0 }
                            }
                          ),
                          (r == null ? void 0 : r.message) && /* @__PURE__ */ n(u, { variant: "subtitle1", color: "error", fontSize: "13px", className: "mt-1 ml-[13px]", children: r.message })
                        ] })
                      }
                    )
                  ] }),
                  G === 2 && /* @__PURE__ */ s(p, { children: [
                    /* @__PURE__ */ n(u, { variant: "subtitle1", children: "Create Table Query:" }),
                    /* @__PURE__ */ n(
                      N,
                      {
                        name: "createTableQuery",
                        control: t.control,
                        render: ({ field: e, fieldState: { error: r } }) => /* @__PURE__ */ s(_, { children: [
                          /* @__PURE__ */ n(
                            oe,
                            {
                              height: "35vh",
                              defaultLanguage: "sql",
                              theme: "vs-dark",
                              options: {
                                fontSize: 12,
                                minimap: { enabled: !1 },
                                automaticLayout: !0,
                                scrollBeyondLastLine: !1,
                                wordWrap: "on",
                                occurrencesHighlight: !1,
                                selectionHighlight: !1,
                                readOnly: !0
                              },
                              value: e.value || "",
                              defaultValue: e.value || "",
                              onChange: e.onChange,
                              editorProps: { $blockScrolling: !0 }
                            }
                          ),
                          (r == null ? void 0 : r.message) && /* @__PURE__ */ n(u, { variant: "subtitle1", color: "error", fontSize: "13px", className: "mt-1 ml-[13px]", children: r.message })
                        ] })
                      }
                    )
                  ] }),
                  /* @__PURE__ */ s(pe, { variant: "outlined", sx: { p: 2, mt: 2, borderRadius: 2, backgroundColor: "action.hover" }, children: [
                    /* @__PURE__ */ n(u, { variant: "subtitle2", sx: { fontWeight: 700, mb: 1.5, color: "text.primary" }, children: "Table Settings" }),
                    /* @__PURE__ */ s(p, { container: !0, spacing: 2, children: [
                      /* @__PURE__ */ s(p, { size: 6, xs: 12, children: [
                        /* @__PURE__ */ s(u, { variant: "body2", sx: { fontWeight: 600, mb: 0.5 }, children: [
                          "Data Scope ",
                          /* @__PURE__ */ n("span", { style: { color: "#ef4444" }, children: "*" })
                        ] }),
                        /* @__PURE__ */ n(Z, { children: /* @__PURE__ */ n(
                          N,
                          {
                            name: "userData",
                            control: t.control,
                            rules: { required: "Required field." },
                            disabled: !m && !o,
                            defaultValue: d.userData !== void 0 && d.userData !== null ? String(d.userData) : "",
                            render: ({ field: e }) => /* @__PURE__ */ s(ye, { row: !0, ...e, value: e.value, children: [
                              /* @__PURE__ */ n(
                                W,
                                {
                                  value: "true",
                                  control: /* @__PURE__ */ n(Q, { disabled: !m && !o, size: "small" }),
                                  label: /* @__PURE__ */ n(u, { variant: "body2", children: "User" })
                                }
                              ),
                              /* @__PURE__ */ n(
                                W,
                                {
                                  value: "false",
                                  control: /* @__PURE__ */ n(Q, { disabled: !m && !o, size: "small" }),
                                  label: /* @__PURE__ */ n(u, { variant: "body2", children: "System" })
                                }
                              )
                            ] })
                          }
                        ) })
                      ] }),
                      /* @__PURE__ */ s(p, { size: 6, xs: 12, children: [
                        /* @__PURE__ */ s(u, { variant: "body2", sx: { fontWeight: 600, mb: 0.5 }, children: [
                          "Sync by Default ",
                          /* @__PURE__ */ n("span", { style: { color: "#ef4444" }, children: "*" })
                        ] }),
                        /* @__PURE__ */ n(Z, { children: /* @__PURE__ */ n(
                          N,
                          {
                            name: "syncByDefault",
                            id: "syncByDefault",
                            control: t.control,
                            rules: { required: "Required field." },
                            disabled: !m && !o,
                            defaultValue: d.syncByDefault !== void 0 && d.syncByDefault !== null ? String(d.syncByDefault) : "",
                            render: ({ field: e }) => /* @__PURE__ */ s(ye, { row: !0, ...e, value: e.value, children: [
                              /* @__PURE__ */ n(
                                W,
                                {
                                  value: "true",
                                  control: /* @__PURE__ */ n(Q, { disabled: !m && !o, size: "small" }),
                                  label: /* @__PURE__ */ n(u, { variant: "body2", children: "Yes" })
                                }
                              ),
                              /* @__PURE__ */ n(
                                W,
                                {
                                  value: "false",
                                  control: /* @__PURE__ */ n(Q, { disabled: !m && !o, size: "small" }),
                                  label: /* @__PURE__ */ n(u, { variant: "body2", children: "No" })
                                }
                              )
                            ] })
                          }
                        ) })
                      ] }),
                      /* @__PURE__ */ s(p, { size: 6, xs: 12, children: [
                        /* @__PURE__ */ s(u, { variant: "body2", sx: { fontWeight: 600, mb: 0.5 }, children: [
                          "Has Media ",
                          /* @__PURE__ */ n("span", { style: { color: "#ef4444" }, children: "*" })
                        ] }),
                        /* @__PURE__ */ n(Z, { children: /* @__PURE__ */ n(
                          N,
                          {
                            name: "hasMedia",
                            id: "hasMedia",
                            control: t.control,
                            rules: { required: "Required field." },
                            disabled: !m && !o,
                            defaultValue: d.hasMedia !== void 0 && d.hasMedia !== null ? String(d.hasMedia) : "",
                            render: ({ field: e }) => /* @__PURE__ */ s(ye, { row: !0, ...e, value: e.value, children: [
                              /* @__PURE__ */ n(
                                W,
                                {
                                  value: "true",
                                  control: /* @__PURE__ */ n(Q, { disabled: !m && !o, size: "small" }),
                                  label: /* @__PURE__ */ n(u, { variant: "body2", children: "Yes" })
                                }
                              ),
                              /* @__PURE__ */ n(
                                W,
                                {
                                  value: "false",
                                  control: /* @__PURE__ */ n(Q, { disabled: !m && !o, size: "small" }),
                                  label: /* @__PURE__ */ n(u, { variant: "body2", children: "No" })
                                }
                              )
                            ] })
                          }
                        ) })
                      ] })
                    ] })
                  ] }),
                  $ === "true" && /* @__PURE__ */ s(_, { children: [
                    /* @__PURE__ */ n(u, { variant: "h6", className: "mt-2", sx: { fontWeight: 600, color: "#566A79" }, children: "Media Configuration:" }),
                    /* @__PURE__ */ s(p, { container: !0, spacing: 2, className: "mt-2", children: [
                      /* @__PURE__ */ s(p, { size: 6, xs: 12, children: [
                        /* @__PURE__ */ s(u, { variant: "subtitle2", sx: { mb: 0.5, fontWeight: 600 }, children: [
                          "Download Type",
                          /* @__PURE__ */ n("span", { style: { color: "red" }, children: "*" }),
                          ":"
                        ] }),
                        /* @__PURE__ */ n(
                          N,
                          {
                            name: "downloadType",
                            control: t.control,
                            rules: {
                              validate: (e) => $ !== "true" || !!e || "Download Type is required."
                            },
                            render: ({ field: { onChange: e, value: r }, fieldState: { error: l } }) => /* @__PURE__ */ n(
                              se,
                              {
                                options: be,
                                value: r || null,
                                disabled: !m && !o,
                                onChange: (a, i) => e(i),
                                renderInput: (a) => /* @__PURE__ */ n(
                                  V,
                                  {
                                    ...a,
                                    size: "small",
                                    placeholder: "Select download type",
                                    error: !!l,
                                    helperText: l == null ? void 0 : l.message
                                  }
                                )
                              }
                            )
                          }
                        )
                      ] }),
                      /* @__PURE__ */ n(p, { size: 12, children: /* @__PURE__ */ s(
                        g,
                        {
                          sx: {
                            border: "1px solid #ddd",
                            borderRadius: "8px",
                            p: 2,
                            backgroundColor: "#fafafa"
                          },
                          children: [
                            /* @__PURE__ */ n(u, { variant: "subtitle1", sx: { fontWeight: 600, mb: 1.5, color: "#374151" }, children: "Download URL" }),
                            /* @__PURE__ */ s(u, { variant: "subtitle2", sx: { mb: 0.5, fontWeight: 600 }, children: [
                              "API Endpoint",
                              /* @__PURE__ */ n("span", { style: { color: "red" }, children: "*" }),
                              ":"
                            ] }),
                            /* @__PURE__ */ n(
                              N,
                              {
                                name: "apiEndpoint",
                                control: t.control,
                                rules: {
                                  validate: (e) => $ !== "true" || !!(e != null && e.trim()) || "API Endpoint is required."
                                },
                                render: ({ field: e, fieldState: { error: r } }) => /* @__PURE__ */ n(
                                  V,
                                  {
                                    ...e,
                                    value: e.value || "",
                                    size: "small",
                                    variant: "outlined",
                                    fullWidth: !0,
                                    placeholder: "https://api.example.com/endpoint",
                                    error: !!r,
                                    helperText: r == null ? void 0 : r.message,
                                    slotProps: { input: { readOnly: !m && !o } },
                                    sx: { mb: 2 }
                                  }
                                )
                              }
                            ),
                            /* @__PURE__ */ s(g, { sx: { display: "flex", alignItems: "center", mb: 1 }, children: [
                              /* @__PURE__ */ s(u, { variant: "subtitle2", sx: { fontWeight: 600, color: "#374151", flex: 1 }, children: [
                                "Request Parameters",
                                /* @__PURE__ */ n("span", { style: { color: "red" }, children: "*" }),
                                ":"
                              ] }),
                              (m || o) && /* @__PURE__ */ n(A, { title: "Add parameter", arrow: !0, children: /* @__PURE__ */ n(
                                B,
                                {
                                  size: "small",
                                  onClick: () => {
                                    const e = [...T, { key: "", value: "" }];
                                    O(e), t.setValue("requestParameters", e, {
                                      shouldDirty: !0,
                                      shouldValidate: !0
                                    });
                                  },
                                  sx: {
                                    backgroundColor: "#566A79",
                                    color: "white",
                                    "&:hover": { backgroundColor: "#3d4f5c" },
                                    borderRadius: "6px",
                                    width: "28px",
                                    height: "28px"
                                  },
                                  children: /* @__PURE__ */ n(C, { iconName: "faPlus", className: "text-[0.75rem]" })
                                }
                              ) })
                            ] }),
                            T.length === 0 && /* @__PURE__ */ n(u, { variant: "caption", sx: { color: "#9ca3af", fontStyle: "italic" }, children: "No request parameters added." }),
                            T.map((e, r) => /* @__PURE__ */ s(
                              g,
                              {
                                sx: {
                                  display: "flex",
                                  gap: 1,
                                  alignItems: "flex-start",
                                  mb: 1,
                                  p: 1,
                                  borderRadius: "6px",
                                  backgroundColor: "#f0f4f8",
                                  border: "1px solid #e2e8f0"
                                },
                                children: [
                                  /* @__PURE__ */ n(
                                    V,
                                    {
                                      size: "small",
                                      label: "Key",
                                      value: e.key,
                                      placeholder: "parameterKey",
                                      disabled: !m && !o,
                                      onChange: (l) => {
                                        const a = T.map(
                                          (i, h) => h === r ? { ...i, key: l.target.value } : i
                                        );
                                        O(a), t.setValue("requestParameters", a, {
                                          shouldDirty: !0,
                                          shouldValidate: !0
                                        });
                                      },
                                      sx: { flex: 1 }
                                    }
                                  ),
                                  /* @__PURE__ */ n(
                                    se,
                                    {
                                      size: "small",
                                      options: b.map((l) => l.name),
                                      value: e.value || null,
                                      disabled: !m && !o,
                                      noOptionsText: b.length === 0 ? "No synced columns yet" : "No match",
                                      onChange: (l, a) => {
                                        const i = T.map(
                                          (h, c) => c === r ? { ...h, value: a || "" } : h
                                        );
                                        O(i), t.setValue("requestParameters", i, {
                                          shouldDirty: !0,
                                          shouldValidate: !0
                                        });
                                      },
                                      renderInput: (l) => /* @__PURE__ */ n(V, { ...l, label: "Value (Column)", placeholder: "Select column" }),
                                      sx: { flex: 1 }
                                    }
                                  ),
                                  (m || o) && /* @__PURE__ */ n(A, { title: "Remove parameter", arrow: !0, children: /* @__PURE__ */ n(
                                    B,
                                    {
                                      size: "small",
                                      onClick: () => {
                                        const l = T.filter((a, i) => i !== r);
                                        O(l), t.setValue("requestParameters", l, {
                                          shouldDirty: !0,
                                          shouldValidate: !0
                                        });
                                      },
                                      sx: { color: "#ef4444", alignSelf: "center" },
                                      children: /* @__PURE__ */ n(C, { iconName: "faTrashCan", className: "text-[0.85rem]" })
                                    }
                                  ) })
                                ]
                              },
                              r
                            )),
                            t.formState.errors.requestParameters && /* @__PURE__ */ n(u, { variant: "caption", sx: { color: "#ef4444", mt: 0.5, display: "block" }, children: t.formState.errors.requestParameters.message })
                          ]
                        }
                      ) }),
                      /* @__PURE__ */ s(p, { size: 6, xs: 12, children: [
                        /* @__PURE__ */ s(u, { variant: "subtitle2", sx: { mb: 0.5, fontWeight: 600 }, children: [
                          "Directory Path",
                          /* @__PURE__ */ n("span", { style: { color: "red" }, children: "*" }),
                          ":"
                        ] }),
                        /* @__PURE__ */ n(
                          N,
                          {
                            name: "directoryPath",
                            control: t.control,
                            defaultValue: "",
                            rules: {
                              validate: (e) => $ !== "true" || !!(e != null && e.trim()) && e.trim().length > 0 || "Directory Path is required."
                            },
                            render: ({ field: e, fieldState: { error: r } }) => /* @__PURE__ */ n(
                              V,
                              {
                                ...e,
                                size: "small",
                                variant: "outlined",
                                fullWidth: !0,
                                error: !!r,
                                helperText: r == null ? void 0 : r.message,
                                slotProps: {
                                  input: {
                                    readOnly: !m && !o,
                                    startAdornment: /* @__PURE__ */ n(ge, { position: "start", children: "#directoryPath#/" }),
                                    endAdornment: /* @__PURE__ */ n(ge, { position: "end", children: "/" })
                                  }
                                }
                              }
                            )
                          }
                        )
                      ] })
                    ] })
                  ] })
                ] }),
                /* @__PURE__ */ n(p, { size: 5, children: /* @__PURE__ */ s(pe, { variant: "outlined", sx: { px: 2, borderRadius: 2.5, height: "100%", mb: 1 }, children: [
                  /* @__PURE__ */ s(
                    g,
                    {
                      sx: {
                        display: "flex",
                        justifyContent: "space-between",
                        alignItems: "center",
                        flexWrap: "wrap",
                        gap: 1,
                        pt: 1.5
                      },
                      children: [
                        /* @__PURE__ */ s(
                          g,
                          {
                            sx: {
                              display: "inline-flex",
                              gap: 0.5,
                              p: 0.5,
                              backgroundColor: "action.hover",
                              borderRadius: 2,
                              border: "1px solid",
                              borderColor: "divider",
                              mb: 1
                            },
                            children: [
                              !o && /* @__PURE__ */ n(
                                z,
                                {
                                  size: "small",
                                  onClick: () => {
                                    ae(0), H("");
                                  },
                                  sx: {
                                    px: 1.5,
                                    py: 0.4,
                                    borderRadius: 1.5,
                                    textTransform: "none",
                                    fontSize: "0.78rem",
                                    fontWeight: f === 0 ? 700 : 500,
                                    color: f === 0 ? "primary.dark" : "text.secondary",
                                    backgroundColor: f === 0 ? "background.paper" : "transparent",
                                    boxShadow: f === 0 ? "0px 1px 4px rgba(0,0,0,0.06)" : "none",
                                    border: "1px solid",
                                    borderColor: f === 0 ? "divider" : "transparent",
                                    "&:hover": { backgroundColor: f === 0 ? "background.paper" : "action.hover" }
                                  },
                                  children: "Synced Columns"
                                }
                              ),
                              P !== void 0 && /* @__PURE__ */ n(
                                z,
                                {
                                  size: "small",
                                  onClick: () => {
                                    ae(1), H("");
                                  },
                                  sx: {
                                    px: 1.5,
                                    py: 0.4,
                                    borderRadius: 1.5,
                                    textTransform: "none",
                                    fontSize: "0.78rem",
                                    fontWeight: f === 1 ? 700 : 500,
                                    color: f === 1 ? "primary.dark" : "text.secondary",
                                    backgroundColor: f === 1 ? "background.paper" : "transparent",
                                    boxShadow: f === 1 ? "0px 1px 4px rgba(0,0,0,0.06)" : "none",
                                    border: "1px solid",
                                    borderColor: f === 1 ? "divider" : "transparent",
                                    "&:hover": { backgroundColor: f === 1 ? "background.paper" : "action.hover" }
                                  },
                                  children: o ? "Columns" : "DB Columns"
                                }
                              )
                            ]
                          }
                        ),
                        f === 0 && b.length > 0 && j && /* @__PURE__ */ n(g, { sx: { minWidth: 200, marginLeft: 2 }, children: /* @__PURE__ */ n(
                          se,
                          {
                            size: "small",
                            options: b.map((e) => e.name),
                            value: t.getValues("primaryKey") || "",
                            readOnly: !m && !o,
                            onChange: (e, r) => {
                              const l = b.map((a) => ({
                                ...a,
                                isPrimaryKey: a.name === r
                              }));
                              D(l), t.setValue("selectedColumns", l, { shouldDirty: !0 }), t.setValue("primaryKey", r);
                            },
                            renderInput: (e) => /* @__PURE__ */ n(V, { ...e, label: "Primary Key", placeholder: "Select column" }),
                            renderOption: (e, r) => /* @__PURE__ */ n("li", { ...e, children: /* @__PURE__ */ n(A, { title: r, placement: "left", arrow: !0, children: /* @__PURE__ */ n("span", { className: "truncate max-w-[250px]", children: r }) }) })
                          }
                        ) }),
                        (o || m) && /* @__PURE__ */ n(
                          z,
                          {
                            id: "edit-button-anchor",
                            size: "small",
                            variant: "contained",
                            sx: {
                              borderRadius: 2,
                              fontWeight: 600,
                              textTransform: "none",
                              boxShadow: "none",
                              px: 1.5,
                              ml: 1
                            },
                            disabled: !!t.formState.errors.tableName,
                            onClick: async (e) => {
                              const r = e.currentTarget;
                              await t.trigger("tableName") && (le(null), te(r));
                            },
                            startIcon: /* @__PURE__ */ n(C, { iconName: "faPlus" }),
                            children: "Column"
                          }
                        ),
                        (o || m) && /* @__PURE__ */ n(
                          un,
                          {
                            id: Me,
                            open: Ve,
                            anchorEl: ke,
                            onClose: async () => {
                              te(null), t.setValue("newColumnName", ""), t.setValue("newColumnType", null), t.setValue("isPrimaryKey", !1), t.clearErrors(["newColumnName", "newColumnType"]), le(null);
                            },
                            anchorOrigin: {
                              vertical: "bottom",
                              horizontal: "left"
                            },
                            children: /* @__PURE__ */ s(
                              p,
                              {
                                container: !0,
                                spacing: 1,
                                sx: {
                                  p: 1,
                                  width: "200px",
                                  height: (Ee = t.getValues("selectedColumns")) != null && Ee.some((e) => e.isPrimaryKey) ? "180px" : "210px"
                                },
                                children: [
                                  /* @__PURE__ */ n(p, { size: 12, className: "h-fit", children: /* @__PURE__ */ n(Z, { fullWidth: !0, children: /* @__PURE__ */ n(
                                    N,
                                    {
                                      name: "newColumnName",
                                      id: "newColumnName",
                                      control: t.control,
                                      render: ({ field: { onChange: e, value: r, ...l }, fieldState: { error: a } }) => /* @__PURE__ */ n(
                                        V,
                                        {
                                          ...l,
                                          value: (r == null ? void 0 : r.trim()) || "",
                                          variant: "outlined",
                                          fullWidth: !0,
                                          size: "small",
                                          error: !!a,
                                          helperText: a == null ? void 0 : a.message,
                                          label: "Column Name",
                                          sx: {
                                            "& .MuiInputLabel-root": {
                                              fontSize: "0.9rem"
                                            },
                                            margin: 0
                                          },
                                          onChange: (i) => {
                                            e(i.target.value), on(i.target.value);
                                          }
                                        }
                                      )
                                    }
                                  ) }) }),
                                  /* @__PURE__ */ n(p, { size: 12, className: "h-fit", children: /* @__PURE__ */ n(Z, { fullWidth: !0, children: /* @__PURE__ */ n(
                                    N,
                                    {
                                      name: "newColumnType",
                                      id: "newColumnType",
                                      control: t.control,
                                      render: ({ field: { onChange: e, value: r }, fieldState: { error: l } }) => /* @__PURE__ */ n(
                                        se,
                                        {
                                          options: De || [],
                                          value: (r == null ? void 0 : r.trim()) || null,
                                          getOptionLabel: (a) => a,
                                          onChange: (a, i) => {
                                            e(i);
                                          },
                                          renderInput: (a) => /* @__PURE__ */ n(
                                            V,
                                            {
                                              ...a,
                                              size: "small",
                                              error: !!l,
                                              helperText: l == null ? void 0 : l.message,
                                              sx: {
                                                "& .MuiInputLabel-root": {
                                                  fontSize: "0.9rem"
                                                },
                                                m: 0
                                              },
                                              label: "Column Type",
                                              onChange: (i) => {
                                                e(i.target.value), t.setValue("newColumnType", i.target.value, {
                                                  shouldDirty: !0
                                                });
                                              }
                                            }
                                          )
                                        }
                                      )
                                    }
                                  ) }) }),
                                  Y === null && !t.getValues("selectedColumns").some((e) => e.isPrimaryKey) && /* @__PURE__ */ n(p, { size: 12, className: "h-fit", children: /* @__PURE__ */ n(
                                    W,
                                    {
                                      control: /* @__PURE__ */ n(
                                        N,
                                        {
                                          name: "isPrimaryKey",
                                          control: t.control,
                                          render: ({ field: { onChange: e, value: r } }) => /* @__PURE__ */ n(
                                            mn,
                                            {
                                              checked: r || !1,
                                              onChange: (l) => e(l.target.checked),
                                              sx: {
                                                margin: "0px"
                                              }
                                            }
                                          )
                                        }
                                      ),
                                      label: "Primary Key"
                                    }
                                  ) }),
                                  /* @__PURE__ */ n(p, { children: /* @__PURE__ */ n(
                                    z,
                                    {
                                      size: "small",
                                      variant: "contained",
                                      disabled: !ze || !Re || ze.trim() === "" || !De.includes(Re.trim()),
                                      onClick: async () => {
                                        const e = t.getValues("isPrimaryKey"), r = t.getValues("newColumnName"), l = t.getValues("newColumnType"), a = t.getValues("selectedColumns").some((c) => c.isPrimaryKey);
                                        let i = [...t.getValues("selectedColumns")];
                                        if (Y !== null)
                                          i[Y] = {
                                            ...i[Y],
                                            name: r,
                                            type: l
                                          };
                                        else {
                                          if (i.some((y) => y.name === r)) {
                                            q({ message: "Column already exists", type: "error" });
                                            return;
                                          }
                                          const c = {
                                            name: r,
                                            type: l
                                          };
                                          !a && e && (t.setValue("primaryKey", r), c.isPrimaryKey = !0), i.push(c);
                                        }
                                        t.setValue("selectedColumns", i, {
                                          shouldDirty: !0
                                        });
                                        const h = [...i].sort((c, y) => c.isPrimaryKey ? -1 : y.isPrimaryKey ? 1 : c.name.localeCompare(y.name));
                                        D(h), o && await he(i), le(null), te(null), f !== 0 && ae(0), t.setValue("newColumnName", " "), t.setValue("newColumnType", " "), t.setValue("isPrimaryKey", null);
                                      },
                                      children: Y !== null ? "Update" : "Add"
                                    }
                                  ) })
                                ]
                              }
                            )
                          }
                        )
                      ]
                    }
                  ),
                  /* @__PURE__ */ n(
                    hn,
                    {
                      sx: {
                        maxHeight: $ === "true" ? "112vh" : "59vh",
                        border: "1px solid",
                        borderColor: "divider",
                        borderRadius: 2
                      },
                      children: /* @__PURE__ */ s(pn, { stickyHeader: !0, children: [
                        /* @__PURE__ */ n(yn, { children: /* @__PURE__ */ n(I, { children: /* @__PURE__ */ s(M, { className: " px-3 ", sx: { backgroundColor: "white" }, children: [
                          /* @__PURE__ */ n(
                            V,
                            {
                              size: "small",
                              placeholder: "Search Columns",
                              variant: "outlined",
                              value: re,
                              onChange: (e) => {
                                H(e.target.value);
                              },
                              className: "w-full py-0.5 text-xs",
                              InputProps: {
                                startAdornment: /* @__PURE__ */ n(ge, { position: "start", children: /* @__PURE__ */ n(B, { children: /* @__PURE__ */ n(C, { iconName: "faMagnifyingGlass", className: "text-[1rem]" }) }) })
                              }
                            }
                          ),
                          !me && /* @__PURE__ */ s(u, { variant: "body2", color: "info", className: "mb-0", children: [
                            /* @__PURE__ */ n("span", { style: { color: "red" }, children: "*" }),
                            " Select a primary key for the table before submitting."
                          ] })
                        ] }) }) }),
                        f === 0 && /* @__PURE__ */ n(He, { children: b.length ? b.map((e, r) => /* @__PURE__ */ n(
                          I,
                          {
                            className: `border-b ${r === b.length - 1 ? "border-none" : "border-[#ccc]"}`,
                            sx: {
                              backgroundColor: e.onlyMobileDb ? "#d3ecc7" : e.isTypeChanged ? "rgba(255, 0, 0, 0.1)" : "#fff",
                              "&:hover": {
                                backgroundColor: e.onlyMobileDb ? "#d2f7c0" : e.isTypeChanged ? "#e79a9a" : "#e9f3ff"
                              }
                            },
                            children: /* @__PURE__ */ s(M, { className: "p-2 px-4 flex justify-between", children: [
                              /* @__PURE__ */ n(
                                A,
                                {
                                  title: e.onlyMobileDb ? "Column only present in mobile database" : e.isTypeChanged ? "Type changed" : "",
                                  placement: "top",
                                  arrow: !0,
                                  disableHoverListener: !(e.onlyMobileDb || e.isTypeChanged),
                                  children: /* @__PURE__ */ s(u, { variant: "caption", className: "w-full text-ellipsis cursor-pointer", children: [
                                    e.name,
                                    " (",
                                    e.type,
                                    ")"
                                  ] })
                                }
                              ),
                              /* @__PURE__ */ s(g, { display: "flex", alignItems: "center", children: [
                                e.isPrimaryKey && /* @__PURE__ */ n(B, { sx: { height: "1.5rem", marginRight: 0, paddingRight: 1 }, children: /* @__PURE__ */ n(C, { iconName: "faKey", className: "text-[1rem] text-[#d3bb51]" }) }),
                                (o || m) && /* @__PURE__ */ s(_, { children: [
                                  /* @__PURE__ */ s(
                                    A,
                                    {
                                      title: !o && e.isPrimaryKey && !j ? "Deletion of primary key is not allowed." : "",
                                      arrow: !0,
                                      placement: "left",
                                      children: [
                                        " ",
                                        /* @__PURE__ */ n("span", { children: /* @__PURE__ */ n(
                                          B,
                                          {
                                            sx: { height: "1.3rem", marginRight: "0" },
                                            disabled: !o && e.isPrimaryKey && !j,
                                            onClick: async () => {
                                              const l = t.getValues("selectedColumns");
                                              H("");
                                              const a = l.filter((c) => c.name !== e.name);
                                              t.setValue("selectedColumns", a, { shouldDirty: !0 });
                                              const i = re.trim().toLowerCase(), h = i ? a.filter((c) => c.name.toLowerCase().includes(i)) : a;
                                              D(h), R(
                                                (c) => c.map((y) => y.name === e.name ? { ...y, isSynced: !1 } : y)
                                              ), e.isPrimaryKey && t.setValue("primaryKey", ""), o && await he(a);
                                            },
                                            children: /* @__PURE__ */ n(
                                              C,
                                              {
                                                iconName: "faTrashCan",
                                                className: "text-[1rem] text-[#df5f5f]"
                                              }
                                            )
                                          }
                                        ) })
                                      ]
                                    }
                                  ),
                                  o && /* @__PURE__ */ n(
                                    B,
                                    {
                                      sx: { height: "1.3rem", marginRight: "0", color: "success" },
                                      onClick: () => {
                                        const a = t.getValues("selectedColumns").findIndex((i) => i.name === e.name);
                                        le(a), t.setValue("newColumnName", e.name), t.setValue("newColumnType", e.type), te(document.getElementById("edit-button-anchor") ?? null);
                                      },
                                      children: /* @__PURE__ */ n(C, { iconName: "faPen", className: "text-[1rem] text-[#4f4f4f]" })
                                    }
                                  )
                                ] })
                              ] })
                            ] })
                          },
                          r + 1
                        )) : /* @__PURE__ */ n(I, { children: /* @__PURE__ */ n(M, { colSpan: 1, className: "p-2", children: /* @__PURE__ */ s(u, { variant: "body2", children: [
                          " ",
                          t.getValues("selectedColumns").length === 0 ? "There are no columns to display." : b.length === 0 ? "No columns for search query." : null
                        ] }) }) }) }),
                        f === 1 && /* @__PURE__ */ n(He, { children: U.length ? U.map((e, r) => {
                          const l = e.name === d.primaryKey, a = e.isSynced || !1, i = /* @__PURE__ */ n(
                            fn,
                            {
                              icon: /* @__PURE__ */ n(
                                g,
                                {
                                  sx: {
                                    backgroundColor: "#001e3c",
                                    borderRadius: "50%",
                                    width: 20,
                                    height: 20,
                                    display: "flex",
                                    alignItems: "center",
                                    justifyContent: "center"
                                  },
                                  children: /* @__PURE__ */ n(C, { iconName: "faBan", size: "sm", className: "pr-[0.4px]" })
                                }
                              ),
                              checkedIcon: /* @__PURE__ */ n(
                                g,
                                {
                                  sx: {
                                    backgroundColor: "#4caf50",
                                    borderRadius: "50%",
                                    width: 20,
                                    height: 20,
                                    display: "flex",
                                    alignItems: "center",
                                    justifyContent: "center"
                                  },
                                  children: /* @__PURE__ */ n(C, { iconName: "faRotate", className: "text-white text-xs" })
                                }
                              ),
                              checked: a,
                              disabled: !m,
                              onChange: (c) => {
                                const y = c.target.checked;
                                if (!y && m && !j && !o && e.name === d.primaryKey)
                                  return;
                                const L = [...U], J = {
                                  ...L[r],
                                  isSynced: c.target.checked
                                };
                                L[r] = J, R(L);
                                const k = [...b];
                                if (y) {
                                  const x = b.some((X) => X.isPrimaryKey);
                                  e.name === d.primaryKey && !x ? (k.push({ ...J, isPrimaryKey: !0 }), t.setValue("primaryKey", e.name)) : k.push(J);
                                } else {
                                  const x = k.findIndex((X) => X.name === L[r].name);
                                  if (x > -1) {
                                    const X = k[x].isPrimaryKey;
                                    k.splice(x, 1), X && t.setValue("primaryKey", "");
                                  }
                                }
                                D(k), t.setValue("selectedColumns", k, { shouldDirty: !0 });
                              }
                            }
                          ), h = /* @__PURE__ */ n(
                            B,
                            {
                              sx: {
                                height: "2rem",
                                marginRight: m ? 1 : 0,
                                paddingRight: m ? 1 : 0
                              },
                              children: /* @__PURE__ */ n(C, { iconName: "faKey", className: "text-lg text-[#d3bb51]" })
                            }
                          );
                          return /* @__PURE__ */ n(
                            I,
                            {
                              className: `border-b ${r === U.length - 1 ? "border-none" : "border-[#ccc]"}`,
                              sx: { "&:hover": { backgroundColor: "#e9f3ff" } },
                              children: /* @__PURE__ */ s(
                                M,
                                {
                                  sx: {
                                    p: 0,
                                    px: 2,
                                    display: "flex",
                                    justifyContent: "space-between",
                                    alignItems: "center"
                                  },
                                  children: [
                                    /* @__PURE__ */ s(
                                      u,
                                      {
                                        variant: "caption",
                                        className: "w-full text-ellipsis cursor-pointer align-content-center",
                                        children: [
                                          e.name,
                                          " (",
                                          e.type,
                                          ")"
                                        ]
                                      }
                                    ),
                                    m ? l ? o ? h : /* @__PURE__ */ s(g, { sx: { display: "flex", alignItems: "center" }, children: [
                                      h,
                                      /* @__PURE__ */ n(
                                        A,
                                        {
                                          title: m && !j && e.name === d.primaryKey ? "Primary key cannot be unsynced in edit mode." : "",
                                          arrow: !0,
                                          placement: "top",
                                          children: /* @__PURE__ */ n("span", { children: i })
                                        }
                                      )
                                    ] }) : i : a ? /* @__PURE__ */ n(
                                      g,
                                      {
                                        sx: {
                                          backgroundColor: "#4caf50",
                                          borderRadius: "50%",
                                          width: 20,
                                          height: 20,
                                          display: "flex",
                                          alignItems: "center",
                                          justifyContent: "center",
                                          margin: "9px 0px"
                                        },
                                        children: /* @__PURE__ */ n(C, { iconName: "faRotate", className: "text-white text-xs" })
                                      }
                                    ) : l ? h : /* @__PURE__ */ n(
                                      g,
                                      {
                                        sx: {
                                          backgroundColor: "#001e3c",
                                          borderRadius: "50%",
                                          width: 20,
                                          height: 20,
                                          display: "flex",
                                          alignItems: "center",
                                          justifyContent: "center",
                                          margin: "9px 0px"
                                        },
                                        children: /* @__PURE__ */ n(C, { iconName: "faBan", className: "text-white pl-[0.5px]" })
                                      }
                                    )
                                  ]
                                }
                              )
                            },
                            r + 1
                          );
                        }) : /* @__PURE__ */ n(I, { children: /* @__PURE__ */ n(M, { colSpan: 1, className: "p-2", children: /* @__PURE__ */ n(u, { variant: "body2", children: "No columns for search query." }) }) }) })
                      ] })
                    }
                  )
                ] }) })
              ] })
            ] })
          }
        ),
        Pe && /* @__PURE__ */ n(gn, { open: Pe, onClose: rn, children: /* @__PURE__ */ s(g, { sx: { ...wn }, children: [
          /* @__PURE__ */ s(g, { sx: { display: "flex", justifyContent: "space-between", alignItems: "center", mb: 2 }, children: [
            /* @__PURE__ */ n(u, { variant: "h6", children: "Generated Migration Script" }),
            /* @__PURE__ */ s(u, { variant: "body2", children: [
              "From : ",
              /* @__PURE__ */ s("strong", { children: [
                "v",
                F == null ? void 0 : F.from_version
              ] }),
              " ",
              /* @__PURE__ */ n(C, { iconName: "faArrowRight", size: "sm", className: "m-[0.3rem 0.3rem 0 0.3rem]" }),
              " To :",
              " ",
              /* @__PURE__ */ s("strong", { children: [
                "v",
                F == null ? void 0 : F.to_version
              ] })
            ] })
          ] }),
          /* @__PURE__ */ n(
            oe,
            {
              height: "50vh",
              defaultLanguage: "sql",
              theme: "light",
              options: {
                fontSize: 12,
                minimap: { enabled: !0 },
                automaticLayout: !0,
                scrollBeyondLastLine: !1,
                wordWrap: "on",
                occurrencesHighlight: !1,
                selectionHighlight: !1
              },
              value: E || "",
              defaultValue: E || "",
              onChange: an,
              editorProps: { $blockScrolling: !0 }
            }
          ),
          /* @__PURE__ */ n(z, { variant: "contained", color: "primary", className: "mt-2", onClick: () => Te(null, !0), children: "OK" })
        ] }) })
      ]
    }
  );
}
const wn = {
  position: "absolute",
  top: "50%",
  left: "50%",
  transform: "translate(-50%, -50%)",
  width: "50%",
  height: "70%",
  bgcolor: "background.paper",
  boxShadow: 24,
  padding: "15px",
  borderRadius: "8px"
};
bn.propTypes = {
  tableInfo: S.any,
  table: S.any,
  isEditMode: S.bool,
  updateEditMode: S.any,
  tableCompleteInfo: S.any,
  reloadComponents: S.any,
  isCreateTableMode: S.bool,
  isSyncTableMode: S.bool,
  updateCreateTableMode: S.any,
  openDeleteModal: S.bool,
  setOpenDeleteModal: S.func
};
export {
  bn as default
};
//# sourceMappingURL=TableDetails-DzREeJkm.js.map
