"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 __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 © 2021 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var api_1 = __importDefault(require("lib/hooks/api"));
var loading_1 = __importDefault(require("components/share/loading"));
var antd_1 = require("antd");
var misc_1 = require("@cocalc/util/misc");
var license_1 = __importDefault(require("components/licenses/license"));
var A_1 = __importDefault(require("components/misc/A"));
var icon_1 = require("@cocalc/frontend/components/icon");
var timestamp_1 = __importDefault(require("components/misc/timestamp"));
function Description(_a) {
    var _b, _c, _d;
    var hosted_invoice_url = _a.hosted_invoice_url, lines = _a.lines, metadata = _a.metadata;
    var license_id = (_b = metadata === null || metadata === void 0 ? void 0 : metadata.license_id) !== null && _b !== void 0 ? _b : (_c = lines.data[0].metadata) === null || _c === void 0 ? void 0 : _c.license_id;
    return ((0, jsx_runtime_1.jsxs)("div", __assign({ style: { wordWrap: "break-word", wordBreak: "break-word" } }, { children: [lines.data[0].description, ((_d = lines === null || lines === void 0 ? void 0 : lines.total_count) !== null && _d !== void 0 ? _d : 1) > 1 && ", etc.", hosted_invoice_url && ((0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsxs)(A_1.default, __assign({ href: hosted_invoice_url }, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "external-link" }), " Invoice"] })) })), license_id && ((0, jsx_runtime_1.jsxs)("div", { children: ["License: ", (0, jsx_runtime_1.jsx)(license_1.default, { license_id: license_id })] }))] })));
}
function Status(_a) {
    var status = _a.status, due_date = _a.due_date, hosted_invoice_url = _a.hosted_invoice_url;
    if (status == "paid") {
        return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: "Paid" });
    }
    return ((0, jsx_runtime_1.jsxs)(A_1.default, __assign({ style: { color: "red" }, href: hosted_invoice_url }, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "external-link" }), " Due", " ", (0, jsx_runtime_1.jsx)(timestamp_1.default, { epoch: 1000 * due_date, dateOnly: true })] })));
}
function Created(_a) {
    var created = _a.created;
    return (0, jsx_runtime_1.jsx)(timestamp_1.default, { epoch: 1000 * created, dateOnly: true });
}
function Amount(_a) {
    var total = _a.total, currency = _a.currency;
    return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, misc_1.stripeAmount)(total, currency) });
}
var columns = [
    {
        responsive: ["xs"],
        title: "Invoices and Receipts",
        render: function (_, invoice) { return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(Description, __assign({}, invoice)), "Created: ", (0, jsx_runtime_1.jsx)(Created, __assign({}, invoice)), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(Amount, __assign({}, invoice)), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(Status, __assign({}, invoice))] })); },
    },
    {
        responsive: ["sm"],
        title: "Description",
        width: "50%",
        render: function (_, invoice) { return (0, jsx_runtime_1.jsx)(Description, __assign({}, invoice)); },
    },
    {
        responsive: ["sm"],
        title: "Status",
        align: "center",
        render: function (_, invoice) { return (0, jsx_runtime_1.jsx)(Status, __assign({}, invoice)); },
        sorter: { compare: function (a, b) { return (0, misc_1.cmp)(a.status, b.status); } },
    },
    {
        responsive: ["sm"],
        title: "Created",
        align: "center",
        render: function (_, invoice) { return (0, jsx_runtime_1.jsx)(Created, __assign({}, invoice)); },
        sorter: { compare: function (a, b) { return -(0, misc_1.cmp)(a.created, b.created); } },
    },
    {
        responsive: ["sm"],
        title: "Amount",
        align: "right",
        render: function (_, invoice) { return (0, jsx_runtime_1.jsx)(Amount, __assign({}, invoice)); },
        sorter: { compare: function (a, b) { var _a, _b; return (0, misc_1.cmp)((_a = a.amount_paid) !== null && _a !== void 0 ? _a : 0, (_b = b.amount_paid) !== null && _b !== void 0 ? _b : 0); } },
    },
];
function InvoicesAndReceipts() {
    var _a;
    var _b = (0, api_1.default)("billing/get-invoices-and-receipts"), result = _b.result, error = _b.error;
    if (error) {
        return (0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: error });
    }
    if (!result) {
        return (0, jsx_runtime_1.jsx)(loading_1.default, {});
    }
    return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("h3", { children: "Invoices and Receipts" }), "Your invoices and receipts are listed below. Click on the \"Invoice\" link to get a printable invoice or receipt version.", (0, jsx_runtime_1.jsx)(antd_1.Table, { columns: columns, dataSource: (_a = result.data) !== null && _a !== void 0 ? _a : [], rowKey: "id", style: { marginTop: "15px" }, pagination: { hideOnSinglePage: true, pageSize: 100 } })] }));
}
exports.default = InvoicesAndReceipts;
//# sourceMappingURL=invoices-and-receipts.js.map