import { c as getParsedTeam, d as whereClauseForOrgWithSlugOrRequestedSlug } from "./user-sJCjuzB2.js";
import { l as logger, t as teamMetadataSchema, M as MembershipRole } from "./i18n-tiaWqB1b.js";
logger.getSubLogger({ prefix: ["repository", "team"] });
const teamSelect = {
  id: true,
  name: true,
  slug: true,
  logoUrl: true,
  parentId: true,
  metadata: true,
  isOrganization: true,
  organizationSettings: true,
  isPlatform: true
};
class TeamRepository {
  constructor(prismaClient) {
    this.prismaClient = prismaClient;
  }
  async findById({ id }) {
    const team = await this.prismaClient.team.findUnique({
      where: {
        id
      },
      select: teamSelect
    });
    if (!team) {
      return null;
    }
    return getParsedTeam(team);
  }
  async findAllByParentId({
    parentId,
    select = teamSelect
  }) {
    return await this.prismaClient.team.findMany({
      where: {
        parentId
      },
      select
    });
  }
  async findByIdAndParentId({
    id,
    parentId,
    select = teamSelect
  }) {
    return await this.prismaClient.team.findFirst({
      where: {
        id,
        parentId
      },
      select
    });
  }
  async findFirstBySlugAndParentSlug({
    slug,
    parentSlug,
    select = teamSelect
  }) {
    return await this.prismaClient.team.findFirst({
      where: {
        slug,
        parent: parentSlug ? whereClauseForOrgWithSlugOrRequestedSlug(parentSlug) : null
      },
      select
    });
  }
  async deleteById({ id }) {
    const deletedTeam = await this.prismaClient.$transaction(async (tx) => {
      await tx.eventType.deleteMany({
        where: {
          teamId: id,
          schedulingType: "MANAGED"
        }
      });
      await tx.membership.deleteMany({
        where: {
          teamId: id
        }
      });
      const deletedTeam2 = await tx.team.delete({
        where: {
          id
        }
      });
      return deletedTeam2;
    });
    return deletedTeam;
  }
  async findTeamWithMembers(teamId) {
    return await this.prismaClient.team.findUnique({
      where: { id: teamId },
      select: {
        members: {
          select: {
            accepted: true
          }
        },
        id: true,
        metadata: true,
        parentId: true,
        isOrganization: true
      }
    });
  }
  async findTeamsByUserId({ userId, includeOrgs }) {
    const memberships = await this.prismaClient.membership.findMany({
      where: {
        // Show all the teams this user belongs to regardless of the team being part of the user's org or not
        // We don't want to restrict in the listing here. If we need to restrict a situation where a user is part of the org along with being part of a non-org team, we should do that instead of filtering out from here
        // This became necessary when we started migrating user to Org, without migrating some teams of the user to the org
        // Also, we would allow a user to be part of multiple orgs, then also it would be necessary.
        userId
      },
      include: {
        team: {
          select: {
            id: true,
            name: true,
            slug: true,
            logoUrl: true,
            isOrganization: true,
            metadata: true,
            inviteTokens: true,
            parent: true,
            parentId: true
          }
        }
      },
      orderBy: { role: "desc" }
    });
    return memberships.filter((mmship) => {
      if (includeOrgs)
        return true;
      return !mmship.team.isOrganization;
    }).map(({ team: { inviteTokens, ...team }, ...membership }) => ({
      role: membership.role,
      accepted: membership.accepted,
      ...team,
      metadata: teamMetadataSchema.parse(team.metadata),
      /** To prevent breaking we only return non-email attached token here, if we have one */
      inviteToken: inviteTokens.find((token) => token.identifier === `invite-link-for-teamId-${team.id}`)
    }));
  }
  async findTeamWithOrganizationSettings(teamId) {
    return await this.prismaClient.team.findUnique({
      where: { id: teamId },
      select: {
        parent: {
          select: {
            isOrganization: true,
            organizationSettings: true
          }
        }
      }
    });
  }
  async findParentOrganizationByTeamId(teamId) {
    const team = await this.prismaClient.team.findUnique({
      where: {
        id: teamId
      },
      select: {
        parent: {
          select: {
            id: true
          }
        }
      }
    });
    return team?.parent;
  }
  async findOrganizationSettingsBySlug({ slug }) {
    return await this.prismaClient.team.findFirst({
      where: {
        slug,
        isOrganization: true
      },
      select: {
        organizationSettings: {
          select: {
            adminGetsNoSlotsNotification: true
          }
        }
      }
    });
  }
  async findTeamSlugById({ id }) {
    return await this.prismaClient.team.findUnique({
      where: {
        id
      },
      select: {
        slug: true
      }
    });
  }
  async getTeamByIdIfUserIsAdmin({ userId, teamId }) {
    return await this.prismaClient.team.findUnique({
      where: {
        id: teamId
      },
      select: {
        id: true,
        metadata: true,
        members: {
          where: {
            userId,
            role: {
              in: [MembershipRole.ADMIN, MembershipRole.OWNER]
            }
          }
        }
      }
    });
  }
}
export {
  TeamRepository as T
};
