'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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DateTimePickerField = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const button_1 = require("../../../components/ui/button");
const calendar_1 = require("../../../components/ui/calendar");
const form_1 = require("../../../components/ui/form");
const popover_1 = require("../../../components/ui/popover");
const time_picker_1 = require("../../../components/ui/time-picker");
const utils_1 = require("../../../lib/utils");
const dayjs_1 = __importDefault(require("dayjs"));
const lucide_react_1 = require("lucide-react");
const React = __importStar(require("react"));
const DateTimePickerField = ({ name, label, tooltip, labelExtra, description, placeholder = 'Select date and time...', disabled, readOnly, control, required, dateFormat = 'MMM DD, YYYY HH:mm', align = 'start', minDate, maxDate, defaultTime = '00:00', ...others }) => {
    const [open, setOpen] = React.useState(false);
    const parseDefaultTime = (time) => {
        const [h, m] = time.split(':').map(Number);
        return {
            hour: Number.isFinite(h) && h >= 0 && h <= 23 ? h : 0,
            minute: Number.isFinite(m) && m >= 0 && m <= 59 ? m : 0
        };
    };
    return ((0, jsx_runtime_1.jsx)(form_1.FormField, { name: name, control: control, render: ({ field }) => {
            const current = field.value ? (0, dayjs_1.default)(field.value) : null;
            const isValid = current?.isValid() ?? false;
            const validCurrent = isValid ? current : null;
            const handleDateSelect = (date) => {
                if (!date)
                    return;
                const time = validCurrent
                    ? { hour: validCurrent.hour(), minute: validCurrent.minute() }
                    : parseDefaultTime(defaultTime);
                const combined = (0, dayjs_1.default)(date)
                    .hour(time.hour)
                    .minute(time.minute)
                    .second(0)
                    .millisecond(0);
                field.onChange(combined.toISOString());
            };
            const handleTimeChange = (time) => {
                if (!validCurrent)
                    return;
                const updated = validCurrent
                    .hour(time.hour)
                    .minute(time.minute)
                    .second(0)
                    .millisecond(0);
                field.onChange(updated.toISOString());
            };
            const handleClear = (e) => {
                e.stopPropagation();
                field.onChange('');
                setOpen(false);
            };
            const showClear = !!field.value && !readOnly && !disabled;
            return ((0, jsx_runtime_1.jsxs)(form_1.FormItem, { required: required, children: [label && ((0, jsx_runtime_1.jsx)(form_1.FormLabel, { extra: tooltip ? (0, jsx_runtime_1.jsx)(form_1.FormTooltip, { children: tooltip }) : labelExtra, children: label })), (0, jsx_runtime_1.jsxs)(popover_1.Popover, { open: open, onOpenChange: setOpen, children: [(0, jsx_runtime_1.jsxs)("div", { className: "relative w-full", children: [(0, jsx_runtime_1.jsx)(popover_1.PopoverTrigger, { asChild: true, children: (0, jsx_runtime_1.jsx)(form_1.FormControl, { children: (0, jsx_runtime_1.jsx)(button_1.Button, { variant: "outline", disabled: disabled, className: (0, utils_1.cn)('bg-input hover:bg-input border-shadcn-400 text-foreground h-10 w-full justify-start rounded-md px-2.5 font-normal', readOnly && 'pointer-events-none', showClear && 'pr-8', open && 'ring-ring ring-2 ring-offset-0'), "data-testid": others['data-testid'], icon: (0, jsx_runtime_1.jsx)(lucide_react_1.CalendarIcon, { className: "size-4" }), children: (0, jsx_runtime_1.jsx)("span", { className: (0, utils_1.cn)('text-foreground flex-1 text-left', validCurrent && 'font-bold'), children: validCurrent
                                                        ? validCurrent.format(dateFormat)
                                                        : placeholder }) }) }) }), showClear && ((0, jsx_runtime_1.jsx)("span", { role: "button", tabIndex: 0, "aria-label": "Clear date and time", className: "text-muted-foreground hover:text-foreground focus-visible:ring-ring absolute right-2 top-1/2 z-10 inline-flex -translate-y-1/2 shrink-0 rounded-sm outline-none focus-visible:ring-2", onClick: handleClear, onKeyDown: (e) => {
                                            if (e.key === 'Enter' || e.key === ' ') {
                                                e.preventDefault();
                                                handleClear(e);
                                            }
                                        }, children: (0, jsx_runtime_1.jsx)(lucide_react_1.XIcon, { className: "size-4" }) }))] }), (0, jsx_runtime_1.jsx)(popover_1.PopoverContent, { className: "w-auto p-0", align: align, children: (0, jsx_runtime_1.jsxs)("div", { className: "flex flex-col sm:flex-row sm:items-stretch", children: [(0, jsx_runtime_1.jsx)(calendar_1.Calendar, { mode: "single", defaultMonth: validCurrent ? validCurrent.toDate() : undefined, selected: validCurrent ? validCurrent.toDate() : undefined, onSelect: handleDateSelect, disabled: (date) => {
                                                if (disabled || readOnly)
                                                    return true;
                                                if (minDate &&
                                                    date < (0, dayjs_1.default)(minDate).startOf('day').toDate())
                                                    return true;
                                                if (maxDate &&
                                                    date > (0, dayjs_1.default)(maxDate).endOf('day').toDate())
                                                    return true;
                                                return false;
                                            } }), (0, jsx_runtime_1.jsx)("div", { className: "bg-border hidden w-px self-stretch sm:block" }), (0, jsx_runtime_1.jsx)("div", { className: "border-border border-t sm:hidden" }), (0, jsx_runtime_1.jsx)(time_picker_1.TimePicker, { hour: validCurrent ? validCurrent.hour() : undefined, minute: validCurrent ? validCurrent.minute() : undefined, onChange: handleTimeChange, disabled: disabled || readOnly || !validCurrent })] }) })] }), (0, jsx_runtime_1.jsx)(form_1.FormMessage, {}), description && (0, jsx_runtime_1.jsx)(form_1.FormDescription, { children: description })] }));
        } }));
};
exports.DateTimePickerField = DateTimePickerField;
