"use strict";
const crypto = require("crypto");
const index = require("./orgDomains-W_rFTMBa.cjs");
const user = require("./user-cPqQS_l_.cjs");
require("./createAProfileForAnExistingUser-o_l6wwt3.cjs");
require("./bookingtimestatusdenormalized-xGCrLyyb.cjs");
require("./team-HqS6vjRW.cjs");
require("./index-browser-rYj6TkGS.cjs");
const server = require("@trpc/server");
const index$1 = require("./email-manager-w-Fd9mb6.cjs");
const isEmail = (str) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str);
index.logger.getSubLogger({ prefix: ["inviteMember.utils"] });
function checkInputEmailIsValid(email) {
  if (!isEmail(email))
    throw new server.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 index.prisma.$transaction(
    async (tx) => {
      const createdUsers2 = [];
      for (let index$12 = 0; index$12 < invitations.length; index$12++) {
        const invitation = invitations[index$12];
        const { orgId, autoAccept } = orgConnectInfoByUsernameOrEmail[invitation.usernameOrEmail];
        const [emailUser, emailDomain] = invitation.usernameOrEmail.split("@");
        const [domainName, TLD] = emailDomain.split(".");
        const orgMemberUsername = emailDomain === autoAcceptEmailDomain ? index.slugify(emailUser) : index.slugify(`${emailUser}-${domainName}${isPlatformManaged ? `-${TLD}` : ""}`);
        const regularTeamMemberUsername = null;
        const isBecomingAnOrgMember = parentId || isOrg;
        const defaultAvailability = user.getAvailabilityFromSchedule(user.DEFAULT_SCHEDULE);
        const t = await index.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: user.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: index.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 = crypto.randomBytes(32).toString("hex");
    await index.prisma.verificationToken.create({
      data: {
        identifier: usernameOrEmail,
        token,
        expires: new Date((/* @__PURE__ */ new Date()).setHours(168)),
        // +1 week
        team: {
          connect: {
            id: teamId
          }
        }
      }
    });
    await index$1.sendTeamInviteEmail({
      language: translation,
      from: inviterName || `${team.name}'s admin`,
      to: usernameOrEmail,
      teamName: team.name,
      joinLink: `${index.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) {
    index.logger.error(
      "Failed to send signup to organization email",
      index.safeStringify({
        usernameOrEmail,
        orgId: teamId
      }),
      error
    );
  }
}
exports.createNewUsersConnectToOrgIfExists = createNewUsersConnectToOrgIfExists;
exports.sendSignupToOrganizationEmail = sendSignupToOrganizationEmail;
