import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
import { useCallback, useState } from "react";
import { useDialog, DevPanel, Icon, Stack, ButtonGroup, Button, Table, TableRow, TableHeaderCell, TableCell, useShowToast } from "@contember/ui";
import { useSessionTokenWithMeta, useProjectSlug, useSetSessionToken } from "@contember/react-client";
import { LogoutLink } from "../LogoutLink.js";
import { useOptionalIdentity } from "../Identity/useOptionalIdentity.js";
import { useCreateApiKey } from "../../tenant/mutations/apiKey/createApiKey.js";
import { EditMembership } from "../../tenant/components/member/EditMembership.js";
const IdentityPanel = () => {
  const identity = useOptionalIdentity();
  const sessionToken = useSessionTokenWithMeta();
  const dialog = useDialog();
  const open = useCallback(() => {
    dialog.openDialog({
      heading: "Login as...",
      content: () => /* @__PURE__ */ jsxDEV(LoginAsRole, {}, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 28,
        columnNumber: 19
      }, globalThis)
    });
  }, [dialog]);
  return /* @__PURE__ */ jsxDEV(DevPanel, { heading: /* @__PURE__ */ jsxDEV(Fragment, { children: [
    /* @__PURE__ */ jsxDEV(Icon, { blueprintIcon: "user" }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
      lineNumber: 33,
      columnNumber: 24
    }, globalThis),
    " Identity"
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 33,
    columnNumber: 22
  }, globalThis), children: identity ? /* @__PURE__ */ jsxDEV(Fragment, { children: /* @__PURE__ */ jsxDEV(Stack, { direction: "vertical", children: [
    /* @__PURE__ */ jsxDEV(ButtonGroup, { children: [
      /* @__PURE__ */ jsxDEV(Button, { onClick: open, children: "Switch role" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 37,
        columnNumber: 7
      }, globalThis),
      sessionToken.source === "localstorage" && /* @__PURE__ */ jsxDEV(LogoutLink, { Component: Button, children: "Logout" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 38,
        columnNumber: 50
      }, globalThis)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
      lineNumber: 36,
      columnNumber: 6
    }, globalThis),
    /* @__PURE__ */ jsxDEV(Table, { children: [
      /* @__PURE__ */ jsxDEV(TableRow, { children: [
        /* @__PURE__ */ jsxDEV(TableHeaderCell, { children: "Identity ID" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 42,
          columnNumber: 8
        }, globalThis),
        /* @__PURE__ */ jsxDEV(TableCell, { children: identity.id }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 43,
          columnNumber: 8
        }, globalThis)
      ] }, void 0, true, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 41,
        columnNumber: 7
      }, globalThis),
      /* @__PURE__ */ jsxDEV(TableRow, { children: [
        /* @__PURE__ */ jsxDEV(TableHeaderCell, { children: "Person ID" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 46,
          columnNumber: 8
        }, globalThis),
        /* @__PURE__ */ jsxDEV(TableCell, { children: identity.person ? identity.person.id : /* @__PURE__ */ jsxDEV("i", { children: "Not a person" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 47,
          columnNumber: 59
        }, globalThis) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 47,
          columnNumber: 8
        }, globalThis)
      ] }, void 0, true, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 45,
        columnNumber: 7
      }, globalThis),
      /* @__PURE__ */ jsxDEV(TableRow, { children: [
        /* @__PURE__ */ jsxDEV(TableHeaderCell, { children: "Token" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 50,
          columnNumber: 8
        }, globalThis),
        /* @__PURE__ */ jsxDEV(TableCell, { children: [
          /* @__PURE__ */ jsxDEV("span", { children: sessionToken.token }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
            lineNumber: 52,
            columnNumber: 9
          }, globalThis),
          /* @__PURE__ */ jsxDEV("br", {}, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
            lineNumber: 52,
            columnNumber: 42
          }, globalThis),
          /* @__PURE__ */ jsxDEV("span", { style: {
            color: "#999",
            fontSize: "0.7rem"
          }, children: sessionToken.source === "props" ? "stored in env" : "stored in a local storage" }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
            lineNumber: 53,
            columnNumber: 9
          }, globalThis)
        ] }, void 0, true, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 51,
          columnNumber: 8
        }, globalThis)
      ] }, void 0, true, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 49,
        columnNumber: 7
      }, globalThis),
      /* @__PURE__ */ jsxDEV(TableRow, { children: [
        /* @__PURE__ */ jsxDEV(TableHeaderCell, { children: "E-mail" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 60,
          columnNumber: 8
        }, globalThis),
        /* @__PURE__ */ jsxDEV(TableCell, { children: identity.person ? identity.person.email : /* @__PURE__ */ jsxDEV("i", { children: "Not a person" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 61,
          columnNumber: 62
        }, globalThis) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 61,
          columnNumber: 8
        }, globalThis)
      ] }, void 0, true, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 59,
        columnNumber: 7
      }, globalThis),
      /* @__PURE__ */ jsxDEV(TableRow, { children: [
        /* @__PURE__ */ jsxDEV(TableHeaderCell, { children: "Project access" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 64,
          columnNumber: 8
        }, globalThis),
        /* @__PURE__ */ jsxDEV(TableCell, { children: /* @__PURE__ */ jsxDEV("ul", { children: identity.projects.map((it) => /* @__PURE__ */ jsxDEV("li", { children: [
          /* @__PURE__ */ jsxDEV("strong", { children: it.slug }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
            lineNumber: 68,
            columnNumber: 11
          }, globalThis),
          " (",
          it.roles.join(", "),
          ")"
        ] }, it.slug, true, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 67,
          columnNumber: 39
        }, globalThis)) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 66,
          columnNumber: 9
        }, globalThis) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 65,
          columnNumber: 8
        }, globalThis)
      ] }, void 0, true, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 63,
        columnNumber: 7
      }, globalThis)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
      lineNumber: 40,
      columnNumber: 6
    }, globalThis)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 35,
    columnNumber: 5
  }, globalThis) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 34,
    columnNumber: 16
  }, globalThis) : /* @__PURE__ */ jsxDEV(Fragment, { children: "Not logged in" }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 76,
    columnNumber: 10
  }, globalThis) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 33,
    columnNumber: 3
  }, globalThis);
};
const LoginAsRole = ({}) => {
  const addToast = useShowToast();
  const [isSubmitting, setSubmitting] = useState(false);
  const [memberships, setMemberships] = useState([void 0]);
  const projectSlug = useProjectSlug();
  const createApiKey = useCreateApiKey();
  const sessionToken = useSessionTokenWithMeta();
  const setSessionToken = useSetSessionToken();
  const submit = useCallback(
    async (e) => {
      e.preventDefault();
      setSubmitting(true);
      const membershipsToSave = memberships.filter((it) => it !== void 0);
      const response = await createApiKey({
        description: "Created from panel",
        memberships: membershipsToSave,
        projectSlug
      }, {
        apiTokenOverride: sessionToken.propsToken
      });
      setSubmitting(false);
      if (!response.ok) {
        switch (response.error.code) {
          case "INVALID_MEMBERSHIP":
            return addToast({ message: `Invalid membership definition`, type: "error" });
          case "PROJECT_NOT_FOUND":
            return addToast({ message: `Project not found`, type: "error" });
        }
      } else {
        setSessionToken(response.result.apiKey.token);
      }
    },
    [memberships, createApiKey, projectSlug, sessionToken.propsToken, addToast, setSessionToken]
  );
  const editUserMembershipProps = { project: projectSlug, memberships, setMemberships };
  return /* @__PURE__ */ jsxDEV("form", { onSubmit: submit, children: /* @__PURE__ */ jsxDEV(Stack, { direction: "vertical", gap: "large", children: [
    /* @__PURE__ */ jsxDEV(EditMembership, { ...editUserMembershipProps }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
      lineNumber: 124,
      columnNumber: 5
    }, globalThis),
    /* @__PURE__ */ jsxDEV(Button, { distinction: "primary", size: "large", type: "submit", disabled: isSubmitting, children: "Login" }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
      lineNumber: 126,
      columnNumber: 5
    }, globalThis)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 123,
    columnNumber: 4
  }, globalThis) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 122,
    columnNumber: 3
  }, globalThis);
};
export {
  IdentityPanel
};
//# sourceMappingURL=IdentityPanel.js.map
