import { randomBytes } from "crypto";
import { l as logger, p as prisma, s as slugify, j as getTranslation, M as MembershipRole, z as WEBAPP_URL, a as safeStringify } from "./orgDomains-eTg-cjth.js";
import { g as getAvailabilityFromSchedule, P as ProfileRepository, D as DEFAULT_SCHEDULE } from "./user-9vti0VBh.js";
import "./createAProfileForAnExistingUser-of0FRNsg.js";
import "./bookingtimestatusdenormalized-M2Q1g19X.js";
import "./team-cSXKHM6s.js";
import "./index-browser-zve3RCF0.js";
import { TRPCError } from "@trpc/server";
import { q as sendTeamInviteEmail } from "./email-manager-mHSBIi6g.js";
const isEmail = (str) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str);
logger.getSubLogger({ prefix: ["inviteMember.utils"] });
function checkInputEmailIsValid(email) {
  if (!isEmail(email))
    throw new TRPCError({
      code: "BAD_REQUEST",
      message: `Invite failed because ${email} is not a valid email address`
    });
}
async function createNewUsersConnectToOrgIfExists({
  invitations,
  isOrg,
  teamId,
  parentId,
  autoAcceptEmailDomain,
  orgConnectInfoByUsernameOrEmail,
  isPlatformManaged,
  timeFormat,
  weekStart,
  timeZone,
  language,
  creationSource
}) {
  invitations.forEach((invitation) => checkInputEmailIsValid(invitation.usernameOrEmail));
  const createdUsers = await prisma.$transaction(
    async (tx) => {
      const createdUsers2 = [];
      for (let index = 0; index < invitations.length; index++) {
        const invitation = invitations[index];
        const { orgId, autoAccept } = orgConnectInfoByUsernameOrEmail[invitation.usernameOrEmail];
        const [emailUser, emailDomain] = invitation.usernameOrEmail.split("@");
        const [domainName, TLD] = emailDomain.split(".");
        const orgMemberUsername = emailDomain === autoAcceptEmailDomain ? slugify(emailUser) : slugify(`${emailUser}-${domainName}${isPlatformManaged ? `-${TLD}` : ""}`);
        const regularTeamMemberUsername = null;
        const isBecomingAnOrgMember = parentId || isOrg;
        const defaultAvailability = getAvailabilityFromSchedule(DEFAULT_SCHEDULE);
        const t = await getTranslation(language ?? "en", "common");
        const createdUser = await tx.user.create({
          data: {
            username: isBecomingAnOrgMember ? orgMemberUsername : regularTeamMemberUsername,
            email: invitation.usernameOrEmail,
            verified: true,
            invitedTo: teamId,
            isPlatformManaged: !!isPlatformManaged,
            timeFormat,
            weekStart,
            timeZone,
            creationSource,
            organizationId: orgId || null,
            // If the user is invited to a child team, they are automatically added to the parent org
            ...orgId ? {
              profiles: {
                createMany: {
                  data: [
                    {
                      uid: ProfileRepository.generateProfileUid(),
                      username: orgMemberUsername,
                      organizationId: orgId
                    }
                  ]
                }
              }
            } : null,
            teams: {
              create: {
                teamId,
                role: invitation.role,
                accepted: autoAccept
                // If the user is invited to a child team, they are automatically accepted
              }
            },
            ...!isPlatformManaged ? {
              schedules: {
                create: {
                  name: t("default_schedule_name"),
                  availability: {
                    createMany: {
                      data: defaultAvailability.map((schedule) => ({
                        days: schedule.days,
                        startTime: schedule.startTime,
                        endTime: schedule.endTime
                      }))
                    }
                  }
                }
              }
            } : {}
          }
        });
        if (parentId) {
          await tx.membership.create({
            data: {
              createdAt: /* @__PURE__ */ new Date(),
              teamId: parentId,
              userId: createdUser.id,
              role: MembershipRole.MEMBER,
              accepted: autoAccept
            }
          });
        }
        createdUsers2.push(createdUser);
      }
      return createdUsers2;
    },
    { timeout: 1e4 }
  );
  return createdUsers;
}
async function sendSignupToOrganizationEmail({
  usernameOrEmail,
  team,
  translation,
  inviterName,
  teamId,
  isOrg
}) {
  try {
    const token = randomBytes(32).toString("hex");
    await prisma.verificationToken.create({
      data: {
        identifier: usernameOrEmail,
        token,
        expires: new Date((/* @__PURE__ */ new Date()).setHours(168)),
        // +1 week
        team: {
          connect: {
            id: teamId
          }
        }
      }
    });
    await sendTeamInviteEmail({
      language: translation,
      from: inviterName || `${team.name}'s admin`,
      to: usernameOrEmail,
      teamName: team.name,
      joinLink: `${WEBAPP_URL}/signup?token=${token}&callbackUrl=/getting-started`,
      isCalcomMember: false,
      isOrg,
      parentTeamName: team?.parent?.name,
      isAutoJoin: false,
      isExistingUserMovedToOrg: false,
      // For a new user there is no prev and new links.
      prevLink: null,
      newLink: null
    });
  } catch (error) {
    logger.error(
      "Failed to send signup to organization email",
      safeStringify({
        usernameOrEmail,
        orgId: teamId
      }),
      error
    );
  }
}
export {
  createNewUsersConnectToOrgIfExists as c,
  sendSignupToOrganizationEmail as s
};
