'use strict';

const Item = require('./Item-00ce8c24.js');
require('./_commonjsHelpers-537d719a.js');
require('./index-09251373.js');

var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
class Order {
    constructor(connection) {
        this.connection = connection;
    }
    create(request, tokenize) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.connection.post("order", request, {
                accept: tokenize ? "application/jwt" : "application/json",
            });
        });
    }
    update(request) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.connection.patch("order", request);
        });
    }
    list() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.connection.get("order");
        });
    }
    get(order) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.connection.get(`order/${order}`);
        });
    }
    receipt(order, format, email) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.connection.get(`order/${order}/receipt${email ? "?email=true" : ""}`, {
                accept: { pdf: "application/pdf", html: "text/html" }[format],
            });
        });
    }
    setPayment(payment, order) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.connection.put(`order/${order}/payment`, payment);
        });
    }
    createEvent(event, order) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.connection.post(`order/${order}/event`, event);
        });
    }
    load(order) {
        const result = Object.assign(Object.assign({}, (typeof order == "string" ? { id: order } : order)), { cancel: (request) => __awaiter(this, void 0, void 0, function* () { return this.createEvent(request, result.id); }), charge: (request) => __awaiter(this, void 0, void 0, function* () { return this.createEvent(request, result.id); }), refund: (request) => __awaiter(this, void 0, void 0, function* () { return this.createEvent(request, result.id); }) });
        return result;
    }
    static open(url, key) {
        const connection = Item.Connection.open(url, key);
        return connection && new Order(connection);
    }
}

exports.Order = Order;
