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