"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.TimePickerModal = void 0;
const react_1 = __importStar(require("react"));
const Modal_1 = require("../../ui/modal/Modal");
const ModalButtons_1 = require("../../ui/modal/ModalButtons");
const TimePicker_styled_1 = require("./TimePicker.styled");
const utils_1 = require("./utils");
const Wheel_1 = require("./Wheel");
const snapToStep = (v, step, max) => {
    const snapped = Math.round(v / step) * step;
    return Math.min(snapped, max - (max % step === 0 ? step : max % step));
};
const TimePickerModal = ({ isOpen, initialValue, withSeconds, minuteStep, secondStep, readOnly, title, okLabel, cancelLabel, onClose, onConfirm, }) => {
    const [draftHours, setDraftHours] = (0, react_1.useState)(0);
    const [draftMinutes, setDraftMinutes] = (0, react_1.useState)(0);
    const [draftSeconds, setDraftSeconds] = (0, react_1.useState)(0);
    (0, react_1.useEffect)(() => {
        if (!isOpen) {
            return;
        }
        const digits = (0, utils_1.parseTimeText)(initialValue, withSeconds);
        const hh = digits.length >= 2 ? parseInt(digits.slice(0, 2), 10) : 0;
        const mmRaw = digits.length >= 4 ? parseInt(digits.slice(2, 4), 10) : 0;
        const ssRaw = digits.length >= 6 ? parseInt(digits.slice(4, 6), 10) : 0;
        setDraftHours(hh);
        setDraftMinutes(snapToStep(mmRaw, minuteStep, 60));
        setDraftSeconds(snapToStep(ssRaw, secondStep, 60));
    }, [isOpen, initialValue, withSeconds, minuteStep, secondStep]);
    const handleConfirm = (0, react_1.useCallback)(() => {
        const next = (0, utils_1.padTwo)(draftHours) + ":" + (0, utils_1.padTwo)(draftMinutes)
            + (withSeconds ? ":" + (0, utils_1.padTwo)(draftSeconds) : "");
        onConfirm(next);
    }, [draftHours, draftMinutes, draftSeconds, withSeconds, onConfirm]);
    const handleWheelsKeyDown = (0, react_1.useCallback)((e) => {
        if (e.key === "Enter" && !readOnly) {
            e.preventDefault();
            handleConfirm();
        }
    }, [readOnly, handleConfirm]);
    return (react_1.default.createElement(Modal_1.Modal, { isOpen: isOpen, onClose: onClose, title: title },
        react_1.default.createElement(TimePicker_styled_1.StyledWheelsContainer, { onKeyDown: handleWheelsKeyDown },
            react_1.default.createElement(Wheel_1.Wheel, { count: 24, step: 1, value: draftHours, onChange: setDraftHours, ariaLabel: "Hours", autoFocus: true }),
            react_1.default.createElement(TimePicker_styled_1.StyledWheelSeparator, null, ":"),
            react_1.default.createElement(Wheel_1.Wheel, { count: 60, step: minuteStep, value: draftMinutes, onChange: setDraftMinutes, ariaLabel: "Minutes" }),
            withSeconds
                ? (react_1.default.createElement(react_1.default.Fragment, null,
                    react_1.default.createElement(TimePicker_styled_1.StyledWheelSeparator, null, ":"),
                    react_1.default.createElement(Wheel_1.Wheel, { count: 60, step: secondStep, value: draftSeconds, onChange: setDraftSeconds, ariaLabel: "Seconds" })))
                : null,
            react_1.default.createElement(TimePicker_styled_1.StyledWheelHighlight, { "aria-hidden": true })),
        react_1.default.createElement(ModalButtons_1.ModalButtons, null,
            react_1.default.createElement(ModalButtons_1.ModalButtons.Button, { onClick: onClose }, cancelLabel),
            readOnly
                ? null
                : (react_1.default.createElement(ModalButtons_1.ModalButtons.Button, { variant: "main", onClick: handleConfirm }, okLabel)))));
};
exports.TimePickerModal = TimePickerModal;
//# sourceMappingURL=TimePickerModal.js.map