/**
 * DevExtreme (cjs/__internal/scheduler/workspaces/view_model/m_time_panel_data_generator.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.TimePanelDataGenerator = void 0;
var _date = _interopRequireDefault(require("../../../../core/utils/date"));
var _date2 = require("../../../core/utils/date");
var _math = require("../../../core/utils/math");
var _index = require("../../../scheduler/r1/utils/index");

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}

function _extends() {
    _extends = Object.assign ? Object.assign.bind() : function(target) {
        for (var i = 1; i < arguments.length; i++) {
            var source = arguments[i];
            for (var key in source) {
                if (Object.prototype.hasOwnProperty.call(source, key)) {
                    target[key] = source[key]
                }
            }
        }
        return target
    };
    return _extends.apply(this, arguments)
}
var __rest = (void 0, function(s, e) {
    var t = {};
    for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) {
            t[p] = s[p]
        }
    }
    if (null != s && "function" === typeof Object.getOwnPropertySymbols) {
        var i = 0;
        for (p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) {
                t[p[i]] = s[p[i]]
            }
        }
    }
    return t
});
const toMs = _date.default.dateToMilliseconds;
let TimePanelDataGenerator = function() {
    function TimePanelDataGenerator(_viewDataGenerator) {
        this._viewDataGenerator = _viewDataGenerator
    }
    var _proto = TimePanelDataGenerator.prototype;
    _proto.getCompleteTimePanelMap = function(options, completeViewDataMap) {
        const {
            startViewDate: startViewDate,
            cellDuration: cellDuration,
            startDayHour: startDayHour,
            isVerticalGrouping: isVerticalGrouping,
            intervalCount: intervalCount,
            currentDate: currentDate,
            viewType: viewType,
            hoursInterval: hoursInterval,
            endDayHour: endDayHour,
            viewOffset: viewOffset,
            today: today,
            showCurrentTimeIndicator: showCurrentTimeIndicator
        } = options;
        const rowsCount = completeViewDataMap.length - 1;
        const realEndViewDate = completeViewDataMap[rowsCount][completeViewDataMap[rowsCount].length - 1].endDate;
        const rowCountInGroup = this._viewDataGenerator.getRowCount({
            intervalCount: intervalCount,
            currentDate: currentDate,
            viewType: viewType,
            hoursInterval: hoursInterval,
            startDayHour: startDayHour,
            endDayHour: endDayHour
        });
        const cellCountInGroupRow = this._viewDataGenerator.getCellCount({
            intervalCount: intervalCount,
            currentDate: currentDate,
            viewType: viewType,
            hoursInterval: hoursInterval,
            startDayHour: startDayHour,
            endDayHour: endDayHour
        });
        let allDayRowsCount = 0;
        let usualCellIndex = 0;
        return completeViewDataMap.map((row, index) => {
            const _a = row[0],
                {
                    allDay: allDay,
                    startDate: startDate,
                    endDate: endDate,
                    groups: groups,
                    groupIndex: groupIndex,
                    isFirstGroupCell: isFirstGroupCell,
                    isLastGroupCell: isLastGroupCell,
                    index: cellIndex
                } = _a,
                restCellProps = __rest(_a, ["allDay", "startDate", "endDate", "groups", "groupIndex", "isFirstGroupCell", "isLastGroupCell", "index"]);
            const highlighted = allDay ? false : this.isTimeCellShouldBeHighlighted(today, viewOffset, {
                startViewDate: startViewDate,
                realEndViewDate: realEndViewDate,
                showCurrentTimeIndicator: showCurrentTimeIndicator
            }, {
                date: startDate,
                index: usualCellIndex,
                duration: Math.round(cellDuration),
                isFirst: 0 === usualCellIndex,
                isLast: this.isLastCellInGroup(completeViewDataMap, index)
            });
            if (allDay) {
                allDayRowsCount += 1;
                usualCellIndex = 0
            } else {
                usualCellIndex += 1
            }
            const timeIndex = (index - allDayRowsCount) % rowCountInGroup;
            return _extends(_extends({}, restCellProps), {
                startDate: startDate,
                allDay: allDay,
                highlighted: highlighted,
                text: _index.weekUtils.getTimePanelCellText(timeIndex, startDate, startViewDate, cellDuration, startDayHour, viewOffset),
                groups: isVerticalGrouping ? groups : void 0,
                groupIndex: isVerticalGrouping ? groupIndex : void 0,
                isFirstGroupCell: isVerticalGrouping && isFirstGroupCell,
                isLastGroupCell: isVerticalGrouping && isLastGroupCell,
                index: Math.floor(cellIndex / cellCountInGroupRow)
            })
        })
    };
    _proto.generateTimePanelData = function(completeTimePanelMap, options) {
        const {
            startRowIndex: startRowIndex,
            rowCount: rowCount,
            topVirtualRowHeight: topVirtualRowHeight,
            bottomVirtualRowHeight: bottomVirtualRowHeight,
            isGroupedAllDayPanel: isGroupedAllDayPanel,
            isVerticalGrouping: isVerticalGrouping,
            isAllDayPanelVisible: isAllDayPanelVisible
        } = options;
        const indexDifference = isVerticalGrouping || !isAllDayPanelVisible ? 0 : 1;
        const correctedStartRowIndex = startRowIndex + indexDifference;
        const displayedRowCount = (0, _index.getDisplayedRowCount)(rowCount, completeTimePanelMap);
        const timePanelMap = completeTimePanelMap.slice(correctedStartRowIndex, correctedStartRowIndex + displayedRowCount);
        const timePanelData = {
            topVirtualRowHeight: topVirtualRowHeight,
            bottomVirtualRowHeight: bottomVirtualRowHeight,
            isGroupedAllDayPanel: isGroupedAllDayPanel
        };
        const {
            previousGroupedData: groupedData
        } = this._generateTimePanelDataFromMap(timePanelMap, isVerticalGrouping);
        timePanelData.groupedData = groupedData;
        return timePanelData
    };
    _proto._generateTimePanelDataFromMap = function(timePanelMap, isVerticalGrouping) {
        return timePanelMap.reduce((_ref, cellData) => {
            let {
                previousGroupIndex: previousGroupIndex,
                previousGroupedData: previousGroupedData
            } = _ref;
            const currentGroupIndex = cellData.groupIndex;
            if (currentGroupIndex !== previousGroupIndex) {
                previousGroupedData.push({
                    dateTable: [],
                    isGroupedAllDayPanel: (0, _index.getIsGroupedAllDayPanel)(!!cellData.allDay, isVerticalGrouping),
                    groupIndex: currentGroupIndex,
                    key: (0, _index.getKeyByGroup)(currentGroupIndex, isVerticalGrouping)
                })
            }
            if (cellData.allDay) {
                previousGroupedData[previousGroupedData.length - 1].allDayPanel = cellData
            } else {
                previousGroupedData[previousGroupedData.length - 1].dateTable.push(cellData)
            }
            return {
                previousGroupIndex: currentGroupIndex,
                previousGroupedData: previousGroupedData
            }
        }, {
            previousGroupIndex: -1,
            previousGroupedData: []
        })
    };
    _proto.isTimeCellShouldBeHighlighted = function(today, viewOffset, _ref2, cellData) {
        let {
            startViewDate: startViewDate,
            realEndViewDate: realEndViewDate,
            showCurrentTimeIndicator: showCurrentTimeIndicator
        } = _ref2;
        const realToday = _date2.dateUtilsTs.addOffsets(today, [viewOffset]);
        const realStartViewDate = _date2.dateUtilsTs.addOffsets(startViewDate, [viewOffset]);
        if (!showCurrentTimeIndicator || realToday < realStartViewDate || realToday >= realEndViewDate) {
            return false
        }
        const realTodayTimeMs = this.getLocalDateTimeInMs(realToday);
        const [startMs, endMs] = this.getHighlightedInterval(cellData);
        return startMs < endMs ? realTodayTimeMs >= startMs && realTodayTimeMs < endMs : realTodayTimeMs >= startMs && realTodayTimeMs < toMs("day") || realTodayTimeMs >= 0 && realTodayTimeMs < endMs
    };
    _proto.getHighlightedInterval = function(_ref3) {
        let {
            date: date,
            index: index,
            duration: duration,
            isFirst: isFirst,
            isLast: isLast
        } = _ref3;
        const cellTimeMs = this.getLocalDateTimeInMs(date);
        const isEvenCell = index % 2 === 0;
        switch (true) {
            case isFirst || isLast && !isEvenCell:
                return [cellTimeMs, (0, _math.shiftIntegerByModule)(cellTimeMs + duration, toMs("day"))];
            case isEvenCell:
                return [(0, _math.shiftIntegerByModule)(cellTimeMs - duration, toMs("day")), (0, _math.shiftIntegerByModule)(cellTimeMs + duration, toMs("day"))];
            default:
                return [cellTimeMs, (0, _math.shiftIntegerByModule)(cellTimeMs + 2 * duration, toMs("day"))]
        }
    };
    _proto.getLocalDateTimeInMs = function(date) {
        const dateUtcMs = date.getTime() - date.getTimezoneOffset() * toMs("minute");
        return (0, _math.shiftIntegerByModule)(dateUtcMs, toMs("day"))
    };
    _proto.isLastCellInGroup = function(completeViewDataMap, index) {
        if (index === completeViewDataMap.length - 1) {
            return true
        }
        const {
            groupIndex: currentGroupIndex
        } = completeViewDataMap[index][0];
        const {
            groupIndex: nextGroupIndex,
            allDay: nextAllDay
        } = completeViewDataMap[index + 1][0];
        return nextAllDay || nextGroupIndex !== currentGroupIndex
    };
    return TimePanelDataGenerator
}();
exports.TimePanelDataGenerator = TimePanelDataGenerator;
