/**
 * DevExtreme (esm/__internal/scheduler/m_subscribes.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/
 */
import $ from "../../core/renderer";
import dateUtils from "../../core/utils/date";
import {
    extend
} from "../../core/utils/extend";
import {
    each
} from "../../core/utils/iterator";
import {
    isPlainObject
} from "../../core/utils/type";
import {
    formatDates,
    getFormatType
} from "./appointments/m_text_utils";
import {
    createAppointmentAdapter
} from "./m_appointment_adapter";
import {
    AGENDA_LAST_IN_DATE_APPOINTMENT_CLASS
} from "./m_classes";
import {
    utils
} from "./m_utils";
var toMs = dateUtils.dateToMilliseconds;
var subscribes = {
    isCurrentViewAgenda() {
        return "agenda" === this.currentViewType
    },
    currentViewUpdated(currentView) {
        this.option("currentView", currentView)
    },
    currentDateUpdated(date) {
        this.option("currentDate", date)
    },
    getOption(name) {
        return this.option(name)
    },
    getWorkspaceOption(name) {
        return this.getWorkSpace().option(name)
    },
    isVirtualScrolling() {
        return this.isVirtualScrolling()
    },
    setCellDataCacheAlias(appointment, geometry) {
        this._workSpace.setCellDataCacheAlias(appointment, geometry)
    },
    isGroupedByDate() {
        return this.getWorkSpace().isGroupedByDate()
    },
    showAppointmentTooltip(options) {
        var targetedAppointment = this.getTargetedAppointment(options.data, options.target);
        this.showAppointmentTooltip(options.data, options.target, targetedAppointment)
    },
    hideAppointmentTooltip() {
        this.hideAppointmentTooltip()
    },
    showEditAppointmentPopup(options) {
        var targetedData = this.getTargetedAppointment(options.data, options.target);
        this.showAppointmentPopup(options.data, false, targetedData)
    },
    updateAppointmentAfterResize(options) {
        var info = utils.dataAccessors.getAppointmentInfo(options.$appointment);
        var {
            exceptionDate: exceptionDate
        } = info.sourceAppointment;
        this._checkRecurringAppointment(options.target, options.data, exceptionDate, () => {
            this._updateAppointment(options.target, options.data, (function() {
                this._appointments.moveAppointmentBack()
            }))
        })
    },
    getUpdatedData(rawAppointment) {
        return this._getUpdatedData(rawAppointment)
    },
    updateAppointmentAfterDrag(_ref) {
        var {
            event: event,
            element: element,
            rawAppointment: rawAppointment,
            newCellIndex: newCellIndex,
            oldCellIndex: oldCellIndex
        } = _ref;
        var info = utils.dataAccessors.getAppointmentInfo(element);
        var appointment = createAppointmentAdapter(rawAppointment, this._dataAccessors, this.timeZoneCalculator);
        var targetedAppointment = createAppointmentAdapter(extend({}, rawAppointment, this._getUpdatedData(rawAppointment)), this._dataAccessors, this.timeZoneCalculator);
        var targetedRawAppointment = targetedAppointment.source();
        var becomeAllDay = targetedAppointment.allDay;
        var wasAllDay = appointment.allDay;
        var movedBetweenAllDayAndSimple = this._workSpace.supportAllDayRow() && (wasAllDay && !becomeAllDay || !wasAllDay && becomeAllDay);
        var isDragAndDropBetweenComponents = event.fromComponent !== event.toComponent;
        if (-1 === newCellIndex) {
            if (!isDragAndDropBetweenComponents) {
                this._appointments.moveAppointmentBack(event)
            }
            return
        }
        if (newCellIndex !== oldCellIndex || isDragAndDropBetweenComponents || movedBetweenAllDayAndSimple) {
            this._checkRecurringAppointment(rawAppointment, targetedRawAppointment, info.sourceAppointment.exceptionDate, () => {
                this._updateAppointment(rawAppointment, targetedRawAppointment, (function() {
                    this._appointments.moveAppointmentBack(event)
                }), event)
            }, void 0, void 0, event)
        } else {
            this._appointments.moveAppointmentBack(event)
        }
    },
    onDeleteButtonPress(options) {
        var targetedData = this.getTargetedAppointment(options.data, $(options.target));
        this.checkAndDeleteAppointment(options.data, targetedData);
        this.hideAppointmentTooltip()
    },
    getTextAndFormatDate(appointmentRaw, targetedAppointmentRaw, format) {
        var appointmentAdapter = createAppointmentAdapter(appointmentRaw, this._dataAccessors, this.timeZoneCalculator);
        var targetedAdapter = createAppointmentAdapter(targetedAppointmentRaw || appointmentRaw, this._dataAccessors, this.timeZoneCalculator);
        var startDate = this.timeZoneCalculator.createDate(targetedAdapter.startDate, {
            path: "toGrid"
        });
        var endDate = this.timeZoneCalculator.createDate(targetedAdapter.endDate, {
            path: "toGrid"
        });
        var formatType = format || getFormatType(startDate, endDate, targetedAdapter.allDay, "month" !== this.currentViewType);
        return {
            text: targetedAdapter.text || appointmentAdapter.text,
            formatDate: formatDates(startDate, endDate, formatType)
        }
    },
    _createAppointmentTitle(data) {
        if (isPlainObject(data)) {
            return data.text
        }
        return String(data)
    },
    getResizableAppointmentArea(options) {
        var {
            allDay: allDay
        } = options;
        var groups = this._getCurrentViewOption("groups");
        if (groups && groups.length) {
            if (allDay || this.getLayoutManager().getRenderingStrategyInstance()._needHorizontalGroupBounds()) {
                var horizontalGroupBounds = this._workSpace.getGroupBounds(options.coordinates);
                return {
                    left: horizontalGroupBounds.left,
                    right: horizontalGroupBounds.right,
                    top: 0,
                    bottom: 0
                }
            }
            if (this.getLayoutManager().getRenderingStrategyInstance()._needVerticalGroupBounds(allDay) && this._workSpace._isVerticalGroupedWorkSpace()) {
                var verticalGroupBounds = this._workSpace.getGroupBounds(options.coordinates);
                return {
                    left: 0,
                    right: 0,
                    top: verticalGroupBounds.top,
                    bottom: verticalGroupBounds.bottom
                }
            }
        }
        return
    },
    needRecalculateResizableArea() {
        return this.getWorkSpace().needRecalculateResizableArea()
    },
    getAppointmentGeometry(settings) {
        return this.getLayoutManager().getRenderingStrategyInstance().getAppointmentGeometry(settings)
    },
    isAllDay(appointmentData) {
        return this.getLayoutManager().getRenderingStrategyInstance().isAllDay(appointmentData)
    },
    getDeltaTime(e, initialSize, itemData) {
        return this.getLayoutManager().getRenderingStrategyInstance().getDeltaTime(e, initialSize, itemData)
    },
    getDropDownAppointmentWidth(isAllDay) {
        return this.getLayoutManager().getRenderingStrategyInstance().getDropDownAppointmentWidth(this._getViewCountConfig().intervalCount, isAllDay)
    },
    getDropDownAppointmentHeight() {
        return this.getLayoutManager().getRenderingStrategyInstance().getDropDownAppointmentHeight()
    },
    getCellWidth() {
        return this.getWorkSpace().getCellWidth()
    },
    getCellHeight() {
        return this.getWorkSpace().getCellHeight()
    },
    getMaxAppointmentCountPerCellByType(isAllDay) {
        return this.getRenderingStrategyInstance()._getMaxAppointmentCountPerCellByType(isAllDay)
    },
    needCorrectAppointmentDates() {
        return this.getRenderingStrategyInstance().needCorrectAppointmentDates()
    },
    getRenderingStrategyDirection() {
        return this.getRenderingStrategyInstance().getDirection()
    },
    updateAppointmentEndDate(options) {
        var {
            endDate: endDate
        } = options;
        var endDayHour = this._getCurrentViewOption("endDayHour");
        var startDayHour = this._getCurrentViewOption("startDayHour");
        var updatedEndDate = endDate;
        if (endDate.getHours() >= endDayHour) {
            updatedEndDate.setHours(endDayHour, 0, 0, 0)
        } else if (!options.isSameDate && startDayHour > 0 && 60 * endDate.getHours() + endDate.getMinutes() < 60 * startDayHour) {
            updatedEndDate = new Date(updatedEndDate.getTime() - toMs("day"));
            updatedEndDate.setHours(endDayHour, 0, 0, 0)
        }
        return updatedEndDate
    },
    renderCompactAppointments(options) {
        this._compactAppointmentsHelper.render(options)
    },
    clearCompactAppointments() {
        this._compactAppointmentsHelper.clear()
    },
    supportCompactDropDownAppointments() {
        return this.getLayoutManager().getRenderingStrategyInstance().supportCompactDropDownAppointments()
    },
    getGroupCount() {
        return this._workSpace._getGroupCount()
    },
    mapAppointmentFields(config) {
        var {
            itemData: itemData,
            itemElement: itemElement,
            targetedAppointment: targetedAppointment
        } = config;
        var targetedData = targetedAppointment || this.getTargetedAppointment(itemData, itemElement);
        return {
            appointmentData: config.itemData,
            appointmentElement: config.itemElement,
            targetedAppointmentData: targetedData
        }
    },
    dayHasAppointment(day, appointment, trimTime) {
        return this.dayHasAppointment(day, appointment, trimTime)
    },
    getLayoutManager() {
        return this._layoutManager
    },
    getAgendaVerticalStepHeight() {
        return this.getWorkSpace().getAgendaVerticalStepHeight()
    },
    getAgendaDuration() {
        return this._getCurrentViewOption("agendaDuration")
    },
    getStartViewDate() {
        return this.getStartViewDate()
    },
    getEndViewDate() {
        return this.getEndViewDate()
    },
    forceMaxAppointmentPerCell() {
        return this.forceMaxAppointmentPerCell()
    },
    onAgendaReady(rows) {
        var $appts = this.getAppointmentsInstance()._itemElements();
        var total = 0;
        var applyClass = function(_, count) {
            var index = count + total - 1;
            $appts.eq(index).addClass(AGENDA_LAST_IN_DATE_APPOINTMENT_CLASS);
            total += count
        };
        for (var i = 0; i < rows.length; i++) {
            each(rows[i], applyClass)
        }
    },
    getTimezone() {
        return this._getTimezoneOffsetByOption()
    },
    getTargetedAppointmentData(appointment, element) {
        return this.getTargetedAppointment(appointment, element)
    },
    getEndDayHour() {
        return this._workSpace.option("endDayHour") || this.option("endDayHour")
    },
    getStartDayHour() {
        return this._workSpace.option("startDayHour") || this.option("startDayHour")
    },
    getViewOffsetMs() {
        return this.getViewOffsetMs()
    },
    isAdaptive() {
        return this.option("adaptivityEnabled")
    },
    removeDroppableCellClass() {
        this._workSpace.removeDroppableCellClass()
    }
};
export default subscribes;
