import { css as J } from "@emotion/react";
import { G as ie, x as ce, n as le, Z as de, j as s, $ as pe, a as e, a0 as Q, a1 as q, a2 as z, a3 as ge, o as I, T as R, a4 as ue, C as M, a5 as fe, a6 as Ce, a7 as xe, a8 as U, a9 as ye, aa as Se, B as H, ab as Re, ac as ze, ad as be, ae as ke, V as j, O as _, P as $, W as G, u as ve, af as Te, ag as Ie, ah as Fe, ai as Ee, aj as we, ak as Oe, I as ee, al as te, am as je, z as ne, an as Me, ao as De, ap as ae, aq as _e, ar as Ge, F as W, as as Le, at as Be, au as Ve, av as We, aw as He, ax as oe, ay as $e, az as qe } from "./App-664af23e.mjs";
import * as L from "react";
import m from "react";
import { S as Ue, a as Pe } from "./Grid-20959940.mjs";
import "react-dom";
import "@emotion/styled";
import "@feedloop/formula-editor";
import "@feedloop/expression-parser";
import "base-64";
import "react-mentions";
function Ae(t) {
  return L.createElement(
    "svg",
    Object.assign({ xmlns: "http://www.w3.org/2000/svg", strokeWidth: 0, viewBox: "0 0 24 24", fill: "currentColor", stroke: "currentColor", width: "1em", height: "1em" }, t),
    L.createElement("path", { d: "M12 2C6.486 2 2 6.486 2 12s4.486 10 10 10 10-4.486 10-10S17.514 2 12 2zm7.931 9h-2.764a14.67 14.67 0 00-1.792-6.243A8.013 8.013 0 0119.931 11zM12.53 4.027c1.035 1.364 2.427 3.78 2.627 6.973H9.03c.139-2.596.994-5.028 2.451-6.974.172-.01.344-.026.519-.026.179 0 .354.016.53.027zm-3.842.7C7.704 6.618 7.136 8.762 7.03 11H4.069a8.013 8.013 0 014.619-6.273zM4.069 13h2.974c.136 2.379.665 4.478 1.556 6.23A8.01 8.01 0 014.069 13zm7.381 6.973C10.049 18.275 9.222 15.896 9.041 13h6.113c-.208 2.773-1.117 5.196-2.603 6.972-.182.012-.364.028-.551.028-.186 0-.367-.016-.55-.027zm4.011-.772c.955-1.794 1.538-3.901 1.691-6.201h2.778a8.005 8.005 0 01-4.469 6.201z" })
  );
}
function Xe(t) {
  return L.createElement(
    "svg",
    Object.assign({ xmlns: "http://www.w3.org/2000/svg", strokeWidth: 0, viewBox: "0 0 24 24", fill: "currentColor", stroke: "currentColor", width: "1em", height: "1em" }, t),
    L.createElement("path", { d: "M21 20a1 1 0 01-2 0c0-2.757-2.243-5-5-5h-4c-2.757 0-5 2.243-5 5a1 1 0 01-2 0c0-3.859 3.14-7 7-7h4c3.859 0 7 3.141 7 7zM12 4c-1.654 0-3 1.346-3 3s1.346 3 3 3 3-1.346 3-3-1.346-3-3-3zm0-2a5 5 0 11-.001 10.001A5 5 0 0112 2z" })
  );
}
function se({
  viewsDisclosure: t,
  role: i,
  permissionFunc: y,
  table: n,
  tableName: l,
  title: u,
  view: o
}) {
  const C = ie.useSelectState((c) => c.settings), {
    rolesAndPermission: x
  } = ce(), B = le(), F = de(), w = m.useRef(null), V = m.useMemo(() => {
    var c;
    return o ? (c = o.query) == null ? void 0 : c.fields.map((r) => {
      var g, f;
      return {
        name: r,
        type: (f = (g = n == null ? void 0 : n.columns) == null ? void 0 : g.find((h) => h.name === r)) == null ? void 0 : f.type
      };
    }) : n == null ? void 0 : n.columns;
  }, [o == null ? void 0 : o.query, n == null ? void 0 : n.columns]), E = m.useCallback(async (c, r, g, f, h) => {
    let S, k;
    c.target.checked ? (S = "Create", k = {
      prop: "actions",
      value: [f]
    }) : (S = "Drop", k = {
      prop: "action",
      value: f
    });
    try {
      await B.migratePermission(S, r, g, k, h);
    } catch (T) {
      F({
        description: T.response.data.message,
        status: "error",
        isClosable: !0
      });
    }
    x == null || x.mutate();
  }, [x]), O = m.useCallback(async (c, r, g, f, h) => {
    try {
      await B.migratePermission("Alter", c, r, {
        prop: "action",
        value: g
      }, h == null ? void 0 : h.name, f);
    } catch {
    }
    x == null || x.mutate();
  }, [i]), b = (c) => {
    var g;
    return c ? ((g = Object.values(c)[0]) == null ? void 0 : g.length) > 0 : void 0;
  };
  return /* @__PURE__ */ s(pe, {
    style: o != null && o.name ? {
      display: t.isOpen ? "revert" : "none"
    } : {},
    children: [/* @__PURE__ */ e(Q, {
      cursor: n != null && n.views.length ? "pointer" : "auto",
      onClick: n != null && n.views.length ? t.onToggle : () => {
      },
      children: /* @__PURE__ */ s(q, {
        paddingLeft: o != null && o.name ? "30px" : "0",
        children: [/* @__PURE__ */ e(z, {
          as: o != null && o.name ? Ue : ge,
          color: "#6F809A",
          fontSize: "md"
        }), /* @__PURE__ */ e(I, {
          children: /* @__PURE__ */ e(R, {
            children: u
          })
        }), /* @__PURE__ */ e(ue, {}), (n == null ? void 0 : n.views.length) && /* @__PURE__ */ e(z, {
          as: Pe,
          fontSize: "md",
          className: t.isOpen ? "rotate down" : "rotate",
          _focus: {
            outline: "transparent"
          },
          "aria-label": "expand-icon",
          tabIndex: -1
        })]
      })
    }), he.map((c, r) => {
      var S, k, T;
      const g = (S = i == null ? void 0 : i.permissions) == null ? void 0 : S.some((v) => y(v, c)), f = (T = (k = i == null ? void 0 : i.permissions) == null ? void 0 : k.find((v) => y(v, c))) == null ? void 0 : T.condition, h = b(f);
      return /* @__PURE__ */ e(Q, {
        w: "5px",
        children: /* @__PURE__ */ s(M, {
          position: "relative",
          children: [/* @__PURE__ */ e(fe, {
            onChange: (v) => {
              E(v, i == null ? void 0 : i.name, l || u, c, o != null && o.name ? u : void 0);
            },
            isChecked: g,
            display: h ? "none" : "flex",
            isDisabled: C.ENV_STAGE === "production"
          }), f && C.ENV_STAGE !== "production" && /* @__PURE__ */ e(Ce, {
            initRef: w,
            fields: V,
            enableCounter: !1,
            applyCondition: O,
            placement: "left",
            role: i == null ? void 0 : i.name,
            table: l || (n == null ? void 0 : n.name),
            action: c,
            permissions: f,
            buttonSize: "sm",
            variant: h ? "standalone" : "children",
            buttonText: /* @__PURE__ */ e(z, {
              as: xe,
              fontSize: h ? "reg" : "sm"
            }),
            height: "5",
            view: o
          })]
        })
      }, r);
    })]
  });
}
function Ne({
  table: t,
  role: i
}) {
  var n;
  const y = U();
  return /* @__PURE__ */ s(m.Fragment, {
    children: [/* @__PURE__ */ e(se, {
      role: i,
      title: t == null ? void 0 : t.name,
      permissionFunc: (l, u) => l.action === u && l.table.name === t.name && !l.view,
      table: t,
      viewsDisclosure: y
    }), (n = t.views) == null ? void 0 : n.map((l, u) => /* @__PURE__ */ e(se, {
      role: i,
      title: (l == null ? void 0 : l.name) || "",
      permissionFunc: (o, C) => {
        var x;
        return o.action === C && o.table.name === t.name && ((x = o.view) == null ? void 0 : x.name) === l.name;
      },
      view: l,
      tableName: t.name,
      viewsDisclosure: y
    }))]
  });
}
function Ke({
  handleMigrateRole: t,
  isLoading: i
}) {
  const y = m.useRef(null), n = ie.useSelectState((u) => u.settings), l = U();
  return /* @__PURE__ */ s(ye, {
    closeOnBlur: !0,
    isOpen: l.isOpen,
    onOpen: l.onOpen,
    onClose: i ? () => {
    } : l.onClose,
    initialFocusRef: y,
    isLazy: !0,
    children: [/* @__PURE__ */ e(Se, {
      children: /* @__PURE__ */ e(H, {
        variant: "unstyled",
        height: "min-content",
        minWidth: "min-content",
        _focus: {
          borderWidth: "0px"
        },
        color: "#0065FF",
        size: "sm",
        marginRight: "4px",
        "data-cy": "create-roles",
        hidden: n.ENV_STAGE === "production",
        children: /* @__PURE__ */ s(q, {
          padding: "8px 16px",
          children: [/* @__PURE__ */ e(M, {
            children: /* @__PURE__ */ e(z, {
              as: Re,
              fontSize: "reg"
            })
          }), /* @__PURE__ */ e(R, {
            children: "Add new role"
          })]
        })
      })
    }), /* @__PURE__ */ e(ze, {
      children: /* @__PURE__ */ s(be, {
        p: 3,
        _focus: {
          shadow: "0px 4px 16px rgba(0, 30, 77, 0.08)"
        },
        children: [/* @__PURE__ */ e(ke, {}), /* @__PURE__ */ e(j, {
          display: "flex",
          alignItems: "flex-start",
          width: "100%",
          children: /* @__PURE__ */ s("form", {
            onSubmit: (u) => {
              u.preventDefault(), t("Create", u.target[0].value, l);
            },
            style: {
              width: "100%"
            },
            children: [/* @__PURE__ */ s(_, {
              mb: 4,
              children: [/* @__PURE__ */ e($, {
                htmlFor: "name",
                children: "Add New Role"
              }), /* @__PURE__ */ e(G, {
                id: "name",
                name: "name",
                type: "text",
                placeholder: "Role name",
                autoComplete: "off",
                borderRadius: "md",
                ref: y,
                "data-cy": "add-roles-name"
              })]
            }), /* @__PURE__ */ e(H, {
              size: "sm",
              colorScheme: "blue",
              type: "submit",
              children: "Create"
            })]
          })
        })]
      })
    })]
  });
}
const he = ["insight", "select", "update", "delete", "insert"], re = /* @__PURE__ */ new Set(["public", "admin"]);
function it() {
  var T, v, P, A, X, N, K;
  const {
    rolesAndPermission: t,
    schema: i
  } = ce(), y = ve(), [n, l] = m.useState(!1), u = U(), [o, C] = m.useState(""), {
    search: x
  } = Te(), F = new URLSearchParams(x).get("id"), w = de(), V = le(), [E, O] = m.useState(!1), [b, c] = m.useState(""), r = m.useMemo(() => {
    var a, d;
    if ((a = t == null ? void 0 : t.data) != null && a.roles)
      return (d = t == null ? void 0 : t.data) == null ? void 0 : d.roles.find((p) => p.id === F);
  }, [(T = t == null ? void 0 : t.data) == null ? void 0 : T.roles, F]), g = m.useMemo(() => {
    var a, d;
    return ((d = (a = t == null ? void 0 : t.data) == null ? void 0 : a.roles) == null ? void 0 : d.findIndex((p) => p.id === F)) || 0;
  }, [(v = t == null ? void 0 : t.data) == null ? void 0 : v.roles, F]), f = m.useMemo(() => n ? (setTimeout(() => {
    l(!1);
  }, 1e3), "Copied") : "Copy", [n]), h = m.useCallback(async (a, d, p, Y) => {
    var Z;
    if (!d || d === "" || Y === "") {
      w({
        description: "Role name can't be empty",
        status: "error",
        isClosable: !0
      });
      return;
    }
    try {
      O(!0);
      let D = {
        name: d
      };
      a === "Alter" && (D = {
        from: d,
        to: Y
      });
      const me = await V.migrateRole(a, D);
      console.log(me), w({
        description: `Successfully ${{
          Create: "added",
          Drop: "deleted",
          Alter: "edited"
        }[a]} ${d} role`,
        status: "success",
        isClosable: !0
      }), O(!1);
    } catch (D) {
      O(!1), w({
        description: D.response.data.message.split("-")[0],
        status: "error",
        isClosable: !0
      });
    }
    t == null || t.mutate(), p == null || p.onClose(), C(""), c(""), a === "Drop" && y.push(`/tables/users/roles?id=${(Z = t == null ? void 0 : t.data) == null ? void 0 : Z.roles[0].id}`);
  }, [t]), S = m.useCallback((a) => {
    a.stopPropagation(), C(a.target.value);
  }, []), k = {
    autoFocus: !0,
    onKeyDown: async (a) => {
      switch (a.key) {
        case "Enter":
          await h("Alter", b, null, o);
          break;
        case "Escape":
          c(""), C("");
          break;
      }
    }
  };
  return /* @__PURE__ */ s(M, {
    flexDirection: "column",
    paddingTop: "20px",
    paddingX: "100px",
    children: [/* @__PURE__ */ s(j, {
      alignItems: "normal",
      width: "full",
      children: [/* @__PURE__ */ e(R, {
        fontSize: "20px",
        as: "b",
        children: "User Roles"
      }), /* @__PURE__ */ e(Ie, {}), /* @__PURE__ */ s(I, {
        d: "flex",
        gap: "16px",
        children: [/* @__PURE__ */ e(I, {
          w: "300px",
          children: /* @__PURE__ */ e(Fe, {
            orientation: "vertical",
            variant: "unstyled",
            size: "md",
            paddingTop: "20px",
            index: g,
            children: /* @__PURE__ */ s(Ee, {
              width: "300px",
              gap: 2,
              children: [(A = (P = t == null ? void 0 : t.data) == null ? void 0 : P.roles) == null ? void 0 : A.map((a, d) => /* @__PURE__ */ s(we, {
                _selected: {
                  color: "#0065FF",
                  bg: "#E6F0FF",
                  borderRadius: "4px"
                },
                _focus: {
                  borderWidth: "0px"
                },
                onClick: () => {
                  y.push(`/tables/users/roles?id=${a.id}`);
                },
                justifyContent: "start",
                css: J`
                        #roles-button {
                          transition: opacity 200ms ease;
                          opacity: 0;
                        }
                        &:hover #roles-button {
                          opacity: 1;
                          color: #6f809a;
                        }
                      `,
                children: [/* @__PURE__ */ s(q, {
                  children: [/* @__PURE__ */ e(M, {
                    children: /* @__PURE__ */ e(z, {
                      color: "#6F809A",
                      fontSize: "md",
                      as: a.name === "admin" ? Oe : a.name === "public" ? Ae : Xe
                    })
                  }), b === a.name ? /* @__PURE__ */ e(_, {
                    children: /* @__PURE__ */ s(ee, {
                      borderRadius: "4px",
                      children: [/* @__PURE__ */ e(G, {
                        ...k,
                        value: o,
                        onClick: (p) => p.stopPropagation(),
                        onChange: S,
                        size: "sm",
                        fontSize: "sm",
                        isDisabled: E,
                        width: "full",
                        border: "transparent",
                        letterSpacing: "0.3",
                        paddingInline: "1.5",
                        background: "white"
                      }), E && /* @__PURE__ */ e(te, {
                        pointerEvents: "none",
                        children: /* @__PURE__ */ e(je, {
                          isIndeterminate: !0,
                          size: 21.25,
                          color: "#6F809A"
                        }),
                        marginTop: "0.5px",
                        height: "100%",
                        marginX: "6px"
                      })]
                    })
                  }) : /* @__PURE__ */ e(R, {
                    children: ne.capitalize(a.name)
                  })]
                }), /* @__PURE__ */ e(ue, {}), b === a.name && /* @__PURE__ */ s(Me, {
                  children: [/* @__PURE__ */ e(z, {
                    as: De,
                    fontSize: "sm",
                    color: "red",
                    marginRight: 2,
                    onClick: (p) => {
                      p.stopPropagation(), C(""), c("");
                    },
                    cursor: "pointer",
                    "data-cy": "save-roles"
                  }), /* @__PURE__ */ e(z, {
                    as: ae,
                    fontSize: "sm",
                    color: "green",
                    marginRight: 2,
                    onClick: (p) => {
                      p.stopPropagation(), h("Alter", b, null, o);
                    },
                    cursor: "pointer",
                    "data-cy": "save-roles"
                  })]
                }), !re.has(a.name) && !b && /* @__PURE__ */ e(z, {
                  as: _e,
                  fontSize: "sm",
                  color: "#6F809A",
                  id: "roles-button",
                  marginRight: 2,
                  onClick: (p) => {
                    p.stopPropagation(), C(a.name), c(a.name);
                  },
                  cursor: "pointer",
                  "data-cy": "edit-roles"
                }), !re.has(a.name) && !b && /* @__PURE__ */ e(z, {
                  as: Ge,
                  fontSize: "sm",
                  color: "#6F809A",
                  id: "roles-button",
                  onClick: (p) => {
                    p.stopPropagation(), C(a.name), u.onOpen();
                  },
                  cursor: "pointer",
                  "data-cy": "delete-roles"
                })]
              }, d)), /* @__PURE__ */ e(M, {
                children: /* @__PURE__ */ e(Ke, {
                  handleMigrateRole: h,
                  isLoading: E
                })
              })]
            })
          })
        }), /* @__PURE__ */ e(I, {
          flexGrow: !0,
          children: /* @__PURE__ */ e(I, {
            paddingTop: "0px",
            children: !!r && /* @__PURE__ */ e(I, {
              paddingTop: "inherit",
              alignItems: "start",
              overflowY: "auto",
              overflowX: "hidden",
              maxHeight: "75vh",
              children: /* @__PURE__ */ s(j, {
                alignItems: "inherit",
                spacing: "24px",
                children: [/* @__PURE__ */ s(j, {
                  gap: 2,
                  children: [/* @__PURE__ */ s(W, {
                    direction: "column",
                    gap: 1,
                    children: [/* @__PURE__ */ e(R, {
                      fontSize: "md",
                      as: "b",
                      children: "General"
                    }), /* @__PURE__ */ e(R, {
                      color: "#747474",
                      fontSize: "reg",
                      children: "Manage available method for signing in to your project"
                    })]
                  }), /* @__PURE__ */ s(_, {
                    children: [/* @__PURE__ */ e($, {
                      children: "Role ID"
                    }), /* @__PURE__ */ s(ee, {
                      children: [/* @__PURE__ */ e(G, {
                        pr: "4.5rem",
                        value: r == null ? void 0 : r.id,
                        isReadOnly: !0,
                        placeholder: "Enter password"
                      }), /* @__PURE__ */ e(te, {
                        width: "4.5rem",
                        children: /* @__PURE__ */ e(Le, {
                          label: f,
                          placement: "top",
                          closeOnClick: !1,
                          hasArrow: !0,
                          children: /* @__PURE__ */ e(H, {
                            variant: "link",
                            colorScheme: n ? "green" : "blue",
                            _focus: {
                              borderWidth: "0px"
                            },
                            onClick: () => {
                              Be(r == null ? void 0 : r.id), l(!0);
                            },
                            children: n ? /* @__PURE__ */ e(ae, {}) : /* @__PURE__ */ e(Ve, {})
                          })
                        })
                      })]
                    })]
                  }), /* @__PURE__ */ s(_, {
                    children: [/* @__PURE__ */ e($, {
                      children: "Role name"
                    }), /* @__PURE__ */ e(G, {
                      value: r == null ? void 0 : r.name,
                      isReadOnly: !0
                    })]
                  })]
                }), /* @__PURE__ */ s(j, {
                  gap: 2,
                  alignItems: "inherit",
                  width: "full",
                  children: [/* @__PURE__ */ s(W, {
                    direction: "column",
                    gap: 1,
                    children: [/* @__PURE__ */ e(R, {
                      fontSize: "md",
                      as: "b",
                      children: "Tables and Views Permission"
                    }), /* @__PURE__ */ e(R, {
                      color: "#747474",
                      fontSize: "reg",
                      children: "Manage available method for signing in to your project"
                    })]
                  }), /* @__PURE__ */ e(I, {
                    width: "full",
                    paddingBottom: "20px",
                    children: (r == null ? void 0 : r.name) === "admin" ? /* @__PURE__ */ s(R, {
                      children: ["User with role of ", /* @__PURE__ */ e("b", {
                        children: "admin"
                      }), " can access everything inside this project."]
                    }) : /* @__PURE__ */ s(We, {
                      variant: "simple",
                      overflow: "auto",
                      maxHeight: "60vh",
                      css: J`
                              tr {
                                &:hover {
                                  background-color: #f6f6f6;
                                }
                              }
                            `,
                      children: [/* @__PURE__ */ e(He, {
                        position: "sticky",
                        top: 0,
                        bgColor: "#f6f6f6",
                        zIndex: 1,
                        color: "#767676",
                        children: /* @__PURE__ */ s(pe, {
                          children: [/* @__PURE__ */ e(oe, {
                            children: "Table"
                          }), he.map((a, d) => /* @__PURE__ */ e(oe, {
                            children: /* @__PURE__ */ e(W, {
                              justifyContent: "center",
                              children: ne.capitalize(a)
                            })
                          }, d))]
                        })
                      }), /* @__PURE__ */ e($e, {
                        children: (K = (N = (X = i == null ? void 0 : i.data) == null ? void 0 : X.data) == null ? void 0 : N.tables) == null ? void 0 : K.map((a, d) => /* @__PURE__ */ e(Ne, {
                          table: a,
                          role: r
                        }, d))
                      })]
                    })
                  })]
                })]
              })
            })
          })
        })]
      })]
    }), /* @__PURE__ */ e(qe, {
      modalDisclosure: u,
      modalType: "deleteRole",
      title: `${o}`,
      isLoading: E,
      cancelAction: () => {
        C(""), u.onClose();
      },
      submit: () => h("Drop", o, u)
    })]
  });
}
export {
  he as actionColumns,
  it as default
};
