"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
/*
 *  This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
const antd_1 = require("antd");
const dayjs_1 = __importDefault(require("dayjs"));
const react_1 = require("react");
const timecalcs_1 = require("@cocalc/util/stripe/timecalcs");
function DateRange(props) {
    const { onChange, style, noPast, maxDaysInFuture, disabled = false, initialValues = [undefined, undefined], } = props;
    // we round values to exactly midnight, because otherwise e.g. 2022-06-12T23:58:95 will be shown as 2022-06-12
    // that's confusing and causes problems down the road
    initialValues[0] = (0, timecalcs_1.roundToMidnight)(initialValues[0], "start");
    initialValues[1] = (0, timecalcs_1.roundToMidnight)(initialValues[1], "end");
    const [dateRange, setDateRange] = (0, react_1.useState)(initialValues);
    const presets = [
        { label: "Week", value: [(0, dayjs_1.default)(), (0, dayjs_1.default)().add(1, "week")] },
        { label: "Month", value: [(0, dayjs_1.default)(), (0, dayjs_1.default)().add(1, "month")] },
        { label: "Year", value: [(0, dayjs_1.default)(), (0, dayjs_1.default)().add(1, "year")] },
        {
            label: "+ Week",
            value: [(0, dayjs_1.default)(dateRange[0]), (0, dayjs_1.default)(dateRange[0]).add(1, "week")],
        },
        {
            label: "+ Month",
            value: [(0, dayjs_1.default)(dateRange[0]), (0, dayjs_1.default)(dateRange[0]).add(1, "month")],
        },
        {
            label: "+ Three Months",
            value: [(0, dayjs_1.default)(dateRange[0]), (0, dayjs_1.default)(dateRange[0]).add(3, "months")],
        },
        {
            label: "+ Four Months",
            value: [(0, dayjs_1.default)(dateRange[0]), (0, dayjs_1.default)(dateRange[0]).add(4, "months")],
        },
    ];
    return ((0, jsx_runtime_1.jsx)("div", { style: style, children: (0, jsx_runtime_1.jsx)(antd_1.DatePicker.RangePicker, { disabled: disabled, allowEmpty: [true, true], renderExtraFooter: () => ((0, jsx_runtime_1.jsxs)("div", { style: { marginBottom: "-15px" }, children: [(0, jsx_runtime_1.jsx)("div", { children: "Select start and end dates above, with the help of the presets below:" }), (0, jsx_runtime_1.jsxs)("ul", { children: [(0, jsx_runtime_1.jsx)("li", { style: { marginTop: "-15px" }, children: "Week = one week starting today" }), (0, jsx_runtime_1.jsx)("li", { style: { marginTop: "-15px" }, children: "+Week = one week, starting from the selected start date" })] })] })), presets: presets, value: [
                dateRange[0] ? (0, dayjs_1.default)(dateRange[0]) : undefined,
                dateRange[1] ? (0, dayjs_1.default)(dateRange[1]) : undefined,
            ], onChange: (value) => {
                const x = [
                    value?.[0]?.toDate(),
                    value?.[1]?.toDate(),
                ];
                setDateRange(x);
                onChange?.(x);
            }, disabledDate: noPast || maxDaysInFuture
                ? (date) => {
                    if (!date)
                        return false;
                    if (noPast && date <= (0, dayjs_1.default)().subtract(1, "days"))
                        return true;
                    if (maxDaysInFuture &&
                        date >= (0, dayjs_1.default)().add(maxDaysInFuture, "days"))
                        return true;
                    return false;
                }
                : undefined }) }));
}
exports.default = DateRange;
//# sourceMappingURL=date-range.js.map