"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const reactClient = require("@contember/react-client");
const ui = require("@contember/ui");
const lucideReact = require("lucide-react");
const react = require("react");
const LogoutLink = require("../LogoutLink.cjs");
const reactDevbar = require("@contember/react-devbar");
const variables = require("../../tenant/lib/variables.cjs");
const reactIdentity = require("@contember/react-identity");
const createApiKey = require("../../tenant/mutations/apiKey/createApiKey.cjs");
const EditMembership = require("../../tenant/components/member/EditMembership.cjs");
const useSingleTenantMutation = require("../../tenant/lib/useSingleTenantMutation.cjs");
const IdentityPanel = () => {
  const identity = reactIdentity.useIdentity();
  const sessionToken = reactClient.useSessionTokenWithMeta();
  const dialog = ui.useDialog();
  const openSwitchRole = react.useCallback(() => {
    dialog.openDialog({
      heading: "Login as...",
      content: () => /* @__PURE__ */ jsxDevRuntime.jsxDEV(LoginAsRole, {}, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 30,
        columnNumber: 19
      }, void 0)
    });
  }, [dialog]);
  const openLoginEmail = react.useCallback(() => {
    dialog.openDialog({
      heading: "Login by email",
      content: () => /* @__PURE__ */ jsxDevRuntime.jsxDEV(LoginWithEmail, {}, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 36,
        columnNumber: 19
      }, void 0)
    });
  }, [dialog]);
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactDevbar.DevPanel, { icon: /* @__PURE__ */ jsxDevRuntime.jsxDEV(lucideReact.UserCircleIcon, {}, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 41,
    columnNumber: 19
  }, void 0), heading: "Identity", children: identity ? /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Stack, { children: [
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.ButtonGroup, { children: [
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Button, { onClick: openSwitchRole, children: "Switch role" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 45,
        columnNumber: 7
      }, void 0),
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Button, { onClick: openLoginEmail, children: "Login with email" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 46,
        columnNumber: 7
      }, void 0),
      sessionToken.source === "localstorage" && /* @__PURE__ */ jsxDevRuntime.jsxDEV(LogoutLink.LogoutLink, { Component: ui.Button, children: "Logout" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 47,
        columnNumber: 50
      }, void 0)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
      lineNumber: 44,
      columnNumber: 6
    }, void 0),
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Table, { children: [
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableRow, { children: [
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableHeaderCell, { children: "Identity ID" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 51,
          columnNumber: 8
        }, void 0),
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableCell, { children: identity.id }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 52,
          columnNumber: 8
        }, void 0)
      ] }, void 0, true, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 50,
        columnNumber: 7
      }, void 0),
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableRow, { children: [
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableHeaderCell, { children: "Person ID" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 55,
          columnNumber: 8
        }, void 0),
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableCell, { children: identity.person ? identity.person.id : /* @__PURE__ */ jsxDevRuntime.jsxDEV("i", { children: "Not a person" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 56,
          columnNumber: 59
        }, void 0) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 56,
          columnNumber: 8
        }, void 0)
      ] }, void 0, true, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 54,
        columnNumber: 7
      }, void 0),
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableRow, { children: [
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableHeaderCell, { children: "Token" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 59,
          columnNumber: 8
        }, void 0),
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableCell, { children: [
          /* @__PURE__ */ jsxDevRuntime.jsxDEV("span", { children: sessionToken.token }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
            lineNumber: 61,
            columnNumber: 9
          }, void 0),
          /* @__PURE__ */ jsxDevRuntime.jsxDEV("br", {}, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
            lineNumber: 61,
            columnNumber: 42
          }, void 0),
          /* @__PURE__ */ jsxDevRuntime.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: 62,
            columnNumber: 9
          }, void 0)
        ] }, void 0, true, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 60,
          columnNumber: 8
        }, void 0)
      ] }, void 0, true, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 58,
        columnNumber: 7
      }, void 0),
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableRow, { children: [
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableHeaderCell, { children: "E-mail" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 69,
          columnNumber: 8
        }, void 0),
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableCell, { children: identity.person ? identity.person.email : /* @__PURE__ */ jsxDevRuntime.jsxDEV("i", { children: "Not a person" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 70,
          columnNumber: 62
        }, void 0) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 70,
          columnNumber: 8
        }, void 0)
      ] }, void 0, true, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 68,
        columnNumber: 7
      }, void 0),
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableRow, { children: [
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableHeaderCell, { children: "Project access" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 73,
          columnNumber: 8
        }, void 0),
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TableCell, { children: /* @__PURE__ */ jsxDevRuntime.jsxDEV("ul", { children: identity.projects.map((it) => /* @__PURE__ */ jsxDevRuntime.jsxDEV("li", { children: [
          /* @__PURE__ */ jsxDevRuntime.jsxDEV("strong", { children: it.slug }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
            lineNumber: 77,
            columnNumber: 11
          }, void 0),
          " (",
          it.roles.join(", "),
          ")"
        ] }, it.slug, true, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 76,
          columnNumber: 39
        }, void 0)) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 75,
          columnNumber: 9
        }, void 0) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 74,
          columnNumber: 8
        }, void 0)
      ] }, void 0, true, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
        lineNumber: 72,
        columnNumber: 7
      }, void 0)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
      lineNumber: 49,
      columnNumber: 6
    }, void 0)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 43,
    columnNumber: 5
  }, void 0) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 42,
    columnNumber: 16
  }, void 0) : /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: "Not logged in" }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 85,
    columnNumber: 10
  }, void 0) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 41,
    columnNumber: 3
  }, void 0);
};
const LoginAsRole = ({}) => {
  const addToast = ui.useShowToast();
  const [isSubmitting, setSubmitting] = react.useState(false);
  const [memberships, setMemberships] = react.useState([void 0]);
  const projectSlug = reactClient.useProjectSlug();
  const createApiKey$1 = createApiKey.useCreateApiKey();
  const sessionToken = reactClient.useSessionTokenWithMeta();
  const setSessionToken = reactClient.useSetSessionToken();
  const submit = react.useCallback(
    async (e) => {
      e.preventDefault();
      setSubmitting(true);
      const membershipsToSave = memberships.filter((it) => it !== void 0);
      const response = await createApiKey$1({
        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$1, projectSlug, sessionToken.propsToken, addToast, setSessionToken]
  );
  const editUserMembershipProps = { project: projectSlug, memberships, setMemberships };
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV("form", { onSubmit: submit, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Stack, { gap: "large", children: [
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(EditMembership.EditMembership, { ...editUserMembershipProps }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
      lineNumber: 133,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Button, { display: "block", distinction: "primary", type: "submit", disabled: isSubmitting, children: "Login" }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
      lineNumber: 135,
      columnNumber: 5
    }, void 0)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 132,
    columnNumber: 4
  }, void 0) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 131,
    columnNumber: 3
  }, void 0);
};
const createSessionKeyVariables = {
  email: variables.GQLVariable.Required(variables.GQLVariable.String)
};
const useCreateSessionToken = () => {
  return useSingleTenantMutation.useSingleTenantMutation(`createSessionToken(email: $email) {
		ok
		error {
			code
		}
		result {
			token
		}
	}`, createSessionKeyVariables);
};
const LoginWithEmail = () => {
  const [email, setEmail] = react.useState("");
  const sessionToken = reactClient.useSessionTokenWithMeta();
  const addToast = ui.useShowToast();
  const [isSubmitting, setSubmitting] = react.useState(false);
  const setSessionToken = reactClient.useSetSessionToken();
  const createSessionToken = useCreateSessionToken();
  const submit = react.useCallback(async (e) => {
    e.preventDefault();
    setSubmitting(true);
    const response = await createSessionToken({
      email
    }, {
      apiTokenOverride: sessionToken.propsToken
    });
    setSubmitting(false);
    if (!response.ok) {
      switch (response.error.code) {
        case "UNKNOWN_EMAIL":
          return addToast({ message: "Person with given email not found.", type: "error" });
      }
    } else {
      setSessionToken(response.result.token);
    }
  }, [addToast, createSessionToken, email, sessionToken.propsToken, setSessionToken]);
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: /* @__PURE__ */ jsxDevRuntime.jsxDEV("form", { onSubmit: submit, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Stack, { children: [
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.FieldContainer, { label: "E-mail", children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.EmailInput, { value: email, onChange: (e) => setEmail(e), notNull: true }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
      lineNumber: 198,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
      lineNumber: 197,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Button, { display: "block", type: "submit", disabled: isSubmitting, children: "Login" }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
      lineNumber: 200,
      columnNumber: 5
    }, void 0)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 196,
    columnNumber: 4
  }, void 0) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 195,
    columnNumber: 3
  }, void 0) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
    lineNumber: 194,
    columnNumber: 9
  }, void 0);
};
exports.IdentityPanel = IdentityPanel;
//# sourceMappingURL=IdentityPanel.cjs.map
