import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
import { useSessionTokenWithMeta, useProjectSlug, useSetSessionToken } from "@contember/react-client";
import { useDialog, Stack, ButtonGroup, Button, Table, TableRow, TableHeaderCell, TableCell, useShowToast, FieldContainer, EmailInput } from "@contember/ui";
import { UserCircleIcon } from "lucide-react";
import { useCallback, useState } from "react";
import { LogoutLink } from "../LogoutLink.js";
import { DevPanel } from "@contember/react-devbar";
import { GQLVariable } from "../../tenant/lib/variables.js";
import { useIdentity } from "@contember/react-identity";
import { useCreateApiKey } from "../../tenant/mutations/apiKey/createApiKey.js";
import { EditMembership } from "../../tenant/components/member/EditMembership.js";
import { useSingleTenantMutation } from "../../tenant/lib/useSingleTenantMutation.js";
const IdentityPanel = () => {
  const identity = useIdentity();
  const sessionToken = useSessionTokenWithMeta();
  const dialog = useDialog();
  const openSwitchRole = 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: 30,
        columnNumber: 19
      }, void 0)
    });
  }, [dialog]);
  const openLoginEmail = useCallback(() => {
    dialog.openDialog({
      heading: "Login by email",
      content: () => /* @__PURE__ */ 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__ */ jsxDEV(DevPanel, { icon: /* @__PURE__ */ jsxDEV(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__ */ jsxDEV(Fragment, { children: /* @__PURE__ */ jsxDEV(Stack, { children: [
    /* @__PURE__ */ jsxDEV(ButtonGroup, { children: [
      /* @__PURE__ */ jsxDEV(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__ */ jsxDEV(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__ */ jsxDEV(LogoutLink, { Component: 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__ */ 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: 51,
          columnNumber: 8
        }, void 0),
        /* @__PURE__ */ jsxDEV(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__ */ jsxDEV(TableRow, { children: [
        /* @__PURE__ */ jsxDEV(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__ */ 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: 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__ */ jsxDEV(TableRow, { children: [
        /* @__PURE__ */ jsxDEV(TableHeaderCell, { children: "Token" }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
          lineNumber: 59,
          columnNumber: 8
        }, void 0),
        /* @__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: 61,
            columnNumber: 9
          }, void 0),
          /* @__PURE__ */ jsxDEV("br", {}, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
            lineNumber: 61,
            columnNumber: 42
          }, void 0),
          /* @__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: 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__ */ jsxDEV(TableRow, { children: [
        /* @__PURE__ */ jsxDEV(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__ */ 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: 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__ */ jsxDEV(TableRow, { children: [
        /* @__PURE__ */ jsxDEV(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__ */ 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: 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__ */ jsxDEV(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 = 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, { gap: "large", children: [
    /* @__PURE__ */ jsxDEV(EditMembership, { ...editUserMembershipProps }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/IdentityPanel.tsx",
      lineNumber: 133,
      columnNumber: 5
    }, void 0),
    /* @__PURE__ */ jsxDEV(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: GQLVariable.Required(GQLVariable.String)
};
const useCreateSessionToken = () => {
  return useSingleTenantMutation(`createSessionToken(email: $email) {
		ok
		error {
			code
		}
		result {
			token
		}
	}`, createSessionKeyVariables);
};
const LoginWithEmail = () => {
  const [email, setEmail] = useState("");
  const sessionToken = useSessionTokenWithMeta();
  const addToast = useShowToast();
  const [isSubmitting, setSubmitting] = useState(false);
  const setSessionToken = useSetSessionToken();
  const createSessionToken = useCreateSessionToken();
  const submit = 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__ */ jsxDEV(Fragment, { children: /* @__PURE__ */ jsxDEV("form", { onSubmit: submit, children: /* @__PURE__ */ jsxDEV(Stack, { children: [
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "E-mail", children: /* @__PURE__ */ jsxDEV(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__ */ jsxDEV(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);
};
export {
  IdentityPanel
};
//# sourceMappingURL=IdentityPanel.js.map
