"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var 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
 */
var antd_1 = require("antd");
var moment_1 = __importDefault(require("moment"));
var react_1 = require("react");
var timecalcs_1 = require("@cocalc/util/stripe/timecalcs");
function DateRange(props) {
    var onChange = props.onChange, style = props.style, noPast = props.noPast, maxDaysInFuture = props.maxDaysInFuture, _a = props.disabled, disabled = _a === void 0 ? false : _a, _b = props.initialValues, initialValues = _b === void 0 ? [undefined, undefined] : _b;
    // 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");
    var _c = __read((0, react_1.useState)(initialValues), 2), dateRange = _c[0], setDateRange = _c[1];
    return ((0, jsx_runtime_1.jsx)("div", __assign({ style: style }, { children: (0, jsx_runtime_1.jsx)(antd_1.DatePicker.RangePicker, { disabled: disabled, allowEmpty: [true, true], renderExtraFooter: function () { return ((0, jsx_runtime_1.jsxs)("div", __assign({ 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", __assign({ style: { marginTop: "-15px" } }, { children: "Week = one week starting today" })), (0, jsx_runtime_1.jsx)("li", __assign({ style: { marginTop: "-15px" } }, { children: "+Week = one week, starting from the selected start date" }))] })] }))); }, ranges: {
                Week: [(0, moment_1.default)(), (0, moment_1.default)().add(1, "week")],
                Month: [(0, moment_1.default)(), (0, moment_1.default)().add(1, "month")],
                Year: [(0, moment_1.default)(), (0, moment_1.default)().add(1, "year")],
                "+ Week": [(0, moment_1.default)(dateRange[0]), (0, moment_1.default)(dateRange[0]).add(1, "week")],
                "+ Month": [
                    (0, moment_1.default)(dateRange[0]),
                    (0, moment_1.default)(dateRange[0]).add(1, "month"),
                ],
                "+ Three Months": [
                    (0, moment_1.default)(dateRange[0]),
                    (0, moment_1.default)(dateRange[0]).add(3, "months"),
                ],
                "+ Four Months": [
                    (0, moment_1.default)(dateRange[0]),
                    (0, moment_1.default)(dateRange[0]).add(4, "months"),
                ],
            }, value: [
                dateRange[0] ? (0, moment_1.default)(dateRange[0]) : undefined,
                dateRange[1] ? (0, moment_1.default)(dateRange[1]) : undefined,
            ], onChange: function (value) {
                var _a, _b;
                var x = [
                    (_a = value === null || value === void 0 ? void 0 : value[0]) === null || _a === void 0 ? void 0 : _a.toDate(),
                    (_b = value === null || value === void 0 ? void 0 : value[1]) === null || _b === void 0 ? void 0 : _b.toDate(),
                ];
                setDateRange(x);
                onChange === null || onChange === void 0 ? void 0 : onChange(x);
            }, disabledDate: noPast || maxDaysInFuture
                ? function (date) {
                    if (!date)
                        return false;
                    if (noPast && date <= (0, moment_1.default)().subtract(1, "days"))
                        return true;
                    if (maxDaysInFuture &&
                        date >= (0, moment_1.default)().add(maxDaysInFuture, "days"))
                        return true;
                    return false;
                }
                : undefined }) })));
}
exports.default = DateRange;
//# sourceMappingURL=date-range.js.map