import { G as J, t as K, u as Q, W as Z, k as s, X as ee, l as e, Y as X, Z as $, $ as I, a0 as se, B as te, T as R, a1 as ae, C as E, a2 as oe, a3 as ie, a4 as re, a5 as q, a6 as ce, a7 as de, o as B, a8 as le, a9 as pe, aa as ue, ab as he, V as D, J as V, K as W, Q as H, F as me, ac as ge, ad as fe, ae as Ce, af as ye, ag as xe, ah as N, ai as Se, w as P, aj as Re, ak as Te, al as be, am as L, I as ze, an as ve, ao as Ie, ap as ke, aq as Fe, ar as De, as as Ee, at as Oe, au as Y, av as je, aw as we } from "./App-1cb933e1.mjs";
import * as M from "react";
import m from "react";
import { S as Me, a as _e } from "./Grid-20959940.mjs";
import "react-dom";
import "@feedloop/formula-editor";
import "@feedloop/expression-parser";
function Ge(t) {
  return M.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),
    M.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 Le(t) {
  return M.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),
    M.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 A({
  viewsDisclosure: t,
  role: i,
  permissionFunc: g,
  table: n,
  tableName: r,
  title: d,
  view: o
}) {
  const z = J.useSelectState((c) => c.settings), {
    rolesAndPermission: h
  } = K(), v = Q(), _ = Z(), U = m.useRef(null), O = m.useMemo(() => {
    var c;
    return o ? (c = o.query) == null ? void 0 : c.fields.map((f) => {
      var l, p;
      return {
        name: f,
        type: (p = (l = n == null ? void 0 : n.columns) == null ? void 0 : l.find((u) => u.name === f)) == null ? void 0 : p.type
      };
    }) : n == null ? void 0 : n.columns;
  }, [o == null ? void 0 : o.query, n == null ? void 0 : n.columns]), k = m.useCallback(async (c, f, l, p, u) => {
    let y, S;
    c.target.checked ? (y = "Create", S = {
      prop: "actions",
      value: [p]
    }) : (y = "Drop", S = {
      prop: "action",
      value: p
    });
    try {
      await v.migratePermission(y, f, l, S, u);
    } catch (T) {
      _({
        description: T.response.data.message,
        status: "error",
        isClosable: !0
      });
    }
    h == null || h.mutate();
  }, [h]), G = m.useCallback(async (c, f, l, p, u) => {
    try {
      await v.migratePermission("Alter", c, f, {
        prop: "action",
        value: l
      }, u == null ? void 0 : u.name, p);
    } catch {
    }
    h == null || h.mutate();
  }, [i]), j = (c) => {
    var l;
    return c ? ((l = Object.values(c)[0]) == null ? void 0 : l.length) > 0 : void 0;
  };
  return /* @__PURE__ */ s(ee, {
    style: o != null && o.name ? {
      display: t.isOpen ? "revert" : "none"
    } : {},
    children: [/* @__PURE__ */ e(X, {
      cursor: n != null && n.views.length ? "pointer" : "auto",
      onClick: n != null && n.views.length ? t.onToggle : () => {
      },
      children: /* @__PURE__ */ s($, {
        paddingLeft: o != null && o.name ? "30px" : "0",
        children: [/* @__PURE__ */ e(I, {
          as: o != null && o.name ? Me : se,
          color: "#6F809A",
          fontSize: "md"
        }), /* @__PURE__ */ e(te, {
          children: /* @__PURE__ */ e(R, {
            children: d
          })
        }), /* @__PURE__ */ e(ae, {}), (n == null ? void 0 : n.views.length) && /* @__PURE__ */ e(I, {
          as: _e,
          fontSize: "md",
          className: t.isOpen ? "rotate down" : "rotate",
          _focus: {
            outline: "transparent"
          },
          "aria-label": "expand-icon",
          tabIndex: -1
        })]
      })
    }), ne.map((c, f) => {
      var y, S, T;
      const l = (y = i == null ? void 0 : i.permissions) == null ? void 0 : y.some((a) => g(a, c)), p = (T = (S = i == null ? void 0 : i.permissions) == null ? void 0 : S.find((a) => g(a, c))) == null ? void 0 : T.condition, u = j(p);
      return /* @__PURE__ */ e(X, {
        w: "5px",
        children: /* @__PURE__ */ s(E, {
          position: "relative",
          children: [/* @__PURE__ */ e(oe, {
            onChange: (a) => {
              k(a, i == null ? void 0 : i.name, r || d, c, o != null && o.name ? d : void 0);
            },
            isChecked: l,
            display: u ? "none" : "flex",
            isDisabled: z.ENV_STAGE === "production"
          }), p && z.ENV_STAGE !== "production" && /* @__PURE__ */ e(ie, {
            initRef: U,
            fields: O,
            enableCounter: !1,
            applyCondition: G,
            placement: "left",
            role: i == null ? void 0 : i.name,
            table: r,
            action: c,
            permissions: p,
            buttonSize: "sm",
            variant: u ? "standalone" : "children",
            buttonText: /* @__PURE__ */ e(I, {
              as: re,
              fontSize: u ? "reg" : "sm"
            }),
            height: "5",
            view: o
          })]
        })
      }, f);
    })]
  });
}
function Be({
  table: t,
  role: i
}) {
  var n;
  const g = q();
  return /* @__PURE__ */ s(m.Fragment, {
    children: [/* @__PURE__ */ e(A, {
      role: i,
      title: t == null ? void 0 : t.name,
      permissionFunc: (r, d) => r.action === d && r.table.name === t.name && !r.view,
      table: t,
      viewsDisclosure: g
    }), (n = t.views) == null ? void 0 : n.map((r, d) => /* @__PURE__ */ e(A, {
      role: i,
      title: (r == null ? void 0 : r.name) || "",
      permissionFunc: (o, z) => {
        var h;
        return o.action === z && o.table.name === t.name && ((h = o.view) == null ? void 0 : h.name) === r.name;
      },
      view: r,
      tableName: t.name,
      viewsDisclosure: g
    }))]
  });
}
function Ve({
  handleMigrateRole: t,
  isLoading: i
}) {
  const g = m.useRef(null), n = J.useSelectState((d) => d.settings), r = q();
  return /* @__PURE__ */ s(ce, {
    closeOnBlur: !0,
    isOpen: r.isOpen,
    onOpen: r.onOpen,
    onClose: i ? () => {
    } : r.onClose,
    initialFocusRef: g,
    isLazy: !0,
    children: [/* @__PURE__ */ e(de, {
      children: /* @__PURE__ */ e(B, {
        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($, {
          padding: "8px 16px",
          children: [/* @__PURE__ */ e(E, {
            children: /* @__PURE__ */ e(I, {
              as: le,
              fontSize: "reg"
            })
          }), /* @__PURE__ */ e(R, {
            children: "Add new role"
          })]
        })
      })
    }), /* @__PURE__ */ e(pe, {
      children: /* @__PURE__ */ s(ue, {
        p: 3,
        _focus: {
          shadow: "0px 4px 16px rgba(0, 30, 77, 0.08)"
        },
        children: [/* @__PURE__ */ e(he, {}), /* @__PURE__ */ e(D, {
          display: "flex",
          alignItems: "flex-start",
          width: "100%",
          children: /* @__PURE__ */ s("form", {
            onSubmit: (d) => {
              d.preventDefault(), t("Create", d.target[0].value, r);
            },
            style: {
              width: "100%"
            },
            children: [/* @__PURE__ */ s(V, {
              mb: 4,
              children: [/* @__PURE__ */ e(W, {
                htmlFor: "name",
                children: "Add New Role"
              }), /* @__PURE__ */ e(H, {
                id: "name",
                name: "name",
                type: "text",
                placeholder: "Role name",
                autoComplete: "off",
                borderRadius: "md",
                ref: g,
                "data-cy": "add-roles-name"
              })]
            }), /* @__PURE__ */ e(B, {
              size: "sm",
              colorScheme: "blue",
              type: "submit",
              children: "Create"
            })]
          })
        })]
      })
    })]
  });
}
const ne = ["insight", "select", "update", "delete", "insert"], We = /* @__PURE__ */ new Set(["public", "admin"]);
function Pe() {
  var p, u, y, S, T;
  const {
    rolesAndPermission: t,
    schema: i
  } = K(), g = me(), [n, r] = m.useState(!1), d = q(), [o, z] = m.useState(0), [h, v] = m.useState(""), {
    search: _
  } = ge(), O = new URLSearchParams(_).get("id"), k = Z(), G = Q(), [j, c] = m.useState(!1);
  m.useEffect(() => {
    var a, C;
    z(((C = (a = t == null ? void 0 : t.data) == null ? void 0 : a.roles) == null ? void 0 : C.findIndex((x) => x.id === O)) || 0);
  }, [(p = t == null ? void 0 : t.data) == null ? void 0 : p.roles, O]);
  const f = m.useMemo(() => n ? (setTimeout(() => {
    r(!1);
  }, 1e3), "Copied") : "Copy", [n]), l = m.useCallback(async (a, C, x) => {
    var F;
    if (!C || C === "") {
      k({
        description: "Role name can't be empty",
        status: "error",
        isClosable: !0
      });
      return;
    }
    try {
      c(!0);
      const b = await G.migrateRole(a, C);
      console.log(b), k({
        description: `Successfully ${{
          Create: "added",
          Drop: "deleted"
        }[a]} ${C} role`,
        status: "success",
        isClosable: !0
      }), c(!1);
    } catch (b) {
      c(!1), k({
        description: b.response.data.message.split("-")[0],
        status: "error",
        isClosable: !0
      });
    }
    t == null || t.mutate(), x == null || x.onClose(), v(""), a === "Drop" && g.push(`/tables/users/roles?id=${(F = t == null ? void 0 : t.data) == null ? void 0 : F.roles[0].id}`);
  }, [t]);
  return /* @__PURE__ */ s(E, {
    flexDirection: "column",
    paddingTop: "20px",
    paddingX: "100px",
    children: [/* @__PURE__ */ s(D, {
      alignItems: "normal",
      width: "full",
      children: [/* @__PURE__ */ e(R, {
        fontSize: "20px",
        as: "b",
        children: "User Roles"
      }), /* @__PURE__ */ e(fe, {}), /* @__PURE__ */ s(Ce, {
        orientation: "vertical",
        variant: "unstyled",
        size: "md",
        paddingTop: "20px",
        index: o,
        children: [/* @__PURE__ */ s(ye, {
          width: "300px",
          gap: 2,
          children: [(y = (u = t == null ? void 0 : t.data) == null ? void 0 : u.roles) == null ? void 0 : y.map((a, C) => /* @__PURE__ */ s(xe, {
            _selected: {
              color: "#0065FF",
              bg: "#E6F0FF",
              borderRadius: "4px"
            },
            _focus: {
              borderWidth: "0px"
            },
            onClick: () => {
              g.push(`/tables/users/roles?id=${a.id}`);
            },
            justifyContent: "start",
            css: N`
                  #delete-roles-button {
                    transition: opacity 200ms ease;
                    opacity: 0;
                  }
                  &:hover #delete-roles-button {
                    opacity: 1;
                    color: #6f809a;
                  }
                `,
            children: [/* @__PURE__ */ s($, {
              children: [/* @__PURE__ */ e(E, {
                children: /* @__PURE__ */ e(I, {
                  color: "#6F809A",
                  fontSize: "md",
                  as: a.name === "admin" ? Se : a.name === "public" ? Ge : Le
                })
              }), /* @__PURE__ */ e(R, {
                children: P.capitalize(a.name)
              })]
            }), /* @__PURE__ */ e(ae, {}), !We.has(a.name) && /* @__PURE__ */ e(I, {
              as: Re,
              fontSize: "sm",
              color: "#6F809A",
              id: "delete-roles-button",
              onClick: (x) => {
                x.stopPropagation(), v(a.name), d.onOpen();
              },
              cursor: "pointer",
              "data-cy": "delete-roles"
            })]
          }, a.id)), /* @__PURE__ */ e(E, {
            children: /* @__PURE__ */ e(Ve, {
              handleMigrateRole: l,
              isLoading: j
            })
          })]
        }), /* @__PURE__ */ e(Te, {
          paddingTop: "0px",
          children: (T = (S = t == null ? void 0 : t.data) == null ? void 0 : S.roles) == null ? void 0 : T.map((a) => {
            var C, x, F;
            return /* @__PURE__ */ e(be, {
              paddingTop: "inherit",
              alignItems: "start",
              overflowY: "auto",
              overflowX: "hidden",
              maxHeight: "75vh",
              children: /* @__PURE__ */ s(D, {
                alignItems: "inherit",
                spacing: "24px",
                children: [/* @__PURE__ */ s(D, {
                  gap: 2,
                  children: [/* @__PURE__ */ s(L, {
                    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(V, {
                    children: [/* @__PURE__ */ e(W, {
                      children: "Role ID"
                    }), /* @__PURE__ */ s(ze, {
                      children: [/* @__PURE__ */ e(H, {
                        pr: "4.5rem",
                        value: a == null ? void 0 : a.id,
                        isReadOnly: !0,
                        placeholder: "Enter password"
                      }), /* @__PURE__ */ e(ve, {
                        width: "4.5rem",
                        children: /* @__PURE__ */ e(Ie, {
                          label: f,
                          placement: "top",
                          closeOnClick: !1,
                          hasArrow: !0,
                          children: /* @__PURE__ */ e(B, {
                            variant: "link",
                            colorScheme: n ? "green" : "blue",
                            _focus: {
                              borderWidth: "0px"
                            },
                            onClick: () => {
                              ke(a == null ? void 0 : a.id), r(!0);
                            },
                            children: n ? /* @__PURE__ */ e(Fe, {}) : /* @__PURE__ */ e(De, {})
                          })
                        })
                      })]
                    })]
                  }), /* @__PURE__ */ s(V, {
                    children: [/* @__PURE__ */ e(W, {
                      children: "Role name"
                    }), /* @__PURE__ */ e(H, {
                      value: a == null ? void 0 : a.name,
                      isReadOnly: !0
                    })]
                  })]
                }), /* @__PURE__ */ s(D, {
                  gap: 2,
                  alignItems: "inherit",
                  width: "full",
                  children: [/* @__PURE__ */ s(L, {
                    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(te, {
                    width: "full",
                    paddingBottom: "20px",
                    children: (a == null ? void 0 : a.name) === "admin" ? /* @__PURE__ */ s(R, {
                      children: ["User with role of ", /* @__PURE__ */ e("b", {
                        children: "admin"
                      }), " can access everything inside this project."]
                    }) : /* @__PURE__ */ s(Ee, {
                      variant: "simple",
                      overflow: "auto",
                      maxHeight: "60vh",
                      css: N`
                            tr {
                              &:hover {
                                background-color: #f6f6f6;
                              }
                            }
                          `,
                      children: [/* @__PURE__ */ e(Oe, {
                        position: "sticky",
                        top: 0,
                        bgColor: "#f6f6f6",
                        zIndex: 1,
                        color: "#767676",
                        children: /* @__PURE__ */ s(ee, {
                          children: [/* @__PURE__ */ e(Y, {
                            children: "Table"
                          }), ne.map((b, w) => /* @__PURE__ */ e(Y, {
                            children: /* @__PURE__ */ e(L, {
                              justifyContent: "center",
                              children: P.capitalize(b)
                            })
                          }, w))]
                        })
                      }), /* @__PURE__ */ e(je, {
                        children: (F = (x = (C = i == null ? void 0 : i.data) == null ? void 0 : C.data) == null ? void 0 : x.tables) == null ? void 0 : F.map((b, w) => /* @__PURE__ */ e(Be, {
                          table: b,
                          role: a
                        }, w))
                      })]
                    })
                  })]
                })]
              })
            }, a.id);
          })
        })]
      })]
    }), /* @__PURE__ */ e(we, {
      modalDisclosure: d,
      modalType: "deleteRole",
      title: `${h}`,
      isLoading: j,
      cancelAction: () => {
        v(""), d.onClose();
      },
      submit: () => l("Drop", h, d)
    })]
  });
}
export {
  ne as actionColumns,
  Pe as default
};
