{"version":3,"file":"get-day-positioned-events.cjs","names":["isAllDayEvent","isEventsOverlap","sortEvents","getDayPosition"],"sources":["../../../../src/components/DayView/get-day-view-events/get-day-positioned-events.ts"],"sourcesContent":["import { AnyDateValue, DayPositionedEventData, ScheduleEventData } from '../../../types';\nimport { getDayPosition, isAllDayEvent, isEventsOverlap, sortEvents } from '../../../utils';\n\ninterface ColumnHasConflictOptions {\n  columns: ScheduleEventData[][];\n  columnIndex: number;\n  event: ScheduleEventData;\n  allDay: boolean;\n  date: AnyDateValue;\n}\n\nfunction columnHasConflict({\n  columns,\n  columnIndex,\n  event,\n  allDay,\n  date,\n}: ColumnHasConflictOptions): boolean {\n  if (!columns[columnIndex]) {\n    return false;\n  }\n\n  return columns[columnIndex].some((e) => {\n    const eAllDay = isAllDayEvent({ event: e, date });\n    const hasTimeConflict = isEventsOverlap(e, event);\n\n    // Don't count all-day events as overlaps\n    if (allDay || eAllDay) {\n      return false;\n    }\n\n    return hasTimeConflict;\n  });\n}\n\ninterface GetDayPositionedEventsInput {\n  /** Date at which events are positioned, used to check if events are all-day */\n  date: AnyDateValue;\n\n  /** List of all events that belong to the given date, extra events must be filtered out before passing to the function */\n  events: ScheduleEventData[];\n\n  /** Start time of the day view, used to calculate event positions */\n  startTime?: string;\n\n  /** End time of the day view, used to calculate event positions */\n  endTime?: string;\n\n  /** Number of minutes per time slot, used to align the canvas to whole slots */\n  intervalMinutes?: number;\n}\n\nexport function getDayPositionedEvents({\n  events,\n  startTime,\n  endTime,\n  intervalMinutes,\n  date,\n}: GetDayPositionedEventsInput) {\n  const columns: ScheduleEventData[][] = [];\n  const positioned: DayPositionedEventData[] = [];\n\n  const sorted = sortEvents(events);\n\n  for (const event of sorted) {\n    const allDay = isAllDayEvent({ event, date });\n\n    let column = 0;\n\n    while (columnHasConflict({ columns, columnIndex: column, event, allDay, date })) {\n      column++;\n    }\n\n    if (!columns[column]) {\n      columns[column] = [];\n    }\n\n    columns[column].push(event);\n\n    const verticalPosition = allDay\n      ? { top: 0, height: 100 }\n      : getDayPosition({ event, startTime, endTime, intervalMinutes });\n\n    positioned.push({\n      ...event,\n      position: {\n        ...verticalPosition,\n        allDay,\n        column,\n\n        // set later\n        width: 0,\n        offset: 0,\n        overlaps: 0,\n      },\n    });\n  }\n  for (const positionedEvent of positioned) {\n    const { allDay, column } = positionedEvent.position;\n\n    if (allDay) {\n      positionedEvent.position.overlaps = 1;\n      positionedEvent.position.width = 100;\n      positionedEvent.position.offset = 0;\n      continue;\n    }\n\n    // Find events that directly overlap with this event\n    const directlyOverlapping: DayPositionedEventData[] = [];\n\n    for (const otherEvent of positioned) {\n      if (otherEvent === positionedEvent || otherEvent.position.allDay) {\n        continue;\n      }\n\n      if (isEventsOverlap(otherEvent, positionedEvent)) {\n        directlyOverlapping.push(otherEvent);\n      }\n    }\n\n    // Find the max column by also checking overlaps of overlapping events\n    // This ensures we account for the full column group\n    let maxColumn = column;\n\n    for (const overlappingEvent of directlyOverlapping) {\n      maxColumn = Math.max(maxColumn, overlappingEvent.position.column);\n\n      // Check what overlaps with each overlapping event\n      for (const otherEvent of positioned) {\n        if (otherEvent === overlappingEvent || otherEvent.position.allDay) {\n          continue;\n        }\n\n        if (isEventsOverlap(otherEvent, overlappingEvent)) {\n          maxColumn = Math.max(maxColumn, otherEvent.position.column);\n        }\n      }\n    }\n\n    const totalColumns = maxColumn + 1;\n\n    // Find the first occupied column to the right of this event\n    // Events expand to fill empty columns but stop at the next occupied column\n    let nextOccupiedColumn = totalColumns;\n\n    for (const other of directlyOverlapping) {\n      if (other.position.column > column && other.position.column < nextOccupiedColumn) {\n        nextOccupiedColumn = other.position.column;\n      }\n    }\n\n    const columnsSpanned = nextOccupiedColumn - column;\n\n    positionedEvent.position.overlaps = totalColumns;\n    positionedEvent.position.width = (columnsSpanned / totalColumns) * 100;\n    positionedEvent.position.offset = (column * 100) / totalColumns;\n  }\n\n  return positioned;\n}\n"],"mappings":";;;;;;AAWA,SAAS,kBAAkB,EACzB,SACA,aAC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