"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const errors_js_1 = require("../../errors.js");
const index_js_1 = require("../../schema/index.js");
const index_js_2 = require("../deserialize/index.js");
const index_js_3 = require("../serialize/index.js");
const parseValueOrValueArray = (value) => (Array.isArray(value) ? value : [value]);
const parseStringOrTenant = (tenant) => typeof tenant === 'string' ? tenant : tenant.name;
const parseTenantREST = (tenant) => ({
    name: tenant.name,
    activityStatus: index_js_2.Deserialize.activityStatusREST(tenant.activityStatus),
});
const tenants = (connection, collection, dbVersionSupport) => {
    const get = (names) => connection
        .tenants(collection)
        .then((builder) => builder.withGet({ names }))
        .then(index_js_2.Deserialize.tenantsGet);
    const update = async (tenants) => {
        const out = [];
        for await (const res of index_js_3.Serialize.tenants(parseValueOrValueArray(tenants), index_js_3.Serialize.tenantUpdate).map((tenants) => new index_js_1.TenantsUpdater(connection, collection, tenants).do().then((res) => res.map(parseTenantREST)))) {
            out.push(...res);
        }
        return out;
    };
    return {
        create: (tenants) => new index_js_1.TenantsCreator(connection, collection, parseValueOrValueArray(tenants).map(index_js_3.Serialize.tenantCreate))
            .do()
            .then((res) => res.map(parseTenantREST)),
        get: () => get(),
        getByNames: (tenants) => get(tenants.map(parseStringOrTenant)),
        getByName: async (tenant) => {
            const tenantName = parseStringOrTenant(tenant);
            if (await dbVersionSupport.supportsTenantGetRESTMethod().then((check) => !check.supports)) {
                return get([tenantName]).then((tenants) => tenants[tenantName] ?? null);
            }
            return connection
                .get(`/schema/${collection}/tenants/${tenantName}`)
                .then(parseTenantREST)
                .catch((err) => {
                if (err instanceof errors_js_1.WeaviateUnexpectedStatusCodeError && err.code === 404) {
                    return null;
                }
                throw err;
            });
        },
        remove: (tenants) => new index_js_1.TenantsDeleter(connection, collection, parseValueOrValueArray(tenants).map(parseStringOrTenant)).do(),
        update,
        activate: (tenant) => {
            return update(parseValueOrValueArray(tenant).map((tenant) => ({
                name: parseStringOrTenant(tenant),
                activityStatus: 'ACTIVE',
            })));
        },
        deactivate: (tenant) => {
            return update(parseValueOrValueArray(tenant).map((tenant) => ({
                name: parseStringOrTenant(tenant),
                activityStatus: 'INACTIVE',
            })));
        },
        offload: (tenant) => {
            return update(parseValueOrValueArray(tenant).map((tenant) => ({
                name: parseStringOrTenant(tenant),
                activityStatus: 'OFFLOADED',
            })));
        },
    };
};
exports.default = tenants;
