/**
 * DevExtreme (cjs/__internal/scheduler/appointments/m_cell_position_calculator.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.CellPositionCalculator = void 0;
var _type = require("../../../core/utils/type");
var _date = require("../../core/utils/date");

function _inheritsLoose(subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    _setPrototypeOf(subClass, superClass)
}

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}

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)
}

function _defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) {
            descriptor.writable = true
        }
        Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor)
    }
}

function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps) {
        _defineProperties(Constructor.prototype, protoProps)
    }
    if (staticProps) {
        _defineProperties(Constructor, staticProps)
    }
    Object.defineProperty(Constructor, "prototype", {
        writable: false
    });
    return Constructor
}

function _toPropertyKey(arg) {
    var key = _toPrimitive(arg, "string");
    return "symbol" === typeof key ? key : String(key)
}

function _toPrimitive(input, hint) {
    if ("object" !== typeof input || null === input) {
        return input
    }
    var prim = input[Symbol.toPrimitive];
    if (void 0 !== prim) {
        var res = prim.call(input, hint || "default");
        if ("object" !== typeof res) {
            return res
        }
        throw new TypeError("@@toPrimitive must return a primitive value.")
    }
    return ("string" === hint ? String : Number)(input)
}
let BaseStrategy = function() {
    function BaseStrategy(options) {
        this.isVirtualScrolling = false;
        this.options = options
    }
    var _proto = BaseStrategy.prototype;
    _proto.calculateCellPositions = function(groupIndices, isAllDayRowAppointment, isRecurrentAppointment) {
        const result = [];
        this.appointments.forEach((dateSetting, index) => {
            const coordinates = this.getCoordinateInfos({
                appointment: dateSetting,
                groupIndices: groupIndices,
                isAllDayRowAppointment: isAllDayRowAppointment,
                isRecurrentAppointment: isRecurrentAppointment
            });
            coordinates.forEach(item => {
                !!item && result.push(this._prepareObject(item, index))
            })
        });
        return result
    };
    _proto.getCoordinateInfos = function(options) {
        const {
            appointment: appointment,
            isAllDayRowAppointment: isAllDayRowAppointment,
            groupIndices: groupIndices,
            recurrent: recurrent
        } = options;
        const {
            startDate: startDate
        } = appointment;
        const groupIndex = !recurrent ? appointment.source.groupIndex : void 0;
        return this.getCoordinatesByDateInGroup(startDate, groupIndices, isAllDayRowAppointment, groupIndex)
    };
    _proto._prepareObject = function(position, dateSettingIndex) {
        position.dateSettingIndex = dateSettingIndex;
        return {
            coordinates: position,
            dateSettingIndex: dateSettingIndex
        }
    };
    _proto.getCoordinatesByDate = function(date, groupIndex, inAllDayRow) {
        const validGroupIndex = groupIndex || 0;
        const cellInfo = {
            groupIndex: validGroupIndex,
            startDate: date,
            isAllDay: inAllDayRow
        };
        const positionByMap = this.viewDataProvider.findCellPositionInMap(cellInfo, true);
        if (!positionByMap) {
            return
        }
        const position = this.getCellPosition(positionByMap, inAllDayRow && !this.isVerticalGrouping);
        const timeShift = inAllDayRow ? 0 : this.getTimeShiftRatio(positionByMap, date);
        const shift = this.getPositionShift(timeShift, inAllDayRow);
        const horizontalHMax = this.positionHelper.getHorizontalMax(validGroupIndex, date);
        const verticalMax = this.positionHelper.getVerticalMax({
            groupIndex: validGroupIndex,
            isVirtualScrolling: this.isVirtualScrolling,
            showAllDayPanel: this.showAllDayPanel,
            supportAllDayRow: this.supportAllDayRow,
            isGroupedAllDayPanel: this.isGroupedAllDayPanel,
            isVerticalGrouping: this.isVerticalGrouping
        });
        return {
            positionByMap: positionByMap,
            cellPosition: position.left + shift.cellPosition,
            top: position.top + shift.top,
            left: position.left + shift.left,
            rowIndex: position.rowIndex,
            columnIndex: position.columnIndex,
            hMax: horizontalHMax,
            vMax: verticalMax,
            groupIndex: validGroupIndex
        }
    };
    _proto.getCoordinatesByDateInGroup = function(startDate, groupIndices, inAllDayRow, groupIndex) {
        const result = [];
        if (this.viewDataProvider.isSkippedDate(startDate)) {
            return result
        }
        let validGroupIndices = [groupIndex];
        if (!(0, _type.isDefined)(groupIndex)) {
            validGroupIndices = this.groupCount ? groupIndices : [0]
        }
        validGroupIndices.forEach(groupIndex => {
            const coordinates = this.getCoordinatesByDate(startDate, groupIndex, inAllDayRow);
            if (coordinates) {
                result.push(coordinates)
            }
        });
        return result
    };
    _proto.getCellPosition = function(cellCoordinates, isAllDayPanel) {
        const {
            dateTableCellsMeta: dateTableCellsMeta,
            allDayPanelCellsMeta: allDayPanelCellsMeta
        } = this.DOMMetaData;
        const {
            columnIndex: columnIndex,
            rowIndex: rowIndex
        } = cellCoordinates;
        const position = isAllDayPanel ? allDayPanelCellsMeta[columnIndex] : dateTableCellsMeta[rowIndex][columnIndex];
        const validPosition = _extends({}, position);
        if (this.rtlEnabled) {
            validPosition.left += position.width
        }
        if (validPosition) {
            validPosition.rowIndex = cellCoordinates.rowIndex;
            validPosition.columnIndex = cellCoordinates.columnIndex
        }
        return validPosition
    };
    _proto.getTimeShiftRatio = function(positionByMap, appointmentDate) {
        const {
            cellDuration: cellDuration,
            viewOffset: viewOffset
        } = this.options;
        const {
            rowIndex: rowIndex,
            columnIndex: columnIndex
        } = positionByMap;
        const matchedCell = this.viewDataProvider.viewDataMap.dateTableMap[rowIndex][columnIndex];
        const matchedCellStartDate = _date.dateUtilsTs.addOffsets(matchedCell.cellData.startDate, [-viewOffset]);
        const result = (appointmentDate.getTime() - matchedCellStartDate.getTime()) / cellDuration;
        return result % 1
    };
    _createClass(BaseStrategy, [{
        key: "DOMMetaData",
        get: function() {
            return this.options.DOMMetaData
        }
    }, {
        key: "appointments",
        get: function() {
            return this.options.dateSettings
        }
    }, {
        key: "viewDataProvider",
        get: function() {
            return this.options.viewDataProvider
        }
    }, {
        key: "positionHelper",
        get: function() {
            return this.options.positionHelper
        }
    }, {
        key: "startViewDate",
        get: function() {
            return this.options.startViewDate
        }
    }, {
        key: "viewStartDayHour",
        get: function() {
            return this.options.viewStartDayHour
        }
    }, {
        key: "viewEndDayHour",
        get: function() {
            return this.options.viewEndDayHour
        }
    }, {
        key: "cellDuration",
        get: function() {
            return this.options.cellDuration
        }
    }, {
        key: "getPositionShift",
        get: function() {
            return this.options.getPositionShiftCallback
        }
    }, {
        key: "groupCount",
        get: function() {
            return this.options.groupCount
        }
    }, {
        key: "rtlEnabled",
        get: function() {
            return this.options.rtlEnabled
        }
    }, {
        key: "isVerticalGrouping",
        get: function() {
            return this.options.isVerticalGroupOrientation
        }
    }, {
        key: "showAllDayPanel",
        get: function() {
            return this.options.showAllDayPanel
        }
    }, {
        key: "supportAllDayRow",
        get: function() {
            return this.options.supportAllDayRow
        }
    }, {
        key: "isGroupedAllDayPanel",
        get: function() {
            return this.options.isGroupedAllDayPanel
        }
    }]);
    return BaseStrategy
}();
let VirtualStrategy = function(_BaseStrategy) {
    _inheritsLoose(VirtualStrategy, _BaseStrategy);

    function VirtualStrategy() {
        var _this;
        _this = _BaseStrategy.apply(this, arguments) || this;
        _this.isVirtualScrolling = true;
        return _this
    }
    var _proto2 = VirtualStrategy.prototype;
    _proto2.calculateCellPositions = function(groupIndices, isAllDayRowAppointment, isRecurrentAppointment) {
        const appointments = isAllDayRowAppointment ? this.appointments : this.appointments.filter(_ref => {
            let {
                source: source,
                startDate: startDate,
                endDate: endDate
            } = _ref;
            return this.viewDataProvider.isGroupIntersectDateInterval(source.groupIndex, startDate, endDate)
        });
        if (isRecurrentAppointment) {
            return this.createRecurrentAppointmentInfos(appointments, isAllDayRowAppointment)
        }
        return _BaseStrategy.prototype.calculateCellPositions.call(this, groupIndices, isAllDayRowAppointment, isRecurrentAppointment)
    };
    _proto2.createRecurrentAppointmentInfos = function(dateSettings, isAllDayRowAppointment) {
        const result = [];
        dateSettings.forEach((_ref2, index) => {
            let {
                source: source,
                startDate: startDate
            } = _ref2;
            const coordinate = this.getCoordinatesByDate(startDate, source.groupIndex, isAllDayRowAppointment);
            if (coordinate) {
                result.push(this._prepareObject(coordinate, index))
            }
        });
        return result
    };
    return VirtualStrategy
}(BaseStrategy);
let CellPositionCalculator = function() {
    function CellPositionCalculator(options) {
        this.options = options
    }
    var _proto3 = CellPositionCalculator.prototype;
    _proto3.calculateCellPositions = function(groupIndices, isAllDayRowAppointment, isRecurrentAppointment) {
        const strategy = this.options.isVirtualScrolling ? new VirtualStrategy(this.options) : new BaseStrategy(this.options);
        return strategy.calculateCellPositions(groupIndices, isAllDayRowAppointment, isRecurrentAppointment)
    };
    return CellPositionCalculator
}();
exports.CellPositionCalculator = CellPositionCalculator;
