"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 });
exports.UsageAndDuration = void 0;
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 consts_1 = require("@cocalc/util/licenses/purchase/consts");
var timecalcs_1 = require("@cocalc/util/stripe/timecalcs");
var antd_1 = require("antd");
var A_1 = __importDefault(require("components/misc/A"));
var date_range_1 = __importDefault(require("components/misc/date-range"));
var util_1 = require("./util");
function UsageAndDuration(props) {
    var _a = props.showExplanations, showExplanations = _a === void 0 ? false : _a, form = props.form, onChange = props.onChange, _b = props.disabled, disabled = _b === void 0 ? false : _b, _c = props.showUsage, showUsage = _c === void 0 ? true : _c, _d = props.duration, duration = _d === void 0 ? "all" : _d, _e = props.discount, discount = _e === void 0 ? true : _e, extraDuration = props.extraDuration;
    var toServerTime = (0, util_1.useTimeFixer)().toServerTime;
    function renderUsage() {
        if (!showUsage)
            return;
        return ((0, jsx_runtime_1.jsx)(antd_1.Form.Item, __assign({ name: "user", initialValue: "academic", label: "Type of Usage", extra: showExplanations ? ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: "Will this license be used for academic or commercial purposes? Academic users receive a 40% discount off the standard price." })) : undefined }, { children: (0, jsx_runtime_1.jsxs)(antd_1.Radio.Group, __assign({ disabled: disabled }, { children: [(0, jsx_runtime_1.jsxs)(antd_1.Space, __assign({ direction: "vertical", style: { margin: "5px 0" } }, { children: [(0, jsx_runtime_1.jsx)(antd_1.Radio, __assign({ value: "academic" }, { children: "Academic - students, teachers, academic researchers, non-profit organizations and hobbyists (40% discount)" })), (0, jsx_runtime_1.jsx)(antd_1.Radio, __assign({ value: "business" }, { children: "Business - for commercial business purposes" }))] })), " "] })) })));
    }
    function fixRangeSelector(range) {
        // fixes the range to the start/end of day in the timezone of the user
        var _a = __read(range, 2), start = _a[0], end = _a[1];
        var fixedStart = start != null ? toServerTime((0, timecalcs_1.startOfDay)(start)) : undefined;
        var fixedEnd = end != null ? toServerTime((0, timecalcs_1.endOfDay)(end)) : undefined;
        var fixed = [fixedStart, fixedEnd];
        return fixed;
    }
    function renderRangeSelector(getFieldValue) {
        var period = getFieldValue("period");
        if (period !== "range")
            return;
        return ((0, jsx_runtime_1.jsx)(date_range_1.default, { disabled: disabled, noPast: true, maxDaysInFuture: 365 * 4, style: { margin: "5px 0 30px", textAlign: "center" }, initialValues: getFieldValue("range"), onChange: function (range) {
                range = fixRangeSelector(range);
                form.setFieldsValue({ range: range });
                onChange();
            } }));
    }
    function renderRange() {
        return ((0, jsx_runtime_1.jsx)(antd_1.Form.Item, __assign({ noStyle: true, shouldUpdate: function (prevValues, currentValues) {
                return prevValues.period !== currentValues.period;
            } }, { children: function (_a) {
                var getFieldValue = _a.getFieldValue;
                return renderRangeSelector(getFieldValue);
            } })));
    }
    function renderSubsDiscount(duration) {
        if (!discount)
            return;
        var pct = Math.round(100 * (1 - consts_1.COSTS.sub_discount[duration]));
        return " (discount ".concat(pct, "%)");
    }
    function renderSubsOptions() {
        if (duration === "all" || duration !== "range") {
            return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [duration !== "yearly" && ((0, jsx_runtime_1.jsxs)(antd_1.Radio, __assign({ value: "monthly" }, { children: ["Monthly Subscription ", renderSubsDiscount("monthly")] }))), duration !== "monthly" && ((0, jsx_runtime_1.jsxs)(antd_1.Radio, __assign({ value: "yearly" }, { children: ["Yearly Subscription ", renderSubsDiscount("yearly")] })))] }));
        }
    }
    function renderRangeOption() {
        if (duration === "all" || duration === "range") {
            return (0, jsx_runtime_1.jsx)(antd_1.Radio, __assign({ value: "range" }, { children: "Specific Start and End Dates" }));
        }
    }
    function renderDurationExplanation() {
        if (extraDuration) {
            return extraDuration;
        }
        if (!showExplanations || !discount)
            return;
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["You receive a discount if you pay for the license monthly or yearly via a", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "/pricing/subscriptions", external: true }, { children: "recurring subscription" })), ". You can also pay once for a specific period of time. Licenses start at midnight in your local timezone on the start date and end at 23:59 your local time zone on the ending date."] }));
    }
    function renderDuration() {
        var init = duration === "range" ? "range" : "monthly";
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(antd_1.Form.Item, __assign({ name: "range", hidden: true }, { children: (0, jsx_runtime_1.jsx)(antd_1.Input, {}) })), (0, jsx_runtime_1.jsx)(antd_1.Form.Item, __assign({ name: "period", initialValue: init, label: "Period", extra: renderDurationExplanation() }, { children: (0, jsx_runtime_1.jsx)(antd_1.Radio.Group, __assign({ disabled: disabled, onChange: function (e) {
                            form.setFieldsValue({ period: e.target.value });
                        } }, { children: (0, jsx_runtime_1.jsxs)(antd_1.Space, __assign({ direction: "vertical", style: { margin: "5px 0" } }, { children: [renderSubsOptions(), renderRangeOption()] })) })) })), renderRange()] }));
    }
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(antd_1.Divider, __assign({ plain: true }, { children: [showUsage ? "Usage and " : "", "Duration"] })), renderUsage(), renderDuration()] }));
}
exports.UsageAndDuration = UsageAndDuration;
//# sourceMappingURL=usage-and-duration.js.map