import * as React from "react";
const PARTICIPANT_COLORS = [
  "bg-chart-emerald",
  "bg-chart-blue",
  "bg-chart-orange",
  "bg-chart-violet",
  "bg-chart-pink",
  "bg-chart-teal"
];
const DEFAULT_REMINDER_MINUTES = {
  meeting: 15,
  call: 5,
  task: 1440,
  email: 15,
  note: 15
};
function padDatePart(value) {
  return String(value).padStart(2, "0");
}
function formatLocalDateInput(date) {
  return `${date.getFullYear()}-${padDatePart(date.getMonth() + 1)}-${padDatePart(date.getDate())}`;
}
function formatLocalTimeInput(date) {
  return `${padDatePart(date.getHours())}:${padDatePart(date.getMinutes())}`;
}
function useScheduleFormState({ open, editData }) {
  const [activityType, setActivityType] = React.useState("meeting");
  const [title, setTitle] = React.useState("");
  const [date, setDate] = React.useState(() => formatLocalDateInput(/* @__PURE__ */ new Date()));
  const [startTime, setStartTime] = React.useState("10:00");
  const [duration, setDuration] = React.useState(30);
  const [allDay, setAllDay] = React.useState(false);
  const [description, setDescription] = React.useState("");
  const [markdownEnabled, setMarkdownEnabled] = React.useState(true);
  const [location, setLocation] = React.useState("");
  const [reminderMinutes, setReminderMinutes] = React.useState(15);
  const [visibility, setVisibility] = React.useState("team");
  const [participants, setParticipants] = React.useState([]);
  const [linkedEntities, setLinkedEntities] = React.useState([]);
  const [recurrenceEnabled, setRecurrenceEnabled] = React.useState(false);
  const [recurrenceDays, setRecurrenceDays] = React.useState([true, false, true, false, false, false, false]);
  const [recurrenceEndType, setRecurrenceEndType] = React.useState("never");
  const [recurrenceCount, setRecurrenceCount] = React.useState(8);
  const [recurrenceEndDate, setRecurrenceEndDate] = React.useState("");
  const [conflict, setConflict] = React.useState(null);
  const [saving, setSaving] = React.useState(false);
  const [guestPermissions, setGuestPermissions] = React.useState({ canInviteOthers: true, canModify: false, canSeeList: true });
  React.useEffect(() => {
    if (open) {
      if (editData) {
        const resolvedType = editData.interactionType ?? "meeting";
        setActivityType(resolvedType);
        setTitle(editData.title ?? "");
        const sourceTimestamp = editData.occurredAt ?? editData.scheduledAt ?? null;
        const seedDate = sourceTimestamp ? new Date(sourceTimestamp) : /* @__PURE__ */ new Date();
        const seedDateValid = !Number.isNaN(seedDate.getTime());
        const fallbackNow = /* @__PURE__ */ new Date();
        const dateForForm = seedDateValid ? seedDate : fallbackNow;
        setDate(formatLocalDateInput(dateForForm));
        setStartTime(formatLocalTimeInput(dateForForm));
        setDuration(editData.durationMinutes ?? 30);
        setAllDay(editData.allDay ?? false);
        setDescription(editData.body ?? "");
        setLocation(editData.location ?? "");
        setReminderMinutes(editData.reminderMinutes ?? DEFAULT_REMINDER_MINUTES[resolvedType]);
        setVisibility(editData.visibility ?? "team");
        setParticipants(
          Array.isArray(editData.participants) ? editData.participants.map((p, i) => ({
            userId: p.userId,
            name: p.name ?? p.userId,
            email: p.email,
            color: PARTICIPANT_COLORS[i % PARTICIPANT_COLORS.length],
            status: p.status ?? "pending"
          })) : []
        );
        setLinkedEntities(
          Array.isArray(editData.linkedEntities) ? editData.linkedEntities.map((e) => ({ id: e.id, type: e.type, label: e.label })) : []
        );
        if (editData.recurrenceRule) {
          setRecurrenceEnabled(true);
          const rule = editData.recurrenceRule;
          const byDayMatch = rule.match(/BYDAY=([A-Z,]+)/);
          if (byDayMatch) {
            const dayNames = ["MO", "TU", "WE", "TH", "FR", "SA", "SU"];
            const activeDays = byDayMatch[1].split(",");
            setRecurrenceDays(dayNames.map((d) => activeDays.includes(d)));
          }
          const countMatch = rule.match(/COUNT=(\d+)/);
          const untilMatch = rule.match(/UNTIL=(\d{8})/);
          if (countMatch) {
            setRecurrenceEndType("count");
            setRecurrenceCount(Number(countMatch[1]));
          } else if (untilMatch) {
            setRecurrenceEndType("date");
            const raw = untilMatch[1];
            setRecurrenceEndDate(`${raw.slice(0, 4)}-${raw.slice(4, 6)}-${raw.slice(6, 8)}`);
          } else {
            setRecurrenceEndType("never");
          }
        } else {
          setRecurrenceEnabled(false);
        }
        if (editData.guestPermissions) {
          setGuestPermissions({
            canInviteOthers: editData.guestPermissions.canInviteOthers ?? true,
            canModify: editData.guestPermissions.canModify ?? false,
            canSeeList: editData.guestPermissions.canSeeList ?? true
          });
        }
      } else {
        setActivityType("meeting");
        setTitle("");
        setDate(formatLocalDateInput(/* @__PURE__ */ new Date()));
        setStartTime("10:00");
        setDuration(30);
        setAllDay(false);
        setDescription("");
        setLocation("");
        setReminderMinutes(DEFAULT_REMINDER_MINUTES.meeting);
        setVisibility("team");
        setParticipants([]);
        setLinkedEntities([]);
        setRecurrenceEnabled(false);
      }
      setConflict(null);
    }
    return () => {
      document.body.style.removeProperty("overflow");
      document.body.style.removeProperty("pointer-events");
    };
  }, [open, editData]);
  const lastReminderTypeRef = React.useRef("meeting");
  React.useEffect(() => {
    if (!open || editData) {
      lastReminderTypeRef.current = activityType;
      return;
    }
    if (lastReminderTypeRef.current === activityType) return;
    lastReminderTypeRef.current = activityType;
    setReminderMinutes(DEFAULT_REMINDER_MINUTES[activityType]);
  }, [activityType, editData, open]);
  const removeParticipant = React.useCallback((userId) => {
    setParticipants((prev) => prev.filter((p) => p.userId !== userId));
  }, []);
  const toggleRecurrenceDay = React.useCallback((index) => {
    setRecurrenceDays((prev) => {
      const next = [...prev];
      next[index] = !next[index];
      return next;
    });
  }, []);
  return {
    activityType,
    setActivityType,
    title,
    setTitle,
    date,
    setDate,
    startTime,
    setStartTime,
    duration,
    setDuration,
    allDay,
    setAllDay,
    description,
    setDescription,
    markdownEnabled,
    setMarkdownEnabled,
    location,
    setLocation,
    reminderMinutes,
    setReminderMinutes,
    visibility,
    setVisibility,
    participants,
    setParticipants,
    linkedEntities,
    setLinkedEntities,
    recurrenceEnabled,
    setRecurrenceEnabled,
    recurrenceDays,
    setRecurrenceDays,
    recurrenceEndType,
    setRecurrenceEndType,
    recurrenceCount,
    setRecurrenceCount,
    recurrenceEndDate,
    setRecurrenceEndDate,
    conflict,
    setConflict,
    saving,
    setSaving,
    guestPermissions,
    setGuestPermissions,
    removeParticipant,
    toggleRecurrenceDay
  };
}
export {
  PARTICIPANT_COLORS,
  useScheduleFormState
};
//# sourceMappingURL=useScheduleFormState.js.map
