"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UsageAndDuration = void 0;
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 consts_1 = require("@cocalc/util/licenses/purchase/consts");
const misc_1 = require("@cocalc/util/misc");
const timecalcs_1 = require("@cocalc/util/stripe/timecalcs");
const antd_1 = require("antd");
const A_1 = __importDefault(require("components/misc/A"));
const date_range_1 = __importDefault(require("components/misc/date-range"));
const util_1 = require("./util");
const profile_1 = __importDefault(require("lib/hooks/profile"));
function UsageAndDuration(props) {
    const { showExplanations = false, form, onChange, disabled = false, showUsage = true, duration = "all", discount = true, extraDuration, } = props;
    const profile = (0, profile_1.default)();
    const { toServerTime } = (0, util_1.useTimeFixer)();
    function renderUsage() {
        if (!showUsage)
            return;
        return ((0, jsx_runtime_1.jsx)(antd_1.Form.Item, { name: "user", initialValue: (0, misc_1.isAcademic)(profile?.email_address) ? "academic" : "business", 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, { disabled: disabled, children: [(0, jsx_runtime_1.jsxs)(antd_1.Space, { direction: "vertical", style: { margin: "5px 0" }, children: [(0, jsx_runtime_1.jsx)(antd_1.Radio, { value: "business", children: "Business - for commercial purposes" }), (0, jsx_runtime_1.jsx)(antd_1.Radio, { value: "academic", children: "Academic - students, teachers, academic researchers, non-profit organizations and hobbyists (40% discount)" })] }), " "] }) }));
    }
    function fixRangeSelector(range) {
        // fixes the range to the start/end of day in the timezone of the user
        const [start, end] = range;
        const fixedStart = start != null ? toServerTime((0, timecalcs_1.startOfDay)(start)) : undefined;
        let fixedEnd = end != null ? toServerTime((0, timecalcs_1.endOfDay)(end)) : undefined;
        const fixed = [fixedStart, fixedEnd];
        return fixed;
    }
    function renderRangeSelector(getFieldValue) {
        const period = getFieldValue("period");
        if (period !== "range")
            return;
        const range = getFieldValue("range");
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(range?.[0] == null || range?.[1] == null) && ((0, jsx_runtime_1.jsx)("div", { style: { textAlign: "center", color: "#ff4d4f" }, children: "Please enter a range of dates." })), (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: (range) => {
                        range = fixRangeSelector(range);
                        form.setFieldsValue({ range });
                        onChange();
                    } })] }));
    }
    function renderRange() {
        return ((0, jsx_runtime_1.jsx)(antd_1.Form.Item, { noStyle: true, shouldUpdate: (prevValues, currentValues) => prevValues.period !== currentValues.period, children: ({ getFieldValue }) => renderRangeSelector(getFieldValue) }));
    }
    function renderSubsDiscount(duration) {
        if (!discount)
            return;
        const pct = Math.round(100 * (1 - consts_1.COSTS.sub_discount[duration]));
        return ` (discount ${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, { value: "monthly", children: ["Monthly Subscription ", renderSubsDiscount("monthly")] })), duration !== "monthly" && ((0, jsx_runtime_1.jsxs)(antd_1.Radio, { value: "yearly", children: ["Yearly Subscription ", renderSubsDiscount("yearly")] }))] }));
        }
    }
    function renderRangeOption() {
        if (duration === "all" || duration === "range") {
            return (0, jsx_runtime_1.jsx)(antd_1.Radio, { 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, { 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() {
        const 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, { name: "range", hidden: true, children: (0, jsx_runtime_1.jsx)(antd_1.Input, {}) }), (0, jsx_runtime_1.jsx)(antd_1.Form.Item, { name: "period", initialValue: init, label: "Period", extra: renderDurationExplanation(), children: (0, jsx_runtime_1.jsx)(antd_1.Radio.Group, { disabled: disabled, children: (0, jsx_runtime_1.jsxs)(antd_1.Space, { 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, { plain: true, children: [showUsage ? "Usage and " : "", "Duration"] }), renderUsage(), renderDuration()] }));
}
exports.UsageAndDuration = UsageAndDuration;
//# sourceMappingURL=usage-and-duration.js.map