import { ok } from "../is.js";
export * from "./client.interface.js";
export class Client {
    baseUrl;
    headers;
    partnerId;
    version;
    prefix;
    constructor({ url, accessToken, partnerId, version, prefix } = {}) {
        this.baseUrl = url ?? "https://discord.patient.nz";
        version = version ?? 1;
        this.version = version;
        this.partnerId = partnerId;
        this.prefix = prefix ?? `/api/version/${version}`;
        const headers = this.headers = new Headers();
        headers.set("Content-Type", "application/json");
        headers.set("Accept", "application/json");
        if (accessToken) {
            headers.set("Authorization", `Bearer ${accessToken}`);
        }
        if (partnerId) {
            headers.set("X-Partner-ID", partnerId);
        }
        return this;
    }
    async addPartner(partner) {
        const response = await this.fetch(`${this.prefix}/partners`, {
            method: "POST",
            body: JSON.stringify(partner)
        });
        ok(response.ok, "addPartner response not ok");
        return await response.json();
    }
    async listPartners() {
        const response = await this.fetch(`${this.prefix}/partners`);
        ok(response.ok, "listPartners response not ok");
        return response.json();
    }
    async listSystemLogs() {
        const { baseUrl, headers, prefix, partnerId } = this;
        const url = new URL(`${prefix}/system-logs`, baseUrl);
        if (partnerId) {
            url.searchParams.set("partnerId", partnerId);
        }
        const response = await this.fetch(url, {
            method: "GET",
            headers
        });
        ok(response.ok, "listSystemLogs response not ok");
        return response.json();
    }
    async background(query) {
        const url = new URL("/api/background", this.baseUrl);
        url.search = new URLSearchParams(query).toString();
        const response = await this.fetch(url);
        ok(response.ok, "background response not ok");
        await response.blob();
    }
    async fetch(givenUrl, options) {
        const { baseUrl, headers } = this;
        const url = new URL(givenUrl, baseUrl);
        return await fetch(url, {
            method: "GET",
            headers,
            ...options
        });
    }
}
//# sourceMappingURL=client.js.map