"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 © 2021 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
const api_1 = __importDefault(require("lib/hooks/api"));
const loading_1 = __importDefault(require("components/share/loading"));
const antd_1 = require("antd");
const misc_1 = require("@cocalc/util/misc");
const license_1 = __importDefault(require("components/licenses/license"));
const A_1 = __importDefault(require("components/misc/A"));
const icon_1 = require("@cocalc/frontend/components/icon");
const timestamp_1 = __importDefault(require("components/misc/timestamp"));
function Description({ hosted_invoice_url, lines, metadata }) {
    const license_id = metadata?.license_id ?? lines.data[0].metadata?.license_id;
    return ((0, jsx_runtime_1.jsxs)("div", { style: { wordWrap: "break-word", wordBreak: "break-word" }, children: [lines.data[0].description, (lines?.total_count ?? 1) > 1 && ", etc.", hosted_invoice_url && ((0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsxs)(A_1.default, { 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({ status, due_date, 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, { 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({ created }) {
    return (0, jsx_runtime_1.jsx)(timestamp_1.default, { epoch: 1000 * created, dateOnly: true });
}
function Amount({ total, currency }) {
    return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, misc_1.stripeAmount)(total, currency) });
}
const columns = [
    {
        responsive: ["xs"],
        title: "Invoices and Receipts",
        render: (_, invoice) => ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(Description, { ...invoice }), "Created: ", (0, jsx_runtime_1.jsx)(Created, { ...invoice }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(Amount, { ...invoice }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(Status, { ...invoice })] })),
    },
    {
        responsive: ["sm"],
        title: "Description",
        width: "50%",
        render: (_, invoice) => (0, jsx_runtime_1.jsx)(Description, { ...invoice }),
    },
    {
        responsive: ["sm"],
        title: "Status",
        align: "center",
        render: (_, invoice) => (0, jsx_runtime_1.jsx)(Status, { ...invoice }),
        sorter: { compare: (a, b) => (0, misc_1.cmp)(a.status, b.status) },
    },
    {
        responsive: ["sm"],
        title: "Created",
        align: "center",
        render: (_, invoice) => (0, jsx_runtime_1.jsx)(Created, { ...invoice }),
        sorter: { compare: (a, b) => -(0, misc_1.cmp)(a.created, b.created) },
    },
    {
        responsive: ["sm"],
        title: "Amount",
        align: "right",
        render: (_, invoice) => (0, jsx_runtime_1.jsx)(Amount, { ...invoice }),
        sorter: { compare: (a, b) => (0, misc_1.cmp)(a.amount_paid ?? 0, b.amount_paid ?? 0) },
    },
];
function InvoicesAndReceipts() {
    const { result, error } = (0, api_1.default)("billing/get-invoices-and-receipts");
    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: result.data ?? [], rowKey: "id", style: { marginTop: "15px" }, pagination: { hideOnSinglePage: true, pageSize: 100 } })] }));
}
exports.default = InvoicesAndReceipts;
//# sourceMappingURL=invoices-and-receipts.js.map