/**
 * DevExtreme (renovation/ui/scheduler/workspaces/base/utils.js)
 * Version: 23.2.4 (build 24056-0112)
 * Build date: Sun Feb 25 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.isCellAllDay = exports.getTotalRowCount = exports.getTotalCellCount = exports.getRowCountWithAllDayRow = exports.getHiddenInterval = exports.getDateTableWidth = exports.getDateForHeaderText = exports.getCellIndices = exports.createVirtualScrollingOptions = exports.createCellElementMetaData = exports.compareCellsByDateAndIndex = exports.DATE_TABLE_MIN_CELL_WIDTH = void 0;
var _date = _interopRequireDefault(require("../../../../../core/utils/date"));
var _m_utils = require("../../../../../__internal/scheduler/resources/m_utils");
var _utils = require("../utils");
var _const = require("../const");

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}
const DAY_MS = _date.default.dateToMilliseconds("day");
const HOUR_MS = _date.default.dateToMilliseconds("hour");
const DATE_TABLE_MIN_CELL_WIDTH = 75;
exports.DATE_TABLE_MIN_CELL_WIDTH = 75;
const getTotalRowCount = (rowCount, groupOrientation, groups, isAllDayPanelVisible) => {
    const isVerticalGrouping = (0, _utils.isVerticalGroupingApplied)(groups, groupOrientation);
    const groupCount = (0, _m_utils.getGroupCount)(groups);
    const totalRowCount = isVerticalGrouping ? rowCount * groupCount : rowCount;
    return isAllDayPanelVisible ? totalRowCount + groupCount : totalRowCount
};
exports.getTotalRowCount = getTotalRowCount;
const getTotalCellCount = (cellCount, groupOrientation, groups) => {
    const isHorizontalGrouping = (0, _utils.isHorizontalGroupingApplied)(groups, groupOrientation);
    const groupCount = (0, _m_utils.getGroupCount)(groups);
    return isHorizontalGrouping ? cellCount * groupCount : cellCount
};
exports.getTotalCellCount = getTotalCellCount;
const getRowCountWithAllDayRow = (rowCount, isAllDayPanelVisible) => isAllDayPanelVisible ? rowCount + 1 : rowCount;
exports.getRowCountWithAllDayRow = getRowCountWithAllDayRow;
const getHiddenInterval = (hoursInterval, cellCountInDay) => {
    const visibleInterval = hoursInterval * cellCountInDay * HOUR_MS;
    return DAY_MS - visibleInterval
};
exports.getHiddenInterval = getHiddenInterval;
const createCellElementMetaData = (tableRect, cellRect) => {
    const {
        bottom: bottom,
        height: height,
        left: left,
        right: right,
        top: top,
        width: width,
        x: x,
        y: y
    } = cellRect;
    return {
        right: right,
        bottom: bottom,
        left: left - tableRect.left,
        top: top - tableRect.top,
        width: width,
        height: height,
        x: x,
        y: y
    }
};
exports.createCellElementMetaData = createCellElementMetaData;
const getDateForHeaderText = (_, date) => date;
exports.getDateForHeaderText = getDateForHeaderText;
const getDateTableWidth = (scrollableWidth, dateTable, viewDataProvider, workSpaceConfig) => {
    const dateTableCell = dateTable.querySelector("td:not(.dx-scheduler-virtual-cell)");
    let cellWidth = dateTableCell.getBoundingClientRect().width;
    if (cellWidth < 75) {
        cellWidth = 75
    }
    const cellCount = viewDataProvider.getCellCount(workSpaceConfig);
    const totalCellCount = getTotalCellCount(cellCount, workSpaceConfig.groupOrientation, workSpaceConfig.groups);
    const minTablesWidth = totalCellCount * cellWidth;
    return scrollableWidth < minTablesWidth ? minTablesWidth : scrollableWidth
};
exports.getDateTableWidth = getDateTableWidth;
const createVirtualScrollingOptions = options => ({
    getCellHeight: () => options.cellHeight,
    getCellWidth: () => options.cellWidth,
    getCellMinWidth: () => 75,
    isRTL: () => options.rtlEnabled,
    getSchedulerHeight: () => options.schedulerHeight,
    getSchedulerWidth: () => options.schedulerWidth,
    getViewHeight: () => options.viewHeight,
    getViewWidth: () => options.viewWidth,
    getScrolling: () => options.scrolling,
    getScrollableOuterWidth: () => options.scrollableWidth,
    getGroupCount: () => (0, _m_utils.getGroupCount)(options.groups),
    isVerticalGrouping: () => options.isVerticalGrouping,
    getTotalRowCount: () => options.completeRowCount,
    getTotalCellCount: () => options.completeColumnCount,
    getWindowHeight: () => options.windowHeight,
    getWindowWidth: () => options.windowWidth
});
exports.createVirtualScrollingOptions = createVirtualScrollingOptions;
const getCellIndices = cell => {
    const row = cell.closest(".".concat(_const.DATE_TABLE_ROW_CLASS, ", .").concat(_const.ALL_DAY_ROW_CLASS));
    const rowParent = row.parentNode;
    const cellParent = cell.parentNode;
    const columnIndex = [...Array.from(cellParent.children)].filter(child => child.className.includes(_const.DATE_TABLE_CELL_CLASS) || child.className.includes(_const.ALL_DAY_PANEL_CELL_CLASS)).indexOf(cell);
    const rowIndex = [...Array.from(rowParent.children)].filter(child => child.className.includes(_const.DATE_TABLE_ROW_CLASS)).indexOf(row);
    return {
        columnIndex: columnIndex,
        rowIndex: rowIndex
    }
};
exports.getCellIndices = getCellIndices;
const compareCellsByDateAndIndex = daysAndIndexes => {
    const {
        date: date,
        firstDate: firstDate,
        firstIndex: firstIndex,
        index: index,
        lastDate: lastDate,
        lastIndex: lastIndex
    } = daysAndIndexes;
    if (firstDate === lastDate) {
        let validFirstIndex = firstIndex;
        let validLastIndex = lastIndex;
        if (validFirstIndex > validLastIndex) {
            [validFirstIndex, validLastIndex] = [validLastIndex, validFirstIndex]
        }
        return firstDate === date && index >= validFirstIndex && index <= validLastIndex
    }
    return date === firstDate && index >= firstIndex || date === lastDate && index <= lastIndex || firstDate < date && date < lastDate
};
exports.compareCellsByDateAndIndex = compareCellsByDateAndIndex;
const isCellAllDay = cell => cell.className.includes(_const.ALL_DAY_PANEL_CELL_CLASS);
exports.isCellAllDay = isCellAllDay;
