function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
import React from "react";
import cls from "classnames";
import { classes } from "../../TimePickerGrid.tokens";
import { StyledTimeColumn, StyledTimeItem, StyledEmpty, CustomScrollbar, ScrollbarTrack, ScrollbarThumb, StyledRoot } from "./TimeColumn.styles";
export var renderTimeColumn = function renderTimeColumn(param) {
    var values = param.values, disabledValues = param.disabledValues, dropdownHeight = param.dropdownHeight, column = param.column, activeTime = param.activeTime, currentColumn = param.currentColumn, handleTimeItemClick = param.handleTimeItemClick, handleTimeItemKeyDown = param.handleTimeItemKeyDown, createScrollbarDragHandler = param.createScrollbarDragHandler, setScrollbar = param.setScrollbar, columnRef = param.columnRef, scrollbarState = param.scrollbarState, scrollbarRef = param.scrollbarRef, thumbRef = param.thumbRef, timeoutRef = param.timeoutRef, timeItemRefs = param.timeItemRefs;
    var handleKeyDown = function handleKeyDown(e, columnType, value) {
        handleTimeItemKeyDown(e, columnType, value);
    };
    var handleClick = function handleClick(value, columnType) {
        var isDisabled = disabledValues === null || disabledValues === void 0 ? void 0 : disabledValues.some(function(disabledValue) {
            return parseInt(disabledValue.toString(), 10) === parseInt(value, 10);
        });
        if (!isDisabled) {
            handleTimeItemClick(value, columnType);
        }
    };
    var minDisableIndex = function minDisableIndex() {
        if (!disabledValues || disabledValues.length === 0) {
            return 0;
        }
        var disabledNumbers = disabledValues.map(function(val) {
            return parseInt(val.toString(), 10);
        });
        for(var i = 0; i < values.length; i++){
            var currentValue = parseInt(values[i], 10);
            if (!disabledNumbers.includes(currentValue)) {
                return i;
            }
        }
        return 0;
    };
    var isColumnActive = currentColumn === column;
    var minAvailableIndex = minDisableIndex();
    return /*#__PURE__*/ React.createElement(StyledRoot, null, /*#__PURE__*/ React.createElement(StyledTimeColumn, {
        key: column,
        ref: columnRef,
        height: dropdownHeight,
        className: classes.timeColumn,
        tabIndex: -1
    }, values.map(function(value, index) {
        var isDisabled = disabledValues === null || disabledValues === void 0 ? void 0 : disabledValues.some(function(disabledValue) {
            return disabledValue === index;
        });
        var isActive = activeTime[column] === parseInt(value, 10);
        var getTabIndex = function getTabIndex() {
            if (isDisabled) return -1;
            var isValueActive = activeTime[column] === parseInt(value, 10);
            if (isColumnActive && isValueActive) {
                return 0;
            }
            if (index === minAvailableIndex) {
                return 0;
            }
            return -1;
        };
        var tabIndex = getTabIndex();
        var _obj;
        return /*#__PURE__*/ React.createElement(StyledTimeItem, {
            key: column + value,
            ref: function ref(el) {
                timeItemRefs.current["".concat(column, "-").concat(value)] = el;
            },
            className: cls((_obj = {}, _define_property(_obj, classes.timeItemActive, isActive), _define_property(_obj, classes.itemDisabled, isDisabled), _obj)),
            onClick: function onClick() {
                return handleClick(value, column);
            },
            onKeyDown: function onKeyDown(e) {
                return handleKeyDown(e, column, value);
            },
            "aria-disabled": isDisabled,
            tabIndex: tabIndex,
            "data-value": value,
            "data-column": column,
            "data-active": isActive
        }, value);
    }), /*#__PURE__*/ React.createElement(StyledEmpty, null)), /*#__PURE__*/ React.createElement(CustomScrollbar, {
        ref: scrollbarRef,
        className: cls(_define_property({}, classes.scrollbarVisible, scrollbarState === null || scrollbarState === void 0 ? void 0 : scrollbarState.isVisible)),
        tabIndex: -1
    }, /*#__PURE__*/ React.createElement(ScrollbarTrack, null, /*#__PURE__*/ React.createElement(ScrollbarThumb, {
        ref: thumbRef,
        style: {
            height: "".concat(scrollbarState === null || scrollbarState === void 0 ? void 0 : scrollbarState.thumbHeight, "px"),
            top: "".concat(scrollbarState === null || scrollbarState === void 0 ? void 0 : scrollbarState.thumbPosition, "px")
        },
        onMouseDown: columnRef && setScrollbar && timeoutRef ? createScrollbarDragHandler(columnRef, setScrollbar, timeoutRef) : undefined,
        tabIndex: -1
    }))));
};
