"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "renderTimeColumn", {
    enumerable: true,
    get: function() {
        return renderTimeColumn;
    }
});
var _react = /*#__PURE__*/ _interop_require_default(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
var _TimePickerGridtokens = require("../../TimePickerGrid.tokens");
var _TimeColumnstyles = require("./TimeColumn.styles");
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;
}
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
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.default.createElement(_TimeColumnstyles.StyledRoot, null, /*#__PURE__*/ _react.default.createElement(_TimeColumnstyles.StyledTimeColumn, {
        key: column,
        ref: columnRef,
        height: dropdownHeight,
        className: _TimePickerGridtokens.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.default.createElement(_TimeColumnstyles.StyledTimeItem, {
            key: column + value,
            ref: function ref(el) {
                timeItemRefs.current["".concat(column, "-").concat(value)] = el;
            },
            className: (0, _classnames.default)((_obj = {}, _define_property(_obj, _TimePickerGridtokens.classes.timeItemActive, isActive), _define_property(_obj, _TimePickerGridtokens.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.default.createElement(_TimeColumnstyles.StyledEmpty, null)), /*#__PURE__*/ _react.default.createElement(_TimeColumnstyles.CustomScrollbar, {
        ref: scrollbarRef,
        className: (0, _classnames.default)(_define_property({}, _TimePickerGridtokens.classes.scrollbarVisible, scrollbarState === null || scrollbarState === void 0 ? void 0 : scrollbarState.isVisible)),
        tabIndex: -1
    }, /*#__PURE__*/ _react.default.createElement(_TimeColumnstyles.ScrollbarTrack, null, /*#__PURE__*/ _react.default.createElement(_TimeColumnstyles.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
    }))));
};
