'use client';
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MINUTES = exports.HOURS = void 0;
exports.TimePicker = TimePicker;
const jsx_runtime_1 = require("react/jsx-runtime");
const React = __importStar(require("react"));
const utils_1 = require("../../../lib/utils");
exports.HOURS = Array.from({ length: 24 }, (_, i) => String(i).padStart(2, '0'));
exports.MINUTES = Array.from({ length: 60 }, (_, i) => String(i).padStart(2, '0'));
function TimeScrollColumn({ items, selected, onSelect, label, disabled }) {
    const listRef = React.useRef(null);
    const hasScrolled = React.useRef(false);
    const prevSelected = React.useRef(selected);
    React.useEffect(() => {
        if (prevSelected.current !== selected) {
            hasScrolled.current = false;
            prevSelected.current = selected;
        }
    }, [selected]);
    React.useEffect(() => {
        if (hasScrolled.current)
            return;
        if (!listRef.current || !selected)
            return;
        const selectedIndex = items.indexOf(selected);
        if (selectedIndex < 0)
            return;
        const selectedElement = listRef.current.children[0]?.children[selectedIndex];
        if (selectedElement) {
            selectedElement.scrollIntoView({ block: 'nearest' });
            hasScrolled.current = true;
        }
    }, [selected, items]);
    return ((0, jsx_runtime_1.jsxs)("div", { className: "flex min-h-0 min-w-0 flex-1 flex-col items-center", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-muted-foreground text-xs font-medium select-none", children: label }), (0, jsx_runtime_1.jsx)("div", { className: "h-1.5" }), (0, jsx_runtime_1.jsx)("div", { ref: listRef, className: "max-h-60 overflow-y-auto", role: "group", "aria-label": label, children: (0, jsx_runtime_1.jsx)("div", { className: "flex flex-col gap-1 p-1", children: items.map((item) => {
                        const isSelected = item === selected;
                        return ((0, jsx_runtime_1.jsx)("button", { type: "button", "aria-pressed": isSelected, disabled: disabled, className: (0, utils_1.cn)('flex h-8 w-11 items-center justify-center rounded-md text-sm tabular-nums transition-colors', isSelected
                                ? 'bg-accent text-accent-foreground font-bold'
                                : 'text-muted-foreground hover:bg-muted font-normal', disabled && 'pointer-events-none opacity-50'), onClick: () => onSelect(item), children: item }, item));
                    }) }) })] }));
}
function TimePicker({ hour, minute, onChange, disabled, className, hourLabel = 'Hour', minuteLabel = 'Minute' }) {
    const selectedHour = String(hour ?? 0).padStart(2, '0');
    const selectedMinute = String(minute ?? 0).padStart(2, '0');
    const handleHourSelect = React.useCallback((value) => {
        onChange?.({ hour: parseInt(value, 10), minute: minute ?? 0 });
    }, [onChange, minute]);
    const handleMinuteSelect = React.useCallback((value) => {
        onChange?.({ hour: hour ?? 0, minute: parseInt(value, 10) });
    }, [onChange, hour]);
    return ((0, jsx_runtime_1.jsxs)("div", { "data-slot": "time-picker", className: (0, utils_1.cn)('flex gap-2 p-3', className), children: [(0, jsx_runtime_1.jsx)(TimeScrollColumn, { items: exports.HOURS, selected: selectedHour, onSelect: handleHourSelect, label: hourLabel, disabled: disabled }), (0, jsx_runtime_1.jsx)(TimeScrollColumn, { items: exports.MINUTES, selected: selectedMinute, onSelect: handleMinuteSelect, label: minuteLabel, disabled: disabled })] }));
}
