'use strict';

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

class Authorization {
    constructor(connection) {
        this.connection = connection;
    }
    async create(request) {
        const result = await this.connection.post("authorization", request);
        return ok.Error.is(result) ? result : this.load(result);
    }
    async update(updates) {
        return await this.connection.patch("authorization/", updates);
    }
    async list(state, merchants, start, end) {
        const queryList = state ? ["state=true"] : [];
        start && queryList.push(`start=${start}`);
        end && queryList.push(`end=${end}`);
        merchants && queryList.push(`merchants=${merchants}`);
        const query = queryList.join("&");
        return await this.connection.get("authorization" + (query ? "?" + query : ""));
    }
    async get(authorization) {
        return this.connection.get("authorization/" + authorization);
    }
    load(authorization) {
        const result = {
            ...(typeof authorization == "string" ? { id: authorization } : authorization),
            cancel: () => this.connection.delete("authorization/" + result.id),
            capture: (capture) => this.connection.post("authorization/" + result.id + "/capture", capture),
            refund: (refund) => this.connection.post("authorization/" + result.id + "/refund", refund),
        };
        return result;
    }
    static open(url, key) {
        const connection = Item.Connection.open(url, key);
        return connection && new Authorization(connection);
    }
}

exports.Authorization = Authorization;
