import { calendarDbService } from '../chunk-OCIJT5VO.mjs';
export { calendarConfigs, calendarConfigsRelations, calendarDbService, calendarEvents, calendarEventsRelations, eventShares, eventSharesRelations, recurrenceRules, recurrenceRulesRelations, reminders, remindersRelations } from '../chunk-OCIJT5VO.mjs';
import '../chunk-AIKEVVDR.mjs';
import '../chunk-BJTO5JO5.mjs';

// src/calendar/server.ts
var DEFAULT_CALENDAR_CONFIG = {
  firstDayOfWeek: 1,
  // 周一开始
  workingHours: {
    start: "09:00",
    end: "18:00"
  },
  timeZone: "Asia/Shanghai",
  dateFormat: "YYYY-MM-DD",
  timeFormat: "HH:mm",
  defaultView: "month",
  defaultEventColor: "#3B82F6",
  weekends: true,
  eventColors: {
    blue: "#3B82F6",
    green: "#10B981",
    purple: "#8B5CF6",
    red: "#EF4444",
    yellow: "#F59E0B",
    pink: "#EC4899",
    indigo: "#6366F1",
    gray: "#6B7280"
  }
};
function validateEventData(eventData) {
  const errors = [];
  if (!eventData.title || typeof eventData.title !== "string" || eventData.title.trim() === "") {
    errors.push("\u4E8B\u4EF6\u6807\u9898\u4E0D\u80FD\u4E3A\u7A7A");
  }
  if (!eventData.startTime) {
    errors.push("\u5F00\u59CB\u65F6\u95F4\u4E0D\u80FD\u4E3A\u7A7A");
  }
  if (!eventData.endTime) {
    errors.push("\u7ED3\u675F\u65F6\u95F4\u4E0D\u80FD\u4E3A\u7A7A");
  }
  if (eventData.startTime && eventData.endTime) {
    const startTime = new Date(eventData.startTime);
    const endTime = new Date(eventData.endTime);
    if (isNaN(startTime.getTime())) {
      errors.push("\u5F00\u59CB\u65F6\u95F4\u683C\u5F0F\u65E0\u6548");
    }
    if (isNaN(endTime.getTime())) {
      errors.push("\u7ED3\u675F\u65F6\u95F4\u683C\u5F0F\u65E0\u6548");
    }
    if (startTime.getTime() >= endTime.getTime()) {
      errors.push("\u7ED3\u675F\u65F6\u95F4\u5FC5\u987B\u665A\u4E8E\u5F00\u59CB\u65F6\u95F4");
    }
  }
  if (eventData.color && !/^#[0-9A-Fa-f]{6}$/.test(eventData.color)) {
    errors.push("\u989C\u8272\u683C\u5F0F\u65E0\u6548\uFF0C\u8BF7\u4F7F\u7528\u5341\u516D\u8FDB\u5236\u683C\u5F0F\uFF08\u5982 #FF0000\uFF09");
  }
  if (!eventData.userId || typeof eventData.userId !== "number" || eventData.userId <= 0) {
    errors.push("\u7528\u6237ID\u65E0\u6548");
  }
  return {
    valid: errors.length === 0,
    errors
  };
}
function validateRecurrenceData(recurrenceData) {
  const errors = [];
  if (!recurrenceData.ruleType) {
    errors.push("\u91CD\u590D\u7C7B\u578B\u4E0D\u80FD\u4E3A\u7A7A");
  }
  const validRuleTypes = ["daily", "weekly", "monthly", "yearly", "custom"];
  if (recurrenceData.ruleType && !validRuleTypes.includes(recurrenceData.ruleType)) {
    errors.push("\u91CD\u590D\u7C7B\u578B\u65E0\u6548");
  }
  if (!recurrenceData.interval || recurrenceData.interval < 1) {
    errors.push("\u91CD\u590D\u95F4\u9694\u5FC5\u987B\u5927\u4E8E0");
  }
  if (recurrenceData.endDate && recurrenceData.count) {
    errors.push("\u4E0D\u80FD\u540C\u65F6\u8BBE\u7F6E\u7ED3\u675F\u65E5\u671F\u548C\u91CD\u590D\u6B21\u6570");
  }
  if (recurrenceData.endDate) {
    const endDate = new Date(recurrenceData.endDate);
    if (isNaN(endDate.getTime())) {
      errors.push("\u7ED3\u675F\u65E5\u671F\u683C\u5F0F\u65E0\u6548");
    }
  }
  if (recurrenceData.count && (recurrenceData.count < 1 || recurrenceData.count > 1e3)) {
    errors.push("\u91CD\u590D\u6B21\u6570\u5FC5\u987B\u57281-1000\u4E4B\u95F4");
  }
  return {
    valid: errors.length === 0,
    errors
  };
}
function generateRecurrenceInstances(baseEvent, recurrenceRule, maxInstances = 100) {
  const instances = [];
  const startTime = new Date(baseEvent.startTime);
  const endTime = new Date(baseEvent.endTime);
  const duration = endTime.getTime() - startTime.getTime();
  let currentDate = new Date(startTime);
  let count = 0;
  const endDate = recurrenceRule.endDate ? new Date(recurrenceRule.endDate) : null;
  const maxCount = recurrenceRule.count || maxInstances;
  while (count < maxCount) {
    if (endDate && currentDate > endDate) {
      break;
    }
    instances.push({
      startTime: new Date(currentDate),
      endTime: new Date(currentDate.getTime() + duration)
    });
    count++;
    switch (recurrenceRule.ruleType) {
      case "daily":
        currentDate.setDate(currentDate.getDate() + recurrenceRule.interval);
        break;
      case "weekly":
        currentDate.setDate(currentDate.getDate() + 7 * recurrenceRule.interval);
        break;
      case "monthly":
        currentDate.setMonth(currentDate.getMonth() + recurrenceRule.interval);
        break;
      case "yearly":
        currentDate.setFullYear(currentDate.getFullYear() + recurrenceRule.interval);
        break;
    }
  }
  return instances;
}
function calculateReminderTime(eventStartTime, reminderOffset) {
  return new Date(eventStartTime.getTime() - reminderOffset);
}
function createErrorResponse(message, errors) {
  return Response.json(
    {
      success: false,
      error: message,
      errors
    },
    { status: 400 }
  );
}
function createSuccessResponse(data, message) {
  return Response.json({
    success: true,
    data,
    message
  });
}
async function checkEventPermission(eventId, userId, requiredPermission = "read") {
  try {
    const event = await calendarDbService.getEventById(eventId);
    if (!event) return false;
    return event.userId === userId;
  } catch (error) {
    console.error("\u68C0\u67E5\u4E8B\u4EF6\u6743\u9650\u65F6\u51FA\u9519\uFF1A", error);
    return false;
  }
}
var CALENDAR_SERVER_MODULE_VERSION = "1.0.0";
var CALENDAR_SERVER_MODULE_NAME = "@qhr123/sa2kit/calendar/server";

export { CALENDAR_SERVER_MODULE_NAME, CALENDAR_SERVER_MODULE_VERSION, DEFAULT_CALENDAR_CONFIG, calculateReminderTime, checkEventPermission, createErrorResponse, createSuccessResponse, generateRecurrenceInstances, validateEventData, validateRecurrenceData };
//# sourceMappingURL=server.mjs.map
//# sourceMappingURL=server.mjs.map