"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const schedule = require("./schedule-aPT1I2PX.cjs");
const user = require("./user-cPqQS_l_.cjs");
const server = require("@trpc/server");
const getUserAvailability = require("./getUserAvailability-iWQ_bLeP.cjs");
const updateSchedule = async ({ input, user: user$1, prisma }) => {
  const availability = input.schedule ? user.getAvailabilityFromSchedule(input.schedule) : (input.dateOverrides || []).map((dateOverride) => ({
    startTime: dateOverride.start,
    endTime: dateOverride.end,
    date: dateOverride.start,
    days: []
  }));
  const userSchedule = await prisma.schedule.findUnique({
    where: {
      id: input.scheduleId
    },
    select: {
      userId: true,
      name: true,
      id: true
    }
  });
  if (!userSchedule) {
    throw new server.TRPCError({
      code: "UNAUTHORIZED"
    });
  }
  if (userSchedule?.userId !== user$1.id) {
    const hasEditPermission = await schedule.hasEditPermissionForUserID({
      ctx: {
        user: user$1
      },
      input: { memberId: userSchedule.userId }
    });
    if (!hasEditPermission) {
      throw new server.TRPCError({
        code: "UNAUTHORIZED"
      });
    }
  }
  let updatedUser;
  if (input.isDefault) {
    const setupDefault = await schedule.setupDefaultSchedule(user$1.id, input.scheduleId, prisma);
    updatedUser = setupDefault;
  }
  if (!input.name) {
    return {
      schedule: userSchedule,
      isDefault: updatedUser ? updatedUser.defaultScheduleId === input.scheduleId : user$1.defaultScheduleId === input.scheduleId
    };
  }
  const schedule$1 = await prisma.schedule.update({
    where: {
      id: input.scheduleId
    },
    data: {
      timeZone: input.timeZone,
      name: input.name,
      availability: {
        deleteMany: {
          scheduleId: {
            equals: input.scheduleId
          }
        },
        createMany: {
          data: [
            ...availability,
            ...(input.dateOverrides || []).map((override) => ({
              date: override.start,
              startTime: override.start,
              endTime: override.end
            }))
          ]
        }
      }
    },
    select: {
      id: true,
      userId: true,
      name: true,
      availability: true,
      timeZone: true,
      eventType: {
        select: {
          id: true,
          eventName: true
        }
      }
    }
  });
  const userAvailability = schedule.transformScheduleToAvailabilityForAtom(schedule$1);
  return {
    schedule: schedule$1,
    availability: userAvailability,
    timeZone: schedule$1.timeZone || user$1.timeZone,
    isDefault: updatedUser ? updatedUser.defaultScheduleId === schedule$1.id : user$1.defaultScheduleId === schedule$1.id,
    prevDefaultId: user$1.defaultScheduleId,
    currentDefaultId: updatedUser ? updatedUser.defaultScheduleId : user$1.defaultScheduleId
  };
};
exports.ScheduleRepository = schedule.ScheduleRepository;
exports.UserAvailabilityService = getUserAvailability.UserAvailabilityService;
exports.updateSchedule = updateSchedule;
